【问题标题】:"else" with GraphQL使用 GraphQL 的“其他”
【发布时间】:2019-10-28 17:16:05
【问题描述】:

如何使用 GQL 指令实现“else”效果

有一种方法可以通过gql 使用@include(if: $withFriends) 有条件地获取某些内容

query Hero($episode: Episode, $withFriends: Boolean!) {
  hero(episode: $episode) {
    name
    friends @include(if: $withFriends) {
      name
    }
  }
}

但如果$withFriends 为假,我想获取其他内容。我可以通过传递额外的变量$notWithFriends来实现它

query Hero($episode: Episode, $withFriends: Boolean!) {
  hero(episode: $episode) {
    name
    friends @include(if: $withFriends) {
      name
    }
    appearsIn @include(if: $notWithFriends)
  }
}

问题:是否可以避免使用附加变量?

类似这样的:@include(else: $withFriends)@include(ifNot: $withFriends)@include(if: !$withFriends)

【问题讨论】:

    标签: graphql apollo relay


    【解决方案1】:

    您可以使用@skip 指令,它的作用与@include 的相反——如果if 参数是true,它会省略选择集中的字段:

    query Hero($episode: Episode, $withFriends: Boolean!) {
      hero(episode: $episode) {
        name
        friends @include(if: $withFriends) {
          name
        }
        appearsIn @skip(if: $withFriends)
      }
    }
    

    这样,如果$withFriendstrue,则只会选择namefriends。如果是false,只会选择nameappearsIn

    【讨论】:

      猜你喜欢
      • 2020-01-15
      • 2020-12-03
      • 2020-12-14
      • 2020-11-26
      • 2019-12-10
      • 2023-03-26
      • 2020-08-02
      • 2020-11-19
      • 2018-09-21
      相关资源
      最近更新 更多