Skip to content

Commit 2c83c7f

Browse files
committed
Fix Int coercion
Int is now properly coerced for values between 2^32 and MAX_INT. Fixes #69
1 parent 18a9ac7 commit 2c83c7f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/type/__tests__/coercion.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ describe('Type System: Scalar coercion', () => {
4141
expect(
4242
GraphQLInt.coerce(1e5)
4343
).to.equal(100000);
44+
// Bigger than 2^32, but still representable as an Int
45+
expect(
46+
GraphQLInt.coerce(9876504321)
47+
).to.equal(9876504321);
48+
expect(
49+
GraphQLInt.coerce(-9876504321)
50+
).to.equal(-9876504321);
51+
// Too big to represent as an Int
4452
expect(
4553
GraphQLInt.coerce(1e100)
4654
).to.equal(null);

src/type/scalars.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ export var GraphQLInt = new GraphQLScalarType({
2121
name: 'Int',
2222
coerce(value) {
2323
var num = +value;
24-
return num === num && num <= MAX_INT && num >= MIN_INT ? num | 0 : null;
24+
if (num === num && num <= MAX_INT && num >= MIN_INT) {
25+
return (num < 0 ? Math.ceil : Math.floor)(num);
26+
}
27+
return null;
2528
},
2629
coerceLiteral(ast) {
2730
if (ast.kind === Kind.INT) {
@@ -30,6 +33,7 @@ export var GraphQLInt = new GraphQLScalarType({
3033
return num;
3134
}
3235
}
36+
return null;
3337
}
3438
});
3539

0 commit comments

Comments
 (0)