diff --git a/src/type/__tests__/scalars-test.ts b/src/type/__tests__/scalars-test.ts index 4d563aee10..73c1b345c8 100644 --- a/src/type/__tests__/scalars-test.ts +++ b/src/type/__tests__/scalars-test.ts @@ -161,17 +161,14 @@ describe('Type System: Specified scalar types', () => { expect(() => serialize(-1e100)).to.throw( 'Int cannot represent non 32-bit signed integer value: -1e+100', ); - expect(() => serialize('one')).to.throw( - 'Int cannot represent non-integer value: "one"', - ); // Doesn't represent number expect(() => serialize('')).to.throw( 'Int cannot represent non-integer value: ""', ); - expect(() => serialize(NaN)).to.throw( - 'Int cannot represent non-integer value: NaN', - ); + + expect(serialize(NaN)).to.equal(null); + expect(() => serialize(Infinity)).to.throw( 'Int cannot represent non-integer value: Infinity', ); @@ -293,18 +290,11 @@ describe('Type System: Specified scalar types', () => { }; expect(serialize(customValueOfObj)).to.equal(5.5); - expect(() => serialize(NaN)).to.throw( - 'Float cannot represent non numeric value: NaN', - ); + expect(serialize(NaN)).to.equal(null); + expect(() => serialize(Infinity)).to.throw( 'Float cannot represent non numeric value: Infinity', ); - expect(() => serialize('one')).to.throw( - 'Float cannot represent non numeric value: "one"', - ); - expect(() => serialize('')).to.throw( - 'Float cannot represent non numeric value: ""', - ); expect(() => serialize([5])).to.throw( 'Float cannot represent non numeric value: [5]', ); diff --git a/src/type/scalars.ts b/src/type/scalars.ts index 4990347887..fa5c55ab75 100644 --- a/src/type/scalars.ts +++ b/src/type/scalars.ts @@ -21,7 +21,7 @@ export const GRAPHQL_MAX_INT = 2147483647; * */ export const GRAPHQL_MIN_INT = -2147483648; -export const GraphQLInt = new GraphQLScalarType({ +export const GraphQLInt = new GraphQLScalarType({ name: 'Int', description: 'The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.', @@ -38,6 +38,10 @@ export const GraphQLInt = new GraphQLScalarType({ num = Number(coercedValue); } + if (typeof num === 'number' && Number.isNaN(num)) { + return null; + } + if (typeof num !== 'number' || !Number.isInteger(num)) { throw new GraphQLError( `Int cannot represent non-integer value: ${inspect(coercedValue)}`, @@ -84,7 +88,7 @@ export const GraphQLInt = new GraphQLScalarType({ }, }); -export const GraphQLFloat = new GraphQLScalarType({ +export const GraphQLFloat = new GraphQLScalarType({ name: 'Float', description: 'The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).', @@ -101,6 +105,10 @@ export const GraphQLFloat = new GraphQLScalarType({ num = Number(coercedValue); } + if (typeof num === 'number' && Number.isNaN(num)) { + return null; + } + if (typeof num !== 'number' || !Number.isFinite(num)) { throw new GraphQLError( `Float cannot represent non numeric value: ${inspect(coercedValue)}`, diff --git a/src/utilities/__tests__/astFromValue-test.ts b/src/utilities/__tests__/astFromValue-test.ts index b8f2361bd7..71a5ba09c7 100644 --- a/src/utilities/__tests__/astFromValue-test.ts +++ b/src/utilities/__tests__/astFromValue-test.ts @@ -79,10 +79,6 @@ describe('astFromValue', () => { expect(() => astFromValue(1e40, GraphQLInt)).to.throw( 'Int cannot represent non 32-bit signed integer value: 1e+40', ); - - expect(() => astFromValue(NaN, GraphQLInt)).to.throw( - 'Int cannot represent non-integer value: NaN', - ); }); it('converts Float values to Int/Float ASTs', () => {