injoker1
injoker1

Reputation: 183

How to search specific page through GitHub API V4

Github APIv4 GraphQL has some good features but I cant find a way to search issues using pagination like

https://api.github.com/search/issues?q=repo:user/somerepo+is:open&page=10&per_page=100

Is there a way to solve it? Thanks!

Upvotes: 5

Views: 1016

Answers (1)

Bertrand Martel
Bertrand Martel

Reputation: 45402

Github GraphQL api uses a cursor to iterate through the results. But, there is no documentation on the cursor format and it seems that, for the search query it just base64 encode the string cursor:<digit>

You can check this when you specify pageInfo { endCursor } :

query { 
  search(type:ISSUE, query:"repo:mui-org/material-ui is:issue", first: 100){
        nodes {
      ... on Issue {
        number
        title
      }
    }
    pageInfo {
      endCursor
    }
  }
}

It gives :

"pageInfo": {
   "endCursor": "Y3Vyc29yOjEwMA=="
}

And if you decode Y3Vyc29yOjEwMA== in base64 it gives : cursor:100 so it's not a real cursor and you can use this to paginate the same way as in Rest API v3 (for instance skipping page as you suggested)

Let's say you want the page 10 directly with 100 items per page, it would be cursor:900 which gives Y3Vyc29yOjkwMA== base64 encoded :

{
  search(type: ISSUE, query: "repo:mui-org/material-ui is:issue", first: 100, after:"Y3Vyc29yOjkwMA==") {
    nodes {
      ... on Issue {
        number
        title
      }
      
    }
    issueCount
    pageInfo {
      endCursor
    }
  }
}

A programmatic approach would be to add after: base64("cursor:<item_num>") with item_num starting from 0 (after:"Y3Vyc29yOjA=") to X. You can know X by requesting issueCount value the first time (or in an initial request depending on your usecase)

Note that there is a limit of 1000 results for the Github search API so you can't access page > 10 with per_page=100 theoretically for instance : https://api.github.com/search/issues?q=repo:mui-org/material-ui&page=11&per_page=100 (the same restriction applies for GraphQL)

Also note that the cursor format seems to change depending on the query type, the above answer only applies for search query. For instance, checkout this post for the commit cursor format

Upvotes: 5

Related Questions