【发布时间】:2023-02-18 18:33:08
【问题描述】:
我有三个type-graphql对象(和typeorm实体):Entry(字典词条)、Meaning(词条的含义)和Pronunciation(词条的转录)。
Entry 和Meaning 之间是多对多关系,Entry 和Pronunciation 之间是一对多关系。
我有 GraphQL 查询 searchEntries,它接受查询并返回匹配的 Entry 对象以及链接的 Meaning 和 Pronunciation 对象。
在我的Entry.ts 文件中,我这样定义Entry 的关系(@Field() 来自type-graphql):
@Field(() => [Pronunciation], { nullable: "itemsAndList" })
@OneToMany(() => Pronunciation, (pronunciation) => pronunciation.entry, {
nullable: true,
})
pronunciations: Relation<Pronunciation[]> | undefined;
@Field(() => [Meaning], { nullable: "itemsAndList" })
@ManyToMany(() => Meaning, (meaning) => meaning.entries, { nullable: true })
meanings: Relation<Meaning[]> | undefined;
因此,如您所见,GraphQL 应该知道 Entry 的字段 pronunciations 和 meanings 可以为空。
但是,我收到此错误(来自graphql):
Cannot return null for non-nullable field Pronunciation.id.
我注意到 pronunciations 和 meanings 的子元素仍然不可为空:
为什么 GraphQL 不推断如果父元素可以为空,那么它的子元素也可以为空?
附加信息:我正在使用 typeorm 的原始 SQL 查询获取数据,打印结果如下所示:
[
{
id: 86,
headword: 'lightning',
createdAt: 2023-02-17T07:12:27.825Z,
updatedAt: 2023-02-17T07:12:27.825Z,
meanings: [ [Object], [Object], [Object] ],
pronunciations: [ [Object], [Object], [Object] ]
}
]
(我使用 JSON_AGG(JSON_BUILD_OBJECT()) 来表示意义和发音的数组,并使用左连接来连接表格。)
任何帮助将不胜感激!
【问题讨论】:
-
更新:问题出在我使用
JSON_BUILD_OBJECT()的原始 SQL 查询中,它返回如下字段:[{ id: null, transcription: null, notes: null, userId: null },...]而不是返回空对象。寻找解决方法 -
查询
Pronunciation的id返回null;在您的架构中指定的位置不能。 -
您已发现问题 :) 发布您自己问题的答案 :)
标签: typescript postgresql graphql typeorm typegraphql