Skip to content

Accepting BigInt type casts #2017

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

Closed
Kostanos opened this issue Jul 5, 2019 · 1 comment
Closed

Accepting BigInt type casts #2017

Kostanos opened this issue Jul 5, 2019 · 1 comment

Comments

@Kostanos
Copy link

Kostanos commented Jul 5, 2019

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?

@Kostanos
Copy link
Author

Kostanos commented Jul 5, 2019

Closing the issue, as the problem is in JSON.serialize, which is not support BigInt yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant