Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export GRAPHQL_MAX_INT and GRAPHQL_MIN_INT #3355

Merged
merged 3 commits into from Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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