shurcooL / githubv4

Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://docs.github.com/en/graphql).
MIT License
1.11k stars 89 forks source link

Query Interfaces and unions #21

Closed ereyes01 closed 6 years ago

ereyes01 commented 6 years ago

Thanks for taking a crack at the client library...

How would you query a specific type when the schema specifies an interface or union as the return type?

For instance, say I try the following query to github:

query {
  search(first: 100, type: ISSUE, query: \"user:ereyes01 repo:firebase label:enhancement state:open\") {
    nodes {
      ... on Issue {
        number
      }
    }
  }
}

The search query returns a SearchResultItem, and in this case, I just want the issues. Is there a way using your library to specify the .. on Issue part of the query above?

dmitshur commented 6 years ago

Hi, yes, GraphQL inline fragments and unions are supported (see #10 for more information), but not documented in README yet. I plan to do that, and tracking it in https://github.com/shurcooL/graphql/issues/3 issue.

The GraphQL query you posted can be expressed something like this:

var query struct {
    Search struct {
        Nodes []struct {
            Issue struct {
                Number int
            } `graphql:"... on Issue"`
        }
    } `graphql:"search(first: 100, type: ISSUE, query: $searchQuery)"`
}
ereyes01 commented 6 years ago

Awesome, and works great. Thanks!