【问题标题】:Conditional where condition in GraphQL QueryGraphQL 查询中的条件 where 条件
【发布时间】:2023-01-25 13:31:41
【问题描述】:

我有以下查询:

query MyExampleQuery($templateQuestionId: Int!, $assetId: Int) {
  report_answer(
    where: { 
      report_template_question: { id: { _eq: $templateQuestionId } }, 
      asset_id: { _eq: $assetId } }
  ) {
    id
  }
}

我希望 where 的 asset_id 条件仅有条件地适用。当提供 $assetId 时,它应该是一个条件,否则它根本不适用。

这如何通过 GraphQL 实现?

【问题讨论】:

    标签: javascript typescript graphql apollo


    【解决方案1】:

    是的,但不是您上面描述的方式。

    你有:

    query MyExampleQuery($templateQuestionId: Int!, $assetId: Int)
    

    其中表示 $templateQuestionId 是必需的,但 $assetId 是可选的。

    当你写你的解析器对于此查询,您可以在执行实际数据库查询时考虑 assetId 的存在(或不存在)。

    但是 GraphQL 语言本身并没有 where 子句的概念。某些 GraphQL API 在其输入对象中包含 where,但它不是语言的一部分。

    【讨论】:

      猜你喜欢
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 2014-10-04
      • 1970-01-01
      相关资源
      最近更新 更多