我正在尝试使用带有属性的 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/