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

How to encode GraphQL Aliases #39

Closed davidoram closed 5 years ago

davidoram commented 5 years ago

Hi, I'm trying to figure out how to encode GraphQL aliases (https://graphql.github.io/learn/queries/#aliases) using this library.

I'm trying to encode something like this query:

{
  repository(owner: "davidoram", name: "gittest") {
    nameWithOwner
    tags: refs(refPrefix: "refs/tags/", last: 30, orderBy: {field: TAG_COMMIT_DATE, direction: DESC}) {
      edges {
        tag: node {
          name
          target {
            sha: oid
          }
        }
      }
    }
  }
}

.. in particular I haven't figured out how to encode the tags: refs alias as a go struct with the appropriate graphql struct tag?

dmitshur commented 5 years ago

@davidoram Thanks for asking. Aliases are supported, but I haven't documented them yet (see #22). I hope to fix that soon.

The answer is as follows. A GraphQL query like this:

query {
  aliasGoesHere: repository(owner: "golang", name: "go") {
    nameWithOwner
    description
  }
}

Would be implemented with the following Go query struct:

var query struct {
    AliasGoesHere struct {
        NameWithOwner string
        Description   string
    } `graphql:"aliasGoesHere: repository(owner: \"golang\", name: \"go\")"`
}

Basically, you just put the alias prefix at the beginning of the graphql tag. Let me know if that makes sense.

davidoram commented 5 years ago

Thanks @dmitshur, that worked like a charm 👍 .

If it helps anyone else - I have another example here where I retrieve the tags for a repository