Use @skip on “null” fields using graphql

无人久伴 提交于 2020-12-26 11:15:39

问题


Given the following GQL

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

(Query against GitHub v4 GraphQL)

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

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.

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.

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

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.

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?


回答1:


Couple of points.

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)
}

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

query {
  foo
}

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

query {
  foo
  bar
}

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 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.

So, is there a workaround?

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.



来源:https://stackoverflow.com/questions/55660365/use-skip-on-null-fields-using-graphql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!