Skip to content

Commit ae23c90

Browse files
Handle NaN as input value (#1369)
1 parent 85971a2 commit ae23c90

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

src/type/definition.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import applyToStringTag from '../jsutils/applyToStringTag';
1111
import instanceOf from '../jsutils/instanceOf';
1212
import inspect from '../jsutils/inspect';
1313
import invariant from '../jsutils/invariant';
14-
import isInvalid from '../jsutils/isInvalid';
1514
import keyMap from '../jsutils/keyMap';
1615
import type { ObjMap } from '../jsutils/ObjMap';
1716
import { Kind } from '../language/kinds';
@@ -566,9 +565,6 @@ export class GraphQLScalarType {
566565
// Parses an externally provided value to use as an input.
567566
parseValue(value: mixed): mixed {
568567
const parser = this._scalarConfig.parseValue;
569-
if (isInvalid(value)) {
570-
return undefined;
571-
}
572568
return parser ? parser(value) : value;
573569
}
574570

src/utilities/__tests__/coerceValue-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ describe('coerceValue', () => {
8383
]);
8484
});
8585

86+
it('returns a single error for NaN input as int', () => {
87+
const result = coerceValue(NaN, GraphQLInt);
88+
expectErrors(result).to.deep.equal([
89+
'Expected type Int; Int cannot represent non-integer value: NaN',
90+
]);
91+
});
92+
8693
it('returns a single error for Infinity input as int', () => {
8794
const result = coerceValue(Infinity, GraphQLInt);
8895
expectErrors(result).to.deep.equal([
@@ -133,6 +140,13 @@ describe('coerceValue', () => {
133140
]);
134141
});
135142

143+
it('returns a single error for NaN input', () => {
144+
const result = coerceValue(NaN, GraphQLFloat);
145+
expectErrors(result).to.deep.equal([
146+
'Expected type Float; Float cannot represent non numeric value: NaN',
147+
]);
148+
});
149+
136150
it('returns a single error for Infinity input', () => {
137151
const result = coerceValue(Infinity, GraphQLFloat);
138152
expectErrors(result).to.deep.equal([

src/utilities/coerceValue.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import { forEach, isCollection } from 'iterall';
1111
import inspect from '../jsutils/inspect';
1212
import isInvalid from '../jsutils/isInvalid';
13-
import isNullish from '../jsutils/isNullish';
1413
import orList from '../jsutils/orList';
1514
import suggestionList from '../jsutils/suggestionList';
1615
import { GraphQLError } from '../error';
@@ -46,7 +45,7 @@ export function coerceValue(
4645
): CoercedValue {
4746
// A value must be provided if the type is non-null.
4847
if (isNonNullType(type)) {
49-
if (isNullish(value)) {
48+
if (value == null) {
5049
return ofErrors([
5150
coercionError(
5251
`Expected non-nullable type ${inspect(type)} not to be null`,
@@ -58,7 +57,7 @@ export function coerceValue(
5857
return coerceValue(value, type.ofType, blameNode, path);
5958
}
6059

61-
if (isNullish(value)) {
60+
if (value == null) {
6261
// Explicitly return the value null.
6362
return ofValue(null);
6463
}

0 commit comments

Comments
 (0)