Get all my Github repos with GraphQL


Github’s latest (v4) API uses GraphQL, which is quite powerful but rather under-documented.

I needed to get a list of all of my repositories, but I have more than 100 and needed to do paging to get them all. I didn’t like parsing the HTTP headers, which is how the v3 API does paging, so I figured out how to get them with v4/GraphQL.

query MyRepos($after: String) {
  viewer {
    login
    name
    repositories(first: 100, affiliations: [OWNER, ORGANIZATION_MEMBER, COLLABORATOR], ownerAffiliations: [OWNER, ORGANIZATION_MEMBER, COLLABORATOR], orderBy: {field: NAME, direction: ASC}, isFork: false, after: $after) {
      totalCount
      pageInfo {
        endCursor
        hasNextPage
      }
      totalDiskUsage
      edges {
        cursor
        node {
          nameWithOwner
          isFork
          updatedAt
          diskUsage
        }
      }
    }
  }
}

You need to pass in after as a GraphQL variable. Pass null the first time, then the value from .data.viewer.repositories.pageInfo.endCursor.

A full bash script is available in bin/myrepos.sh.

Useful links:

Tags:  git

Share: