4

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!

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
injoker1
  • 163
  • 10
  • checkout [this](https://stackoverflow.com/questions/48116781/github-api-v4-how-can-i-traverse-with-pagination-graphql) – Bertrand Martel Sep 29 '20 at 19:07
  • @BertrandMartel Thanks.But this after parameter can only go to the next page. I want to know how to skip to the specified page,like page 1 to page 10 – injoker1 Sep 30 '20 at 02:37

1 Answers1

5

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

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • The glory days are over it seems. I tried that for several different repos, but the cursor string (decoded) was always something like `b'cursor:v2:\\x92\\x00\\xce\\x0fu\\x07\\xb1'`. – Nico Schlömer Mar 31 '21 at 17:28
  • @NicoSchlömer do you have a specific repo/request that return this cursor format ? was it with `type: ISSUE` ? – Bertrand Martel Mar 31 '21 at 17:44
  • I'm looking at stargazers. Upon further inspection, I see that it _returns_ the `v2` cursor, but I think that it accepts the original cursor you propose as well. Playing around with it right now to verify. – Nico Schlömer Mar 31 '21 at 17:55
  • @NicoSchlömer Maybe it's a recent addition not yet deployed on the whole network. I still get the original cursor for example [this gist](https://gist.github.com/bertrandmartel/34a3db0a8f223a9d14f07ddaa0329d49) return `cursor:100` for me – Bertrand Martel Mar 31 '21 at 17:59
  • [This gist](https://gist.github.com/nschloe/230532a648c0589362644bd42d0e68ee) gives cursorv2. – Nico Schlömer Mar 31 '21 at 18:12
  • 1
    @NicoSchlömer that's interesting, I've tried on aws ireland zone to see if it was only my location but it also returns `cursor:100`. I will check again in a few days – Bertrand Martel Mar 31 '21 at 18:26
  • 1
    I've added some curl versions at https://github.community/t/get-stargazer-time-with-custom-cursor/171929 (together with what doesn't work) in case you're interested. – Nico Schlömer Apr 01 '21 at 08:53