Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub API v4: How can I traverse with pagination? (GraphQL)

I'm using Github API v4 to run search query.

From the API documentation I can understand that the following query gives me pageInfo but I don't know how to use it to traverse.

query {
  search(first: 100, type:USER, query:"location:usa repos:>0 language:java") {
    pageInfo {
      startCursor
      hasNextPage
      endCursor
    }
    userCount
    nodes {
        ... on User {
        bio
        company
        email
        id
        isBountyHunter
        isCampusExpert
        isDeveloperProgramMember
        isEmployee
        isHireable
        isSiteAdmin
        isViewer
        location
        login
        name
        url
        websiteUrl
      }
    }
  }
}

And response is:

{
    "data": {
        "search": {
            "pageInfo": {
                "startCursor": "Y3Vyc29yOjE=",
                "hasNextPage": true,
                "endCursor": "Y3Vyc29yOjEwMA=="
            },
    ...
}
like image 458
Furkan Yavuz Avatar asked Jan 05 '18 15:01

Furkan Yavuz


People also ask

How do I use GraphQL with GitHub API?

To communicate with GitHub's GraphQL API, fill in the header name with "Authorization" and the header value with "bearer [your personal access token]". Save this new header for your GraphiQL application. Finally, you are ready to make requests to GitHub's GraphQL API with your GraphiQL application.

Does GraphQL support paging?

To resolve this issue, GraphQL servers can paginate their list fields. This diagram shows offset-based pagination, in which a client requests a page based on an absolute index in the list ( offset ) and the maximum number of elements to return ( limit ).

What is pagination GraphQL?

Pagination means getting a part of data in a large dataset, and continuously get parts of it till the whole dataset has been received. This helps in optimizing our app's performance because trying to get all the data will result in a slow load time of our app and also a slow rendering of the results in the UI.

What is pagination API?

Pagination is a process that is used to divide a large dataset into smaller chunks (pages). All Square API endpoints that return a list of resources support pagination. For example, the SearchCustomers endpoint (Customers API) and ListCustomerRefunds endpoint (Refunds API) support pagination.


1 Answers

According to graphql documentation there are more than one pagination model.

GitHub is using complete connection model

In this model you can traverse with adding after:"Y3Vyc29yOjEwMA==" to your search query.

query {
  search(first: 100, after:"Y3Vyc29yOjEwMA==" type:USER, query:"location:usa repos:>0 language:java") {
    pageInfo {
      startCursor
      hasNextPage
      endCursor
    }
    userCount
    nodes {
        ... on User {
        bio
        company
        email
        id
        isBountyHunter
        isCampusExpert
        isDeveloperProgramMember
        isEmployee
        isHireable
        isSiteAdmin
        isViewer
        location
        login
        name
        url
        websiteUrl
      }
    }
  }
}
like image 84
Furkan Yavuz Avatar answered Sep 16 '22 15:09

Furkan Yavuz