GraphQL - 有条件地执行子查询 [英] GraphQL - Execute sub query conditionally

查看:85
本文介绍了GraphQL - 有条件地执行子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试优化由我的一些在整个应用程序中共享的 React 组件(例如页脚和页眉组件)执行的查询.

I'm trying to optimise the query executed by some of my react components that are shared across the whole app, such as Footer and Header components.

当未提供变量 institutionPath 时,我试图不获取学生解决方案的详细信息.

I'm trying not to fetch the Student Solution details when the variable institutionPath is not provided.

query organisationAndInstitution($organisationName: String!, $institutionPath: String!, $fetchInstitution: Boolean!){
    organisation(where: {
      name: $organisationName
    }){
      name
    }

    studentSolutionRelationships(where:{ 
      AND: [
        {
          status: PUBLISHED
        },
        {
          studentSolution: {
            status: PUBLISHED
          }
        }
      ]
    }) @include(if: $fetchInstitution) {
      status
  }
}

为此,我添加了一个 fetchInstitution 布尔变量并添加了 @include(if: $fetchInstitution) 指令.

To do so, I added a fetchInstitution boolean variable and added the @include(if: $fetchInstitution) directive.

但是指令似乎只适用于字段,而不适用于整个查询.所以我想知道我想做的事情是否可行,因为我写的方式是无效的.

But directives seem to apply only on fields, not on whole queries. So I wonder if what I want to do is possible, because the way I wrote it is invalid.

推荐答案

GraphQL 文档中的任何字段都可以使用 @include 指令显式include或显式> 排除 使用 @skip 指令.该指令应在字段名称和参数之后提供,但在字段的选择集之前(如果有),如您的问题所示:

Any field in a GraphQL document can be explicitly included using the @include directive or explicitly excluded using the @skip directive. The directive should be provided after the field name and arguments, but before the field's selection set, if it has one, as shown in your question:

studentSolutionRelationships(where:{ 
  #...input fields omitted for brevity
}) @include(if: $fetchInstitution) {
  status
}

指令接受一个参数 (if),它必须是一个 Boolean 值.该值可以是文字(即 truefalse)或 Boolean 类型的变量.GraphQL 不提供评估表达式的方法——任何条件逻辑都必须驻留在客户端代码中,并用于确定传递给 if 参数的变量的值.

The directive takes a single argument (if) which must be a Boolean value. This value may be a literal (i.e. true or false) or a variable of the Boolean type. GraphQL does not provide a way to evaluate expressions -- any conditional logic has to be reside in the client code and be used to determine the value of the variable passed to the if argument.

指令可以应用于文档中的任何字段,包括问题中的studentSolutionRelationshipsorganisation等根级字段.事实上,您可以使用这些指令排除所有根字段——请记住,在这种情况下,查询仍将运行并只返回一个空对象.

The directives may be applied to any field in a document, including root-level ones like studentSolutionRelationships and organisation in the question. In fact, you can exclude all root fields using these directives -- just keep in mind that in such a scenario, the query will still run and just return an empty object.

换句话说,您在这里的方法是正确的.如果查询失败,则是因为一个无关的问题.

In other words, your approach here is correct. If the query is failing, it's because of an unrelated issue.

这篇关于GraphQL - 有条件地执行子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆