[英]GraphQL is there a way to get resolved Union type fields without explicit “…on” query?
I have the following Union defined in my schema file: 我在架构文件中定义了以下联合:
union ContentUnion = Content | Setting | Page | Picture
and for example this type defintion for Setting 例如用于设置的此类型定义
type Setting {
id: String!
doctype: String!
name: String!
}
I also have a working Type resolver for ContentUnion . 我还有一个适用于ContentUnion的类型解析器。
Using this query 使用此查询
{
content(id: "113804"){
... on Setting{
id
}
}
}
I am able to retrieve a value. 我能够检索一个值。 But this feels weird for me because I do not understand why I have to explicitly tell that the Type is Setting .
但这对我来说很奇怪,因为我不明白为什么我必须明确地告诉我Type是Setting 。 I thought that is what the TypeResolver is actually for?
我以为那是TypeResolver真正的目的?
I would like to simply use this query 我只想使用此查询
{
content(id: "113804"){
id
}
}
But the result therefore is: 但是结果是:
{
"data": null,
"errors": [
{
"message": "Validation error of type FieldUndefined: Field 'id' in type 'ContentUnion' is undefined @ 'content/id'",
"locations": [
{
"line": 1,
"column": 26
}
],
"description": "Field 'id' in type 'ContentUnion' is undefined",
"validationErrorType": "FieldUndefined",
"queryPath": [
"content",
"id"
],
"errorType": "ValidationError",
"path": null,
"extensions": null
}
],
"extensions": null,
"dataPresent": false
}
That is because the schema was not able to locate an id field on the ContentUnion union type since it is an union. 这是因为由于架构是联合,因此无法在ContentUnion联合类型上找到id字段。 I wonder if there is a way to get this to work maybe?
我想知道是否有办法使它起作用? Because it would remove a lot of headache for me in my implementation.
因为这将为我的实施消除很多麻烦。
I'm sorry you have to use the query with typed fragments. 抱歉,您必须对带类型的片段使用查询。
Per unions in the GraphQL specification , GraphQL Unions represent an object that could be one of a list of GraphQL Object types, but provides for no guaranteed fields between those types. 对于GraphQL规范中的每个联合 ,GraphQL联合表示一个对象,该对象可以是GraphQL对象类型的列表之一,但在这些类型之间不提供任何保证的字段。 No fields may be queried on union without the use of typed fragments.
如果不使用类型化的片段,则不能在联合上查询任何字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.