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:
- Use scalars.
JavaScript
x
43
43
1
import { GraphQLScalarType } from 'graphql';
2
import { makeExecutableSchema } from '@graphql-tools/schema';
3
4
const myCustomScalarType = new GraphQLScalarType({
5
name: 'MyCustomScalar',
6
description: 'Description of my custom scalar type',
7
serialize(value) {
8
let result;
9
return result;
10
},
11
parseValue(value) {
12
let result;
13
return result;
14
},
15
parseLiteral(ast) {
16
switch (ast.kind) {
17
}
18
}
19
});
20
21
const schemaString = `
22
23
scalar MyCustomScalar
24
25
type Foo {
26
aField: MyCustomScalar
27
}
28
29
type Query {
30
foo: Foo
31
}
32
33
`;
34
35
const resolverFunctions = {
36
MyCustomScalar: myCustomScalarType
37
};
38
39
const jsSchema = makeExecutableSchema({
40
typeDefs: schemaString,
41
resolvers: resolverFunctions,
42
});
43
Both of those solutions convert the big int to string
, and I’d rather not use string (I prefer a number type).
Advertisement
Answer
Correct, there’s no concept like bigInt
in graphQL
.
You can try one of these:
- Set type as
Float
instead ofInt
– it will handle all large int values and also send them asnumber
[below are technically options, although you stated you don’t likestring
solutions] - Set type as
String
, as you described - Give (an npm-provided data type, like)
BigInt
, as you described. It will handle big int, but will convert your values intostring
npm BigInt dependency with Graphql v16 support: