Skip to content

Accepting BigInt type casts #2017

Closed
Closed
@Kostanos

Description

@Kostanos

There are several issues opened regarding BigInt, none of them actually fixes the issue.

If maintainers do not want to create a new type like GraphQLBigInt, at least could be added support for custom type cast.

Example:

const { GraphQLScalarType } = require('graphql');
const { INT } = require ('graphql/language/kinds');

const MAX_INT = Number.MAX_SAFE_INTEGER;
const MIN_INT = Number.MIN_SAFE_INTEGER;

const coerceBigint = value => {
  if (value === '') {
    throw new TypeError(
      'Bigint cannot represent non 64-bit signed integer value: (empty string)',
    );
  }
  const num = BigInt(value);
  if (num > MAX_INT || num < MIN_INT) {
    throw new TypeError(
      `Bigint cannot represent non 64-bit signed integer value: ${String(
        value,
      )}`,
    );
  }
  return num;
};

const GraphQLBigInt = new GraphQLScalarType({
  name: 'BigInt',
  description:
    'The `Bigint` scalar type 64 bit',
  serialize: coerceBigint,
  parseValue: coerceBigint,
  parseLiteral(ast) {
    if (ast.kind === INT) {
      const num = BigInt(ast.value);
      if (num <= MAX_INT && num >= MIN_INT) {
        return num;
      }
    }
    return null;
  },
});

exports.GraphQLBigInt = GraphQLBigInt;

gives me the error:

Error: Schema must contain uniquely named types but contains multiple types named "BigInt"

So, technically the only way is to convert to String, which will not work for many cases.

Can we just suppress the error above, which will allow actually to use the BigInt?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions