Skip to content

Commit

Permalink
Allow for serializing int as BigInt
Browse files Browse the repository at this point in the history
Co-Authored-By: Hkmu <3169251+hkmu@users.noreply.github.com>
  • Loading branch information
JoviDeCroock and Hkmu committed Oct 11, 2024
1 parent 50607e4 commit 1318fa5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/type/__tests__/scalars-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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';
Expand Down
5 changes: 4 additions & 1 deletion src/type/scalars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export const GraphQLID = new GraphQLScalarType<string>({
if (typeof coercedValue === 'string') {
return coercedValue;
}
if (Number.isInteger(coercedValue)) {
if (Number.isInteger(coercedValue) || typeof coercedValue === 'bigint') {
return String(coercedValue);
}
throw new GraphQLError(
Expand All @@ -267,6 +267,9 @@ export const GraphQLID = new GraphQLScalarType<string>({
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)}`);
},

Expand Down

0 comments on commit 1318fa5

Please sign in to comment.