node.js - 聚合函数返回 null GraphQL

标签 node.js express graphql

我正在使用 Sequelize 中的计数来测试基本聚合函数,这是我的类型计数:

type Creserve {
    id: ID!
    rDateStart: Date!
    rDateEnd: Date!
    grade: Int!
    section: String!
    currentStatus: String!
    user: User! 
    cartlab: Cartlab! 
}
type Counts {
    section: String!
    count: Int
}
type Query {
    getBooking(id: ID!): Creserve!
    allBookings: [Creserve]
    getBookingByUser(userId: ID): Creserve
    upcomingBookings: [Creserve]  
    countBookings: [Counts]
}

我使用 countBookings 作为聚合函数的查询,这是我的查询解析器:

countBookings: async (parent, args, {models}) => 
    {
      const res = await models.Creserve.findAndCountAll({
        group: 'section',
        attributes: ['section', [Sequelize.fn('COUNT', 'section'), 'count']]
      });
        return res.rows;
  },

它输出的查询是这样的:

Executing (default): SELECT "section", COUNT('section') AS "count" FROM "Creserve" AS "Creserve" GROUP BY "section";

并在我的 psql shell 中尝试了这个查询,它工作正常:

 section | count
---------+-------
 A       |     2
 R       |     2

但是,当我尝试在 GraphQL Playground 中查询 countBookings 时,返回了部分,但没有返回计数:

    {
  "data": {
    "countBookings": [
      {
        "section": "A",
        "count": null
      },
      {
        "section": "R",
        "count": null
      }
    ]
  }
}

我是不是漏掉了什么?或者这是一个错误?这是我在这个例子中尝试遵循的答案:https://stackoverflow.com/a/45586121/9760036

非常感谢!

编辑:返回 console.log(res.rows) 输出如下所示:

   [ Creserve {
    dataValues: { section: 'A', count: '2' },
    _previousDataValues: { section: 'A', count: '2' },
    _changed: {},
    _modelOptions:
     { timestamps: true,
       validate: {},
       freezeTableName: true,
       underscored: false,
       underscoredAll: false,
       paranoid: false,
       rejectOnEmpty: false,
       whereCollection: null,
       schema: null,
       schemaDelimiter: '',
       defaultScope: {},
       scopes: [],
       indexes: [],
       name: [Object],
       omitNull: false,
       hooks: [Object],
       sequelize: [Sequelize],
       uniqueKeys: {} },
    _options:
     { isNewRecord: false,
       _schema: null,
       _schemaDelimiter: '',
       raw: true,
       attributes: [Array] },
    __eagerlyLoadedAssociations: [],
    isNewRecord: false },
  Creserve {
    dataValues: { section: 'R', count: '2' },
    _previousDataValues: { section: 'R', count: '2' },
    _changed: {},
    _modelOptions:
     { timestamps: true,
       validate: {},
       freezeTableName: true,
       underscored: false,
       underscoredAll: false,
       paranoid: false,
       rejectOnEmpty: false,
       whereCollection: null,
       schema: null,
       schemaDelimiter: '',
       defaultScope: {},
       scopes: [],
       indexes: [],
       name: [Object],
       omitNull: false,
       hooks: [Object],
       sequelize: [Sequelize],
       uniqueKeys: {} },
    _options:
     { isNewRecord: false,
       _schema: null,
       _schemaDelimiter: '',
       raw: true,
       attributes: [Array] },
    __eagerlyLoadedAssociations: [],
    isNewRecord: false } ]

这是 res.count:

Executing (default): SELECT "section", COUNT('section') AS "count" FROM "Creserve" AS "Creserve" GROUP BY "section";
[ { count: '2' }, { count: '2' } ]

最佳答案

问题

实际上,您正在此处执行所有操作...但是这里发生的情况是 sequlize 不返回普通对象...它始终以 instance< 形式返回数据 像这样

[ Creserve {
    dataValues: { section: 'A', count: '2' },
    _previousDataValues: { section: 'A', count: '2' },
    _changed: {},
    _modelOptions:
     { timestamps: true,

解决方案

I am not sure but there is no other way instead of looping and makes response to json object...

const array = []
res.rows.map((data) => {
  array.push(data.toJSON())
})
return array

关于node.js - 聚合函数返回 null GraphQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50377419/

相关文章:

javascript - MySQL 查询返回一个字符串作为 JSON_OBJECT() 的结果

node.js - 为什么不鼓励使用 util.inherits()?

javascript - 如何在某些文件中使用这个node.js模块

node.js - TypeError : req. session.destroy 不是函数 - Express.js

javascript - 使用 moment 将日期发送到 graphql

javascript - AS/400数据队列可以访问node-jt400吗? (javascript)

node.js - Ubuntu 18,PM2 和 NGinx 冲突(使用中的错误地址)

node.js - Express.static() 不起作用

javascript - 架构必须包含唯一的命名类型,但包含多个名为 "Page"的类型

reactjs - 使用 GraphQL 在 NextJS 中呈现比之前呈现更多的钩子(Hook)