From 1318fa5703c6394787c5b1bf0ba35d0167f4413e Mon Sep 17 00:00:00 2001 From: jdecroock Date: Fri, 11 Oct 2024 09:16:00 +0200 Subject: [PATCH] Allow for serializing int as BigInt Co-Authored-By: Hkmu <3169251+hkmu@users.noreply.github.com> --- src/type/__tests__/scalars-test.ts | 4 ++++ src/type/scalars.ts | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/type/__tests__/scalars-test.ts b/src/type/__tests__/scalars-test.ts index fbb2cd9087..d0ecb04fb7 100644 --- a/src/type/__tests__/scalars-test.ts +++ b/src/type/__tests__/scalars-test.ts @@ -534,6 +534,8 @@ describe('Type System: Specified scalar types', () => { expect(parseValue(1)).to.equal('1'); expect(parseValue(0)).to.equal('0'); expect(parseValue(-1)).to.equal('-1'); + expect(parseValue(BigInt(123))).to.equal('123'); + expect(parseValue(1n)).to.equal('1'); // Maximum and minimum safe numbers in JS expect(parseValue(9007199254740991)).to.equal('9007199254740991'); @@ -614,6 +616,8 @@ describe('Type System: Specified scalar types', () => { expect(serialize(123)).to.equal('123'); expect(serialize(0)).to.equal('0'); expect(serialize(-1)).to.equal('-1'); + expect(serialize(BigInt(123))).to.equal('123'); + expect(serialize(1n)).to.equal('1'); const valueOf = () => 'valueOf ID'; const toJSON = () => 'toJSON ID'; diff --git a/src/type/scalars.ts b/src/type/scalars.ts index a9003f7d54..c11032e9b9 100644 --- a/src/type/scalars.ts +++ b/src/type/scalars.ts @@ -252,7 +252,7 @@ export const GraphQLID = new GraphQLScalarType({ if (typeof coercedValue === 'string') { return coercedValue; } - if (Number.isInteger(coercedValue)) { + if (Number.isInteger(coercedValue) || typeof coercedValue === 'bigint') { return String(coercedValue); } throw new GraphQLError( @@ -267,6 +267,9 @@ export const GraphQLID = new GraphQLScalarType({ if (typeof inputValue === 'number' && Number.isInteger(inputValue)) { return inputValue.toString(); } + if (typeof inputValue === 'bigint') { + return inputValue.toString(); + } throw new GraphQLError(`ID cannot represent value: ${inspect(inputValue)}`); },