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

查看:16
本文介绍了在“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)

organizationlogin 的值是 "nasa"null

the value for login under organization is either "nasa" or null

我想弄清楚是否可以对登录名/组织使用@skip,以便仅显示作为 nasa 组织成员的 repo 贡献者.我相信对于这个特定的查询,你可以用另一种方式来做,但这只是一个例子.

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(if 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 则不包括该可提及的用户.在mentionableUser 上没有搜索字段选项.从我的阅读中,我猜想这样做的唯一方法是修改 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.

假设我们有一个查询:

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 只是一种在客户端保持 DRY 的便捷方式.它们不能用于过滤服务器返回的结果.

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天全站免登陆