Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 3
Message 1 of 2

GraphQL API - Pagination within pages

I'm trying to list the commit history of repositories. In my search query I've set up pagination to get all repositories with a certain open source license, with each page containing 100 repositories.

 

Then I found in each page, I need to set up another pagination for the commit history of every repository, each has a different endCursor. Since they are of two different levels of pagination, I'm wondering:

1) Can I paginate on both repositories and their nodes in one search query?
2) If I need to write another query, how to pass the conditions of the first query (repository name, date, page) to the second one?

 

{
  rateLimit {
    cost
    remaining
    resetAt
  }
  search(query: "license:mit", type: REPOSITORY, first: 100, after: Y3Vyc29yOjEw) {
    repositoryCount
    pageInfo {
      endCursor
      startCursor
      hasNextPage
    }
    nodes {
      ... on Repository {
        defaultBranchRef {
          target {
            ... on Commit {
              history(first: 10) {
                totalCount
                pageInfo {
                  startCursor
                  endCursor
                  hasNextPage
                }
                nodes {
                  additions
                  deletions
                }
              }
            }
          }
        }
      }
    }
  }
}


Any help would be appreciated!

1 Reply
Community Manager
Message 2 of 2

Re: GraphQL API - Pagination within pages

In theory, you could paginate on repositories and their commits in one query, but then you're requesting far more information than you're using. Take your query for example. You would do the following:

 

  1. Make the first request, which returns 100 repository nodes with 10 commit nodes each => 1,000 nodes requested
  2. Paginate to the next 10 commits in the first repository => 1,000 nodes requested for 10 new nodes of information
  3. ...
  4. Paginate to the last 10 commits in the first repository => 1,000 nodes requested for 10 new nodes of information
  5. Paginate to the next 10 commits in the second repository => 1,000 nodes requested for 10 new nodes of information
  6. etc

 

Why spend the time reparsing the 99% of the identical information to get to the 1% that is new? I would collect a list of repositories that I want the commits from, then issue queries to get the commits from them.

 

I hope that helps!