Skip to content

Commit 06f97b6

Browse files
committed
Spec compliant Int sizing
As discussed in #182, this was a deviation between spec and reference implementation, where the spec is more reasonable. This tightens the allowed range of Int values to valid 32-bit signed integers, supporting the broadest collection of platforms. For those who stumble upon this rev looking for ways to represent numeric-looking large values, see ID or Custom Scalars.
1 parent 0d03392 commit 06f97b6

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/type/__tests__/serialization.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ describe('Type System: Scalar coercion', () => {
4141
expect(
4242
GraphQLInt.serialize(1e5)
4343
).to.equal(100000);
44-
// Bigger than 2^32, but still representable as an Int
44+
// Maybe a safe JavaScript int, but bigger than 2^32, so not
45+
// representable as a GraphQL Int
4546
expect(
4647
GraphQLInt.serialize(9876504321)
47-
).to.equal(9876504321);
48+
).to.equal(null);
4849
expect(
4950
GraphQLInt.serialize(-9876504321)
50-
).to.equal(-9876504321);
51-
// Too big to represent as an Int
51+
).to.equal(null);
52+
// Too big to represent as an Int in JavaScript or GraphQL
5253
expect(
5354
GraphQLInt.serialize(1e100)
5455
).to.equal(null);

src/type/scalars.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
import { GraphQLScalarType } from './definition';
1212
import { Kind } from '../language';
1313

14-
// Integers are only safe when between -(2^53 - 1) and 2^53 - 1 due to being
15-
// encoded in JavaScript and represented in JSON as double-precision floating
16-
// point numbers, as specified by IEEE 754.
17-
var MAX_INT = 9007199254740991;
18-
var MIN_INT = -9007199254740991;
14+
// As per the GraphQL Spec, Integers are only treated as valid when a valid
15+
// 32-bit signed integer, providing the broadest support across platforms.
16+
//
17+
// n.b. JavaScript's integers are safe between -(2^53 - 1) and 2^53 - 1 because
18+
// they are internally represented as IEEE 754 doubles.
19+
var MAX_INT = 2147483647;
20+
var MIN_INT = -2147483648;
1921

2022
function coerceInt(value) {
2123
var num = Number(value);

0 commit comments

Comments
 (0)