【问题标题】:Graphql include directiveGraphql 包含指令
【发布时间】:2018-05-06 01:50:58
【问题描述】:

我有一个查询,它将状态作为输入变量,它是(未来、过去、全部)的枚举。

我只想在状态为未来时返回一些字段。我试过使用@include,但它似乎只接受一个布尔值。

有没有办法让它工作:

aField @include(if: $status == future)

【问题讨论】:

  • 不,在标准 GraphQL 中没有这样的东西。也许你可以通过两个查询来实现它?
  • @xs0 你在删除的答案中过滤是什么意思?
  • 更新了我的答案..这些有帮助吗?

标签: graphql


【解决方案1】:

我认为你甚至不能使用带有 @include 的布尔字段 - 仅限文字/查询变量,所以表达式也是不行的。

如果它真的很重要,并且您能够修改 API/架构,则可以(ab)使用接口功能 - 让未来的实体解析为一种类型,将过去的实体解析为另一种类型,那么您可以使用片段来选择基于它的字段:

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
}

【讨论】:

    【解决方案2】:

    variables 是一个 JSON。所以,你也可以尝试传递status : inputStatus==='FUTURE' ? true : false

    【讨论】:

      猜你喜欢
      • 2021-02-07
      • 1970-01-01
      • 2013-02-20
      • 2020-08-28
      • 1970-01-01
      • 2019-03-27
      • 2015-04-01
      • 2014-01-23
      • 2012-09-19
      相关资源
      最近更新 更多