graphql-js icon indicating copy to clipboard operation
graphql-js copied to clipboard

Handling of `NaN` on `Int` scalar violates GraphQL Specification

Open robross0606 opened this issue 2 months ago • 0 comments

When resolving an output value through Int scalar, NaN causes an error to be thrown:

Int cannot represent non-integer value: NaN

  const schema = makeExecutableSchema({
    typeDefs: /* GraphQL */ `
    type Query {
      testInt: Int
    }
  `,
    resolvers: {
      Query: {
        testInt: () => NaN,
      },
    },
  });
  const query = parse(/* GraphQL */ `query {
    testInt
  }`);
  const result = executeSync({
    schema,
    document: query,
  });
  expect(result).toEqual({ data: { testInt: null }, errors: [expect.objectContaining({ message: 'Int cannot represent non-integer value: NaN' })] });

However, the GraphQL Specification states that NaN should be coerced to null and returned:

  1. If result is null (or another internal value similar to null such as undefined or NaN), return null.

robross0606 avatar Oct 03 '25 12:10 robross0606