diff --git a/src/index.ts b/src/index.ts index 75a1e6f0c0..844ecaf5a2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,6 +51,9 @@ export { GraphQLString, GraphQLBoolean, GraphQLID, + /** Int boundaries constants */ + GRAPHQL_MAX_INT, + GRAPHQL_MIN_INT, /** Built-in Directives defined by the Spec */ specifiedDirectives, GraphQLIncludeDirective, diff --git a/src/type/index.ts b/src/type/index.ts index 2561426b53..220412c4f0 100644 --- a/src/type/index.ts +++ b/src/type/index.ts @@ -151,6 +151,9 @@ export { GraphQLString, GraphQLBoolean, GraphQLID, + /** Int boundaries constants */ + GRAPHQL_MAX_INT, + GRAPHQL_MIN_INT, } from './scalars'; export { diff --git a/src/type/scalars.ts b/src/type/scalars.ts index 45b7dbffdf..06ce25aa8b 100644 --- a/src/type/scalars.ts +++ b/src/type/scalars.ts @@ -9,13 +9,17 @@ import { GraphQLError } from '../error/GraphQLError'; import type { GraphQLNamedType } from './definition'; import { GraphQLScalarType } from './definition'; -// As per the GraphQL Spec, Integers are only treated as valid when a valid -// 32-bit signed integer, providing the broadest support across platforms. -// -// n.b. JavaScript's integers are safe between -(2^53 - 1) and 2^53 - 1 because -// they are internally represented as IEEE 754 doubles. -const MAX_INT = 2147483647; -const MIN_INT = -2147483648; +/** + * Maximum possible Int value as per GraphQL Spec (32-bit signed integer). + * n.b. This differs from JavaScript's numbers that are IEEE 754 doubles safe up-to 2^53 - 1 + * */ +export const GRAPHQL_MAX_INT = 2147483647; + +/** + * Minimum possible Int value as per GraphQL Spec (32-bit signed integer). + * n.b. This differs from JavaScript's numbers that are IEEE 754 doubles safe starting at -(2^53 - 1) + * */ +export const GRAPHQL_MIN_INT = -2147483648; export const GraphQLInt = new GraphQLScalarType({ name: 'Int', @@ -39,7 +43,7 @@ export const GraphQLInt = new GraphQLScalarType({ `Int cannot represent non-integer value: ${inspect(coercedValue)}`, ); } - if (num > MAX_INT || num < MIN_INT) { + if (num > GRAPHQL_MAX_INT || num < GRAPHQL_MIN_INT) { throw new GraphQLError( 'Int cannot represent non 32-bit signed integer value: ' + inspect(coercedValue), @@ -54,7 +58,7 @@ export const GraphQLInt = new GraphQLScalarType({ `Int cannot represent non-integer value: ${inspect(inputValue)}`, ); } - if (inputValue > MAX_INT || inputValue < MIN_INT) { + if (inputValue > GRAPHQL_MAX_INT || inputValue < GRAPHQL_MIN_INT) { throw new GraphQLError( `Int cannot represent non 32-bit signed integer value: ${inputValue}`, ); @@ -70,7 +74,7 @@ export const GraphQLInt = new GraphQLScalarType({ ); } const num = parseInt(valueNode.value, 10); - if (num > MAX_INT || num < MIN_INT) { + if (num > GRAPHQL_MAX_INT || num < GRAPHQL_MIN_INT) { throw new GraphQLError( `Int cannot represent non 32-bit signed integer value: ${valueNode.value}`, valueNode,