Skip to content

Commit

Permalink
Fix Int coercion
Browse files Browse the repository at this point in the history
Int is now properly coerced for values between 2^32 and MAX_INT. Fixes #69
  • Loading branch information
leebyron committed Jul 17, 2015
1 parent 18a9ac7 commit 2c83c7f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/type/__tests__/coercion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/type/scalars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -30,6 +33,7 @@ export var GraphQLInt = new GraphQLScalarType({
return num;
}
}
return null;
}
});

Expand Down

0 comments on commit 2c83c7f

Please sign in to comment.