graphql-scalars
graphql-scalars copied to clipboard
BigInt incorrect output / serialization
Describe the bug
Incorrect output / serialization because of BigInt.prototype.toJSON as Number(this)
To Reproduce
Number(BigInt("112345667891012345"))
// => 112345667891012350
Expected behavior "112345667891012345"
- Remove BigInt.prototype.toJSON (to force user implement it explicitly)
- or use toString() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json
Environment:
- OS: Windows
- GraphQL Scalars Version: 1.15.0
- NodeJS: 16
I'm sorry I don't understand what's the issue here?
The issue that your BigInt.prototype.toJSON returns bigint as number which lead to data loss.
I'll think about that. In the meanwhile, you can try json-bigint-patch that implements BigInt serialization in JSON.parse and JSON.stringify correctly.
I think it returns string if the number is higher than 52-bit.
See https://github.com/Urigo/graphql-scalars/blob/master/src/scalars/BigInt.ts#L78
So if you still have issues with that, please share a reproduction in CodeSandbox or StackBlitz. Thanks.