我正在构建一个 GraphQL API。返回联合/接口类型字段时,是否可以让服务器告诉我对象类型?即这样的东西
{
search(text: "an") {
... on Human {
__type
name
height
}
... on Droid {
__type
name
primaryFunction
}
... on Starship {
name
length
}
}
}
// or even better
{
search(text: "an") {
__type // <--- even though it's a Union query, everything has a type right? :/
... on Human {
name
height
}
... on Droid {
name
primaryFunction
}
... on Starship {
name
length
}
}
}
这将返回
{
"data": {
"search": [
{
"__type": "Human",
"name": "Han Solo",
"height": 1.8
},
{
"__type": "Human",
"name": "Leia Organa",
"height": 1.5
},
{
"name": "TIE Advanced x1",
"length": 9.2
}
]
}
}
显然,我可以根据需要通过向对象添加“类型”字段来手动添加此功能,但我想像这样的东西已经内置到 GraphQL 中了?看到所有的对象都已经有了名字。基本上,我试图将内省信息作为正常查询的一部分来访问。