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:
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,
});
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?
Yes, there's no concept like bigInt
in graphQL
You can try one of these,
set type as Float
instead Int
it will handle all large int values and also send as number
.
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
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments