graphql-scalars icon indicating copy to clipboard operation
graphql-scalars copied to clipboard

BigInt incorrect output / serialization

Open unlight opened this issue 3 years ago • 3 comments
trafficstars

Describe the bug Incorrect output / serialization because of BigInt.prototype.toJSON as Number(this)

To Reproduce

Number(BigInt("112345667891012345"))
// => 112345667891012350

Expected behavior "112345667891012345"

  1. Remove BigInt.prototype.toJSON (to force user implement it explicitly)
  2. 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

unlight avatar Mar 07 '22 20:03 unlight

I'm sorry I don't understand what's the issue here?

ardatan avatar Mar 07 '22 21:03 ardatan

The issue that your BigInt.prototype.toJSON returns bigint as number which lead to data loss.

unlight avatar Mar 08 '22 10:03 unlight

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.

ardatan avatar Mar 23 '22 07:03 ardatan

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.

ardatan avatar Sep 29 '22 18:09 ardatan