graphql - "else"与 GraphQL

标签 graphql apollo relay

如何使用 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 )

最佳答案

您可以使用 @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会被选中。

关于graphql - "else"与 GraphQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56591874/

相关文章:

centos - Sendmail 中继问题

node.js - Apollo服务器网络错误: unable to reach server

mysql - GraphQL : must be Output Type but got: undefined Error

graphql - 当 Playground 被禁用时, Apollo 服务器返回缺少的 GET 查询

stomp - Apollo 可以根据消费者协议(protocol)转换消息吗?

reactjs - 如何同步Redux和Relay?

node.js - GraphQL 错误 : Must provide name

node.js - 必须使用值调用 loader.load() 函数,但得到 : undefined

graphql - 在 apollo-client 的 useQuery 中使用 fetchPolicy ='cache-and-network' 不在 fetch 上更新缓存更多

null - 如何理解 GraphQL 中的空边和节点