Search code examples
graphqlgithub-api

How to get release count and release download count from Github GraphQL?


I can get a list of releases (with details, such as download count) using this abridged graph:

query {
  repository(owner: "UserName", name: "RepoName") {
    releases(last: 100, orderBy: { field: CREATED_AT, direction: DESC}) {
      nodes {
        releaseAssets(last: 4) {
          nodes {
            downloadCount
          }
        }
      }
    }
  }
}

The issue being that I have a limit in the pagination (100 entries).

Is there any way correctly get the total download count of the repo, maybe with number of releases without bumping into issues with the pagination limits?


Solution

  • There isn't a field that sums downloadCount by release, or across releases, so the only option is to walk the graph and add them up yourself.

    Release count is easier, as repository.releases has a totalCount, so you can get the total number of releases from that, and at least see whether pagination is a limit (where that totalCount is over 100). Pagination isn't so bad, here's an earlier answer I provided on that topic.

    For a repo that has enough releases to need pagination. e.g. this query:

    query{
      repository(owner: "microsoft", name: "typescript") {
        releases(first: 100, orderBy: {field: CREATED_AT, direction: DESC}) {
          nodes {
            releaseAssets(first: 10) {
              nodes {
                downloadCount
                name
              }
              totalCount
            }
            name
          }
          totalCount
          pageInfo {
            endCursor
            hasNextPage
          }
        }
      }
    }
    

    ends with this response fragment:

            "totalCount": 146,
            "pageInfo": {
              "endCursor": "Y3Vyc29yOnYyOpK5MjAxOC0wMi0xMlQyMTo0MDo1NyswMDowMM4Ak_Ha",
              "hasNextPage": true
            }
    

    So there's another page with 46 more nodes on it, which you can get at with:

    query{
      repository(owner: "microsoft", name: "typescript") {
        releases(first: 100, after: "Y3Vyc29yOnYyOpK5MjAxOC0wMi0xMlQyMTo0MDo1NyswMDowMM4Ak_Ha", orderBy: {field: CREATED_AT, direction: DESC}) {
    ...