javascript - GraphQL 大整数错误 : Int cannot represent non 32-bit signed integer value

标签 javascript mongodb mongoose graphql

我正在尝试使用 GraphQLMongoDB 中存储 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/

相关文章:

javascript - React Native DRY Javascript

javascript - D3.js 无法加载 json 文件

javascript - 检测或捕获 hashbang/querystring 更改为相同的 href 目标

javascript - MongoDB 和 URL 路由

javascript - 如何保护 angularjs 指令的 CSS?

java - 带种子的 MongoClient 构造函数

javascript - 为什么 Angular-fullstack 对 Express 同时使用 put 和 patch 请求?

node.js - Mongoose /keystonejs : unique field except if field is empty

javascript - MongooseJS 几乎无法使用 Keystone Location 字段

MongoDb 仅以数组的形式返回特定字段(电话号码)