Skip to content

Commit 58d5b5b

Browse files
committed
Allow for serializing int as BigInt
1 parent 50607e4 commit 58d5b5b

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/type/__tests__/scalars-test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ describe('Type System: Specified scalar types', () => {
534534
expect(parseValue(1)).to.equal('1');
535535
expect(parseValue(0)).to.equal('0');
536536
expect(parseValue(-1)).to.equal('-1');
537+
expect(parseValue(BigInt(123))).to.equal('123');
537538

538539
// Maximum and minimum safe numbers in JS
539540
expect(parseValue(9007199254740991)).to.equal('9007199254740991');
@@ -614,6 +615,7 @@ describe('Type System: Specified scalar types', () => {
614615
expect(serialize(123)).to.equal('123');
615616
expect(serialize(0)).to.equal('0');
616617
expect(serialize(-1)).to.equal('-1');
618+
expect(serialize(BigInt(123))).to.equal('123');
617619

618620
const valueOf = () => 'valueOf ID';
619621
const toJSON = () => 'toJSON ID';

src/type/scalars.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export const GraphQLID = new GraphQLScalarType<string>({
252252
if (typeof coercedValue === 'string') {
253253
return coercedValue;
254254
}
255-
if (Number.isInteger(coercedValue)) {
255+
if (Number.isInteger(coercedValue) || typeof coercedValue === 'bigint') {
256256
return String(coercedValue);
257257
}
258258
throw new GraphQLError(
@@ -267,6 +267,9 @@ export const GraphQLID = new GraphQLScalarType<string>({
267267
if (typeof inputValue === 'number' && Number.isInteger(inputValue)) {
268268
return inputValue.toString();
269269
}
270+
if (typeof inputValue === 'bigint') {
271+
return inputValue.toString();
272+
}
270273
throw new GraphQLError(`ID cannot represent value: ${inspect(inputValue)}`);
271274
},
272275

0 commit comments

Comments
 (0)