Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Graphql GQL方案为同一密钥接受多个数据结构_Graphql_Apollo_Apollo Client_Gql_Gqlquery - Fatal编程技术网

Graphql GQL方案为同一密钥接受多个数据结构

Graphql GQL方案为同一密钥接受多个数据结构,graphql,apollo,apollo-client,gql,gqlquery,Graphql,Apollo,Apollo Client,Gql,Gqlquery,我目前正在我的应用程序中使用GQL模块 在下面的数据结构中,内容将具有对象或数组 var A = { content: { text: "Hello" } } var B = { content: { banner: [{ text: "Hello" }] } } 如何使内容接受动态模式 下面是我累了,但没有工作。请帮忙 type body { content: TextContent | [Banner] } type Banner

我目前正在我的应用程序中使用
GQL模块

在下面的数据结构中,
内容
将具有
对象
数组

var A = {
  content: {
    text: "Hello"
  }
}

var B = {
  content: {
    banner: [{
      text: "Hello"
    }]
  }
}
如何使
内容
接受动态模式

下面是我累了,但没有工作。请帮忙

type body {
 content: TextContent | [Banner]
}

type Banner {
  text: TextContent
}

type TextContent {
 text: String
}

GraphQL要求字段总是解析为单个值或列表——它不能解析为任何一个值或列表。但是,字段可以在运行时使用抽象类型(联合或接口)返回不同的类型。因此,您可以按如下方式重新构造模式:

type Body {
  content: Content
}

union Content = TextContent | BannerContent

type TextContent {
  text: String
}

type BannerContent {
  banners: [Banner]
}
然后使用片段查询
内容

query {
  someField {
    body {
      content: {
        ...on TextContent {
          text
        }
        ...on BannerContent {
          banners
        }

      }
    }
  }
}