Skip to content

Allow for serializing int as BigInt #4223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/type/__tests__/scalars-test.ts
Original file line number Diff line number Diff line change
@@ -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';
5 changes: 4 additions & 1 deletion src/type/scalars.ts
Original file line number Diff line number Diff line change
@@ -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(
@@ -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)}`);
},