如何解决无法将自定义结果从解析器传递到Graphql
我正在尝试使用具有属性的续集来获取数据,并将其传递给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,_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"
},]
}
}
您知道我如何将未支付的课程传递给graphql吗?
解决方法
要调试此功能,您需要检查DB返回的形状:
const values = await Attendance.findAll({...
console.log( values );
// adapt structure to match query requirements
// finally return
return values;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。