我正在尝试使用 GraphQL
在 MongoDB
中存储 UNIX 时间戳,但它发现 GraphQL 在处理整数方面存在限制。请参阅下面的突变:
const addUser = {
type: UserType,
description: 'Add an user',
args: {
data: {
name: 'data',
type: new GraphQLNonNull(CompanyInputType)
}
},
resolve(root, params) {
params.data.creationTimestamp = Date.now();
const model = new UserModel(params.data);
const saved = model.save();
if (!saved)
throw new Error('Error adding user');
return saved;
}
}
结果:
"errors": [
{
"message": "Int cannot represent non 32-bit signed integer value: 1499484833027",
"locations": [
{
"line": 14,
"column": 5
}
],
"path": [
"addUser",
"creationTimestamp"
]
}
我目前在类型定义的这个字段中使用 GraphQLInteger
:
creationTimestamp: {
type: GraphQLInt
}
如果 GraphQL
中没有更大的 GraphQLInt
可用,我该如何解决?
最佳答案
GraphQL 不支持大于 32 位的整数,如错误所示。最好使用自定义标量,如 GraphQL Date .还有一个“长”类型可用here .或者你可以滚动你自己的自定义类型; Apollo 有一个很好的例子here .
如果你好奇为什么 GraphQL 不支持更大的东西,你可以查看 this issue on Github .
关于javascript - GraphQL 大整数错误 : Int cannot represent non 32-bit signed integer value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44982279/