graphql - 无法将自定义结果从解析器传递给 Graphql

标签 graphql sequelize.js apollo

我正在尝试使用带有属性的 sequelize 获取数据并将其传递给 graphql。
结果在控制台中很好,但 graphql 查询为属性字段返回 null。
我的解析器

    getUnpayedLessons: async (_, args, { models }) => {
  const { Attendance, Student } = models;
  return await Attendance.findAll({
    include: {
      model: Student,
    },
    where: {
      fk_lessonsSerieId: { [Op.is]: null },
    },
    attributes: ["id", [sequelize.fn("count", sequelize.col("absenceFlag")), "unpayedLessons"]],
    group: ["student.id"],
  });
},
询问
getUnpayedLessons {
  id
  unpayedLessons
  student {
    id
    firstName
    lastName
  }
}
模式
type UnpayedLessons {
  id: Int
  unpayedLessons: Int
  student: Student
  }

extend type Query {
  getUnpayedLessons: [UnpayedLessons]
  }
这是我运行查询时解析器的 console.log
    [
   attendance {
    dataValues: { id: 2, unpayedLessons: 8, student: [student] },
    _previousDataValues: { id: 2, unpayedLessons: 8, student: [student] },
    _changed: Set {},
    _options: {
      isNewRecord: false,
     _schema: null,
     _schemaDelimiter: '',
      include: [Array],
      includeNames: [Array],
      includeMap: [Object],
      includeValidated: true,
      attributes: [Array],
      raw: true
     },
]
和来自graphql
    {
  "data": {
    "getUnpayedLessons": [
      {
        "id": 2,
        "unpayedLessons": null,
        "student": {
          "id": 2,
          "__typename": "Student"
        },
        "__typename": "UnpayedLessons"
      },
     ]
    }
   }
知道如何将 unpayedLessons 传递给 graphql 吗?

最佳答案

要调试它,您需要检查从 DB 返回的内容,形状:

const values = await Attendance.findAll({...
console.log( values );
// adapt structure to match query requirements
// finally return
return values;

关于graphql - 无法将自定义结果从解析器传递给 Graphql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63978487/

相关文章:

javascript - VSCode 不会对模板字符串内的内容进行样式设置

amazon-web-services - 具有业务逻辑的 AWS Appsync + DynamoDB

sequelize.js - Sequelize 与一个表的关联 "hasMany"其他表

jms - ActiveMQ、Apollo、Kafka

javascript - Graphql 创建 2 个具有 1 个突变的条目

reactjs - "No Apollo Client instance can be found"

javascript - React-Apollo,不要在组件加载时运行查询

node.js - 可以对 type-graphql 中的结果列表元素进行计数

node.js - Sequelize reload() 清除所有模型数据

node.js - Sequelize include 不适用于关联表