繁体   English   中英

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

[英]GraphQL is there a way to get resolved Union type fields without explicit “…on” query?

我在架构文件中定义了以下联合:

union ContentUnion = Content | Setting | Page | Picture

例如用于设置的此类型定义

type Setting {
    id: String!
    doctype: String!
    name: String!
}

我还有一个适用于ContentUnion的类型解析器。

使用此查询

{ 
 content(id: "113804"){
  ... on Setting{
      id
  }
 }
}

我能够检索一个值。 但这对我来说很奇怪,因为我不明白为什么我必须明确地告诉我Type是Setting 我以为那是TypeResolver真正的目的?

我只想使用此查询

  { 
     content(id: "113804"){
      id
    }
  }

但是结果是:

{
  "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
}

这是因为由于架构是联合,因此无法在ContentUnion联合类型上找到id字段。 我想知道是否有办法使它起作用? 因为这将为我的实施消除很多麻烦。

抱歉,您必须对带类型的片段使用查询。

对于GraphQL规范中的每个联合 ,GraphQL联合表示一个对象,该对象可以是GraphQL对象类型的列表之一,但在这些类型之间不提供任何保证的字段。 如果不使用类型化的片段,则不能在联合上查询任何字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM