如您所知,GraphQL 没有像 long int 这样的数据类型。因此,每当数字大到 10000000000
时,它就会抛出这样的错误: Int cannot represent non 32-bit signed integer value: 1000000000000
为此,我知道两种解决方案:
- 使用标量。
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,
});
这两种解决方案都将 big int 转换为 string
,我宁愿不使用字符串(我更喜欢数字类型)。
原文由 Harsh Patel 发布,翻译遵循 CC BY-SA 4.0 许可协议
正确,在
graphQL
中没有像bigInt
这样的概念。您可以尝试其中之一:
Float
而不是Int
它会处理所有大的 int 值并将它们发送为number
技术选项,尽管你在技术上没有说明 - [不喜欢string
解决方案]String
,如您所述BigInt
。它将处理 big int,但会将您的值转换为string
支持 Graphql v16 的 npm BigInt 依赖项: