我有一个查询将状态作为输入变量,它是( future 、过去、全部)的枚举。
我只想在状态为 future 时返回一些字段。我试过使用 @include,但它似乎只接受一个 bool 值。
有什么方法可以让它工作吗:
aField @include(if: $status == future)
最佳答案
我认为您甚至不能将 bool 字段与 @include
一起使用 - 仅限文字/查询变量,因此表达式也是不行的。
如果它真的很重要,并且您能够修改 API/架构,您可以(滥用)使用接口(interface)功能 - 将 future 的实体解析为一种类型,将过去的实体解析为另一种类型,然后您可以使用片段来选择基于它的字段:
interface Competition {
id: ID!
name: String!
status: Status!
something: String
}
type FutureCompetition implements Competition {
// same fields
}
type PastCompetition implements Competition {
// same fields
}
然后你可以这样查询:
competitions {
id
name
status
... on FutureCompetition {
something
}
}
假设您可以按状态查询,一个可能更容易做的事情是简单地在一个查询中执行两个查询并在客户端合并结果:
pastCompetitions: competitions(withStatus: PAST) {
id
name
status
}
futureCompetitions: competitions(withStatus: FUTURE) {
id
name
status
something
}
关于Graphql 包含指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47432755/