Skip to content

Instantly share code, notes, and snippets.

@jamesluberda
Created June 20, 2018 01:07
Show Gist options
  • Save jamesluberda/d73376298e22e3fc4abbca590b97d5e0 to your computer and use it in GitHub Desktop.
Save jamesluberda/d73376298e22e3fc4abbca590b97d5e0 to your computer and use it in GitHub Desktop.
Simple User Gists Query Using GitHub GraphQL API (v4 API)

I couldn't find any examples of gists queries using GraphQL--most GraphQL examples seem to focus on traditional repositories--so here is one. As a preface, I cannot recommend strongly enough, at least when getting started, developing queries using the GitHub GraphQL Explorer. I initially started by issuing queries via curl, constructing them using the docs available on the site and a downloaded copy of the schema. Unfortunately, I ended up with errors that I couldn't quite parse. I knew, for example, from the schema, the possible field values for ordering gists. However, whenever I tried to use one of those values, the API returned that it was invalid, like so:

{"data":null,"errors":[{"message":"Argument 'orderBy' on Field 'gists' has an invalid value. Expected type 'GistOrder'.","locations":[{"line":1,"column":48}]}]}"

When I finally turned to the Explorer, I discovered that not only was the value I was using correct (field: CREATED_AT), thanks to its autocomplete/prompting feature, but it also helpfully suggested that I put a direction: in, and suggested values. As it turns out, and I might have either supposed it, or gleaned it from enough study of the schema, direction must be specified along with field. There is no default.

Following is the query I ended up with, thanks to some prompting by the Explorer.

# simple user gists query for current auth
# can substitute "user (login: <username>)"
# for "viewer"

query { 
  viewer { 
    gists (first: 100, orderBy: {field: CREATED_AT, direction: DESC} ) {
      edges {
        node {
          createdAt
          description
          name
          pushedAt
          stargazers (first: 100) {
            totalCount
            edges {
              node {
                id
              }
            }
          }
          updatedAt
        }
      }
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment