【发布时间】:2020-12-02 17:50:28
【问题描述】:
我正在运行一个带有 sequelize as orm 的 apollo 服务器到一个 postgres 数据库。这是架构:
type Tag {
id: ID!
name: String!
}
type Service {
id: ID!
name: String!
slug: String!
tags: [Tag!]!
}
解析器:
findServicesByTag: async(_, { tag }, { models }) => {
const res = await models.Service.findAll({
where: {
'$Tags.name$': tag
}
,include: [
{
model: models.Tag,
as: 'Tags'
}
]
})
console.log(res)
return res
}
但是当执行这个查询时
query {
findServicesByTag(tag: "restaurant")
{
id name slug
tags {
name
}
}
}
我收到消息“不能为不可为空的字段 Service.id 返回 null。”
console.log 指令打印这些数据:
[ Service {
dataValues: { id: 1, name: 'Restaurant X', Tags: [Array] },
_previousDataValues: { id: 1, name: 'Restaurant X', Tags: [Array] },
_changed: Set {},
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
include: [Array],
includeNames: [Array],
includeMap: [Object],
includeValidated: true,
attributes: [Array],
raw: true },
isNewRecord: false,
Tags: [ [Tag] ] },
Service {
dataValues: { id: 2, name: 'Restaurant Y', Tags: [Array] },
_previousDataValues: { id: 2, name: 'Restaurant Y', Tags: [Array] },
_changed: Set {},
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
include: [Array],
includeNames: [Array],
includeMap: [Object],
includeValidated: true,
attributes: [Array],
raw: true },
isNewRecord: false,
Tags: [ [Tag] ] } ]
好像 apollo 无法处理这些数据,它不会查询后续的标签实体。
【问题讨论】:
-
console.log(res)得到了什么?您的service数据是否有有效的id? -
@slideshowp2 添加了 console.log 输出
标签: javascript node.js graphql sequelize.js apollo-server