【问题标题】:Using Knex and Objection with GraphQL, how to return the count()?在 GraphQL 中使用 Knex 和 Objection,如何返回 count()?
【发布时间】:2018-01-07 18:38:34
【问题描述】:

我有一个使用 Node、Knew 和 Objection 的 GraphQL 服务器。

我的模型中有以下内容:

  totalCount = async () => {
    const totalCount = await Request
      .query()
      .count();
    console.log(totalCount);
    return totalCount;
  }

以上内容应该返回一个 Int 但当前错误:"Int cannot represent non 32-bit signed integer value: [object Object]",

console.log:

[ Request { totalCount: [Function], count: 14 } ]

控制台日志中的数字 14 是正确的。我怎样才能让上面的函数返回一个带有计数的 Int?

【问题讨论】:

    标签: javascript reactjs graphql knex.js objection.js


    【解决方案1】:

    据推测,totalCount 字段在您的架构中定义为整数。正如您的日志所示,您的调用正在返回一个对象。您的整数字段解析器必须返回一个整数;如果它返回一个对象,GraphQL 不知道如何处理它并抛出该错误。

    只需从查询结果中提取计数,如下所示:

    totalCount = () => {
      return Request
        .query()
        .count()
        .then(([res]) => res && res.count)
    }
    

    【讨论】:

    • 它需要一个 res[0].count -- 这就是我搞砸的地方
    猜你喜欢
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 2018-09-24
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多