From 2c83c7f9c40ce80fa64ffde2ae4f62be32ecc96d Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Thu, 16 Jul 2015 17:44:05 -0700 Subject: [PATCH] Fix Int coercion Int is now properly coerced for values between 2^32 and MAX_INT. Fixes #69 --- src/type/__tests__/coercion.js | 8 ++++++++ src/type/scalars.js | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/type/__tests__/coercion.js b/src/type/__tests__/coercion.js index 01244e916f..3b1454a43c 100644 --- a/src/type/__tests__/coercion.js +++ b/src/type/__tests__/coercion.js @@ -41,6 +41,14 @@ describe('Type System: Scalar coercion', () => { expect( GraphQLInt.coerce(1e5) ).to.equal(100000); + // Bigger than 2^32, but still representable as an Int + expect( + GraphQLInt.coerce(9876504321) + ).to.equal(9876504321); + expect( + GraphQLInt.coerce(-9876504321) + ).to.equal(-9876504321); + // Too big to represent as an Int expect( GraphQLInt.coerce(1e100) ).to.equal(null); diff --git a/src/type/scalars.js b/src/type/scalars.js index 594cf4a636..472c5cef48 100644 --- a/src/type/scalars.js +++ b/src/type/scalars.js @@ -21,7 +21,10 @@ export var GraphQLInt = new GraphQLScalarType({ name: 'Int', coerce(value) { var num = +value; - return num === num && num <= MAX_INT && num >= MIN_INT ? num | 0 : null; + if (num === num && num <= MAX_INT && num >= MIN_INT) { + return (num < 0 ? Math.ceil : Math.floor)(num); + } + return null; }, coerceLiteral(ast) { if (ast.kind === Kind.INT) { @@ -30,6 +33,7 @@ export var GraphQLInt = new GraphQLScalarType({ return num; } } + return null; } });