Skip to content

Commit 1318fa5

Browse files
JoviDeCroockHkmu
andcommitted
Allow for serializing int as BigInt
Co-Authored-By: Hkmu <[email protected]>
1 parent 50607e4 commit 1318fa5

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/type/__tests__/scalars-test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ 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');
538+
expect(parseValue(1n)).to.equal('1');
537539

538540
// Maximum and minimum safe numbers in JS
539541
expect(parseValue(9007199254740991)).to.equal('9007199254740991');
@@ -614,6 +616,8 @@ describe('Type System: Specified scalar types', () => {
614616
expect(serialize(123)).to.equal('123');
615617
expect(serialize(0)).to.equal('0');
616618
expect(serialize(-1)).to.equal('-1');
619+
expect(serialize(BigInt(123))).to.equal('123');
620+
expect(serialize(1n)).to.equal('1');
617621

618622
const valueOf = () => 'valueOf ID';
619623
const toJSON = () => 'toJSON ID';

src/type/scalars.ts

+4-1
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)