How can I handle long Int with GraphQL?

Harsh Patel

As you know that GraphQL has no data type like long int. So, whenever the number is something big like 10000000000, it throws an error like this: Int cannot represent non 32-bit signed integer value: 1000000000000

For that I know two solutions:

  1. Use scalars.
import { GraphQLScalarType } from 'graphql';
import { makeExecutableSchema } from '@graphql-tools/schema';

const myCustomScalarType = new GraphQLScalarType({
  name: 'MyCustomScalar',
  description: 'Description of my custom scalar type',
  serialize(value) {
    let result;
    return result;
  },
  parseValue(value) {
    let result;
    return result;
  },
  parseLiteral(ast) {
    switch (ast.kind) {
    }
  }
});

const schemaString = `

scalar MyCustomScalar

type Foo {
  aField: MyCustomScalar
}

type Query {
  foo: Foo
}

`;

const resolverFunctions = {
  MyCustomScalar: myCustomScalarType
};

const jsSchema = makeExecutableSchema({
  typeDefs: schemaString,
  resolvers: resolverFunctions,
});
  1. Use apollo-type-bigint package.

Both of them convert the big int to string and that's not the perfect solution.

Please if you know then help me. How to pass big int as number only not string?

lazzy_ms

Yes, there's no concept like bigInt in graphQL

You can try one of these,

  1. set type as Float instead Int it will handle all large int values and also send as number.

  2. if the data type does not matter then you can also give BigInt data type. it will also handle big int but will convert your values into the string

  3. Again data type is not the issue for you then you can choose npm to handle your case

npm links:

https://www.npmjs.com/package/apollo-type-bigint

https://www.npmjs.com/package/graphql-bigint

https://www.npmjs.com/package/graphql-type-bigint

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can't I assign a final long to an int?

Can I assume the size of long int is always 4 bytes?

How should I map long to int in hashCode()?

Java: Why can't I cast int to Long

How can I cast a Long to an int in Java?

How do I handle long text labels on a universal storyboard?

how can I handle <>f__AnonymousType0`2[System.Int32,System.String][]?

How I can use graphql with angular 1.5?

How can I use from GraphQl in android?

How can I authenticate a GraphQL endpoint with Passport?

How can I use Paw as a GraphQL client

How can I set a length (for example "short" or "long") to a int variable?

How do I handle long running jobs in Azure App Service?

How can I pass a long int to itoa function?

How do I handle an int** in Ada?

How can I handle an option made of an arbitrary number with Getopt::Long[::Descriptive]?

How can I create a long int from a char buffer?

How can I create a typesafe handle that behaves almost exactly like int64_t, but disallow implicit conversions between handle types?

How can I convert long to int in JavaScript?

How can I convert Long[] to IEnumerable<long?>

Why can't I use "long long int" with "int" in my C code?

Why can't I convert signed int to unsigned long

How can I handle so much logic / linked fields (a bit long)?

How can I get user by userName in GraphQL?

How can I compare Long and Int with assertEquals. Junit Kotlin

How can i provide a a value for an argument in GraphQL?

How can I call mutation in GraphQL playground?

How do I create a timer that I can query during the program and whose format is int, long or double?

How I can use a long int and a long int array in assembly languages