简体   繁体   English

GraphQL是否有一种无需明确的“…on”查询即可获取已解决的联合类型字段的方法?

[英]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.

相关问题 有没有办法将 GraphQL 查询字段分组到子/嵌套查询组中? - Is there a way to group GraphQL query fields into sub/nested query groups? Hibernate获取而无需显式事务 - Hibernate get without explicit Transaction 如果没有显式类型转换,语言是否可以“完整”? - Can a language be “complete” without explicit type casting? 有什么方法可以从实体获取字段而无需实际加入该表? - Any way to get fields from entity without actually joining into that table? 将枚举类型作为参数传递给 GraphQL 查询 - Quarkus - Passing Enum Type as Parameter to GraphQL Query - Quarkus 例程(get_data_type)无法解析 - Routine (get_data_type) can not be resolved 如何使用 Apollo graphql 动态调整查询形状(字段)? - How to dynamically adjust Query shape (fields) using Apollo graphql? 如何在 Avro Union 逻辑类型字段中为默认值指定转换器? - How to specify converter for default value in Avro Union logical type fields? 在 graphql-java 中检测查询类型(查询、突变、订阅) - Detect query type( query, mutation, subscription) in graphql-java Java 有没有办法在不知道类型的情况下获取数组的大小? - Java is there a way to get the size of an array without knowing the type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM