有一种方法可以使用@include(if: $withFriends)
使用gql有条件地获取内容
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)
答案 0 :(得分:1)
您可以使用@skip
伪指令,该伪指令的工作方式与@include
相反—如果if
参数为true
,则它会忽略选择集中的字段:< / p>
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
。