在"null"上使用@skip使用graphql的字段 [英] Use @skip on "null" fields using graphql

查看:51
本文介绍了在"null"上使用@skip使用graphql的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下GQL

query getMembers {
  repository(owner: "nasa", name: "cumulus") {
    mentionableUsers(first: 100) {
      nodes {
        login
        organization(login: "nasa") {
          login
        }
      }
    }
  }
}

(针对GitHub v4 GraphQL的查询)

(Query against GitHub v4 GraphQL)

organization 下的 login 的值为"nasa" null

我正在尝试确定是否可以对登录/组织使用@skip,以便仅显示作为国家航空航天局组织成员的回购参与者.我相信对于这个特定的查询,您可以使用另一种方式来做,但这只是一个例子.

I am trying to figure out if it's possible to use @skip against the login/organization so that only contributors to the repo, who are members of the nasa org are shown. I believe for this particular query you can do it another way, but this is just an example.

如何将@ skip/@ include与非布尔值一起使用.关于此方面的文档很少.尽管我可以在客户端应用程序中过滤响应JSON,但接收通过网络发送的数据较少然后在应用程序中进行解析会更有效.

How would you use @skip/@include with a non boolean. There is minimal documentation on this. While I could filter the response JSON in my client side app, it would be more efficient to receive less data sent over the network and then to parse in my app.

在GraphQLi中玩游戏时,遇到这种尝试各种方式的错误-也许只有在字段本身返回布尔值的情况下才有可能?

Playing in GraphQLi I received errors trying this various ways - maybe its only possible if the field returns a boolean itself?

例如,我无法执行 login @skip(如果login == null).我还尝试将变量部分中的值设置为null并在查询中引用它,但是我尝试的所有变体都没有.

e.g., I couldn't do login @skip(if login==null). I also tried setting a value to null in the variables section and the referencing it in the query, but none of the variations I tried work.

如果子字段是某个值,我真正想做的就是不包括父字段.例如,如果login = null,则不包括该可提及的用户.sayableUser上没有搜索字段选项.从我的阅读中,我猜想唯一的方法就是修改API,以便在提到提到的用户上放置一个搜索或过滤器字段,否则我需要对我的客户进行此操作吗?

What I would really like to do is not include the parent if the child field is some value. e.g., if login=null then don't include that mentionable user. There is no search field option on mentionableUser. From my reading, I am guessing that the only way to do this would be if the API was modified to put a search or filter field on the mentionalbeUsers, otherwise I would need to do this with my client?

推荐答案

点对.

@skip @include 指令均提供相同的功能-允许客户端任意选择是否在请求中包括字段.

Both the @skip and @include directives provide the same functionality -- allowing the client to arbitrarily chose whether a field should be included in the request.

假设我们有一个查询,例如:

Let's say we have a query like:

query ($skipBar: Boolean!) {
  foo
  bar @skip(if: $skipBar)
}

如果将 skipBar 设置为 true ,则实际上是在发送以下查询:

If I set skipBar to true, I am effectively just sending this query:

query {
  foo
}

如果将其设置为 false ,则实际上是在发送以下查询:

If I set it to false, I am effectively just sending this query:

query {
  foo
  bar
}

作为客户端,我的逻辑必须确定要分配给 skipBar 的值,但是我可以轻松地使用相同的逻辑来决定在发送这两个查询之一之间进行决定.换句话说,就像变量和片段一样,@ skip和@include只是将内容保持在客户端的简便方法.它们不能用于过滤服务器返回的结果.

As a client, my logic has to determine the value to assign to skipBar, but I could just as easily use that same logic to decide between sending one of those two queries. In other words, like variables and fragments, @skip and @include are simply a convenient way to keep things DRY on the client-side. They cannot be used to filter the result returned by the server.

GraphQL语法不支持表达式(或为此,对响应部分的任何形式的引用).此外, @skip @include 仅采用单个参数( if ),并且必须向该参数传递 布尔值-既可以作为变量也可以作为文字值.但是,即使您可以某种方式将表达式传递给 if 参数,指令也会确定该字段是否包含在请求句点中.因此,如果被跳过的字段是返回的列表(如节点列表)的一部分,则每个节点在被跳过时都将不存在.

GraphQL syntax does not support expressions (or for that matter, any sort of references to parts of the response). Additionally, @skip and @include only take a single argument (if) and that argument must be passed a Boolean -- either as a variable or as a literal value. Even if you could somehow pass an expression to the if argument, though, the directives determine whether the field is included in the request, period. So if the skipped field is part of a returned List (like a List of nodes), it will be absent from every node when it's skipped.

那么,有什么解决方法吗?

不是真的:(正如您已经猜到的,如果GitHub API没有提供过滤字段的方法,那么作为客户端您可以做的事情不多-您必须应用过滤逻辑客户端-侧.

Not really :( As you've already guessed, if the GitHub API doesn't provide a way to filter a field, there's not much you can do as a client -- you'll have to apply the filtering logic client-side.

这篇关于在"null"上使用@skip使用graphql的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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