Skip to content

Commit

Permalink
Export GRAPHQL_MAX_INT and GRAPHQL_MIN_INT (#3355)
Browse files Browse the repository at this point in the history
  • Loading branch information
tofran committed Nov 16, 2021
1 parent 085c9ef commit 11a0802
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/type/index.ts
Expand Up @@ -151,6 +151,9 @@ export {
GraphQLString,
GraphQLBoolean,
GraphQLID,
/** Int boundaries constants */
GRAPHQL_MAX_INT,
GRAPHQL_MIN_INT,
} from './scalars';

export {
Expand Down
24 changes: 14 additions & 10 deletions src/type/scalars.ts
Expand Up @@ -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<number>({
name: 'Int',
Expand All @@ -39,7 +43,7 @@ export const GraphQLInt = new GraphQLScalarType<number>({
`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),
Expand All @@ -54,7 +58,7 @@ export const GraphQLInt = new GraphQLScalarType<number>({
`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}`,
);
Expand All @@ -70,7 +74,7 @@ export const GraphQLInt = new GraphQLScalarType<number>({
);
}
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,
Expand Down

0 comments on commit 11a0802

Please sign in to comment.