我在类型中具有可空属性:
type Person {
job: String
}
和一个查询:
query Persons() {
persons() {
job
}
}
查询对象时,总是得到带有字符串或空值的job
键。
{
"job": null
}
但是我需要的是job
键仅在定义作业时才出现在结果中,否则我希望完全跳过它。
{}
我尝试返回null
,undefined
并从数据中跳过密钥,但是Apollo始终返回密钥,无论如何。
答案 0 :(得分:1)
随后删除字段要容易得多:
if (result.job === null) {
delete result.job;
}
要使用graphql获得相同的结果,您必须将Person
分为两种类型:PersonWithJob
和PersonWithoutJob
。从persons
查询返回一个并集,并将查询更改为:
query Persons() {
persons() {
... on PersonWithJob {
job
}
}
}