Skip to content

astFromValue: reject non-finite numbers #2437

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

Merged
merged 1 commit into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions src/utilities/__tests__/astFromValue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ describe('astFromValue', () => {
value: 'value',
});

expect(astFromValue(NaN, passthroughScalar)).to.equal(null);
expect(() => astFromValue(Infinity, passthroughScalar)).to.throw(
'Cannot convert value to AST: Infinity.',
);

const returnNullScalar = new GraphQLScalarType({
name: 'ReturnNullScalar',
serialize() {
Expand Down
3 changes: 2 additions & 1 deletion src/utilities/astFromValue.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow strict

import isFinite from '../polyfills/isFinite';
import arrayFrom from '../polyfills/arrayFrom';
import objectValues from '../polyfills/objectValues';

Expand Down Expand Up @@ -116,7 +117,7 @@ export function astFromValue(value: mixed, type: GraphQLInputType): ?ValueNode {
}

// JavaScript numbers can be Int or Float values.
if (typeof serialized === 'number') {
if (typeof serialized === 'number' && isFinite(serialized)) {
const stringNum = String(serialized);
return integerStringRegExp.test(stringNum)
? { kind: Kind.INT, value: stringNum }
Expand Down