如何使用 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)
}
}
这样,如果$withFriends
为true
,则只会选择name
和friends
。如果为false
,则只有name
和appearsIn
会被选中。
关于graphql - "else"与 GraphQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56591874/