Skip to content

Commit 11a0802

Browse files
authored
Export GRAPHQL_MAX_INT and GRAPHQL_MIN_INT (#3355)
1 parent 085c9ef commit 11a0802

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ export {
5151
GraphQLString,
5252
GraphQLBoolean,
5353
GraphQLID,
54+
/** Int boundaries constants */
55+
GRAPHQL_MAX_INT,
56+
GRAPHQL_MIN_INT,
5457
/** Built-in Directives defined by the Spec */
5558
specifiedDirectives,
5659
GraphQLIncludeDirective,

src/type/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ export {
151151
GraphQLString,
152152
GraphQLBoolean,
153153
GraphQLID,
154+
/** Int boundaries constants */
155+
GRAPHQL_MAX_INT,
156+
GRAPHQL_MIN_INT,
154157
} from './scalars';
155158

156159
export {

src/type/scalars.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ import { GraphQLError } from '../error/GraphQLError';
99
import type { GraphQLNamedType } from './definition';
1010
import { GraphQLScalarType } from './definition';
1111

12-
// As per the GraphQL Spec, Integers are only treated as valid when a valid
13-
// 32-bit signed integer, providing the broadest support across platforms.
14-
//
15-
// n.b. JavaScript's integers are safe between -(2^53 - 1) and 2^53 - 1 because
16-
// they are internally represented as IEEE 754 doubles.
17-
const MAX_INT = 2147483647;
18-
const MIN_INT = -2147483648;
12+
/**
13+
* Maximum possible Int value as per GraphQL Spec (32-bit signed integer).
14+
* n.b. This differs from JavaScript's numbers that are IEEE 754 doubles safe up-to 2^53 - 1
15+
* */
16+
export const GRAPHQL_MAX_INT = 2147483647;
17+
18+
/**
19+
* Minimum possible Int value as per GraphQL Spec (32-bit signed integer).
20+
* n.b. This differs from JavaScript's numbers that are IEEE 754 doubles safe starting at -(2^53 - 1)
21+
* */
22+
export const GRAPHQL_MIN_INT = -2147483648;
1923

2024
export const GraphQLInt = new GraphQLScalarType<number>({
2125
name: 'Int',
@@ -39,7 +43,7 @@ export const GraphQLInt = new GraphQLScalarType<number>({
3943
`Int cannot represent non-integer value: ${inspect(coercedValue)}`,
4044
);
4145
}
42-
if (num > MAX_INT || num < MIN_INT) {
46+
if (num > GRAPHQL_MAX_INT || num < GRAPHQL_MIN_INT) {
4347
throw new GraphQLError(
4448
'Int cannot represent non 32-bit signed integer value: ' +
4549
inspect(coercedValue),
@@ -54,7 +58,7 @@ export const GraphQLInt = new GraphQLScalarType<number>({
5458
`Int cannot represent non-integer value: ${inspect(inputValue)}`,
5559
);
5660
}
57-
if (inputValue > MAX_INT || inputValue < MIN_INT) {
61+
if (inputValue > GRAPHQL_MAX_INT || inputValue < GRAPHQL_MIN_INT) {
5862
throw new GraphQLError(
5963
`Int cannot represent non 32-bit signed integer value: ${inputValue}`,
6064
);
@@ -70,7 +74,7 @@ export const GraphQLInt = new GraphQLScalarType<number>({
7074
);
7175
}
7276
const num = parseInt(valueNode.value, 10);
73-
if (num > MAX_INT || num < MIN_INT) {
77+
if (num > GRAPHQL_MAX_INT || num < GRAPHQL_MIN_INT) {
7478
throw new GraphQLError(
7579
`Int cannot represent non 32-bit signed integer value: ${valueNode.value}`,
7680
valueNode,

0 commit comments

Comments
 (0)