【发布时间】:2019-05-27 23:49:15
【问题描述】:
当我在我的 graphQL 架构中使用联合类型时,我通常像这样使用它:
const documentTypeDefs = gql`
union TestType = TypeExample1 | TypeExample2
type Document {
exampleKey: TestType
}
`
然后我这样解决:
TestType: {
__resolveType(obj) {
if(obj.property1) {
return 'TypeExample1';
}
if(obj.property2) {
return 'TypeExample2';
}
return null;
},
}
但有时我在解析函数中得到空对象(即obj 是{})。我认为返回 null 或 undefined 可以完成这项工作,但不幸的是我遇到了错误:
"Abstract type ItemsType must resolve to an Object type at runtime for field Document.exampleKey with value {}, received \"{}\". Either the ItemsType type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function."
那我该如何解析空对象呢? 谢谢!
【问题讨论】: