Graphql 包含指令

标签 graphql

我有一个查询将状态作为输入变量,它是( 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/

相关文章:

validation - 使用解析器时如何在graphql-spring-boot中引发多个验证错误?

node.js - 在 GraphQL 中处理 Mongoose 填充字段

reactjs - Apollo 客户端未配对 UserInputError

reactjs - 尝试将 React Apollo Query 组件用于带有 typescript 的 GraphQL 查询导致错误

graphql - Apollo 更新查询未调用?

html - 从 Kickstarter 项目中抓取文本不会返回任何结果

django - 默认解析器的身份验证/授权

javascript - 如何从生成的模式中获取简单的 graphql 突变查询?

java - GraphQL 实现 Java

graphql - GraphQL 错误中的 'locations' 指的是什么?