The following is the first few sections of a chapter from GraphQL and Android, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Queries and Paging

Sometimes, there is just too much data.

Users of Web browsers are used to the notion of data paging, whether that comes in the form of pages of search engine results, “append as you scroll” patterns for Twitter timelines, or in other forms.

For data sets known to be potentially large, Web service APIs often offer paging, where clients can indicate a starting point and a number of items to retrieve from the Web service. GraphQL-powered Web services are no exception. In particular, there is one convention for implementing such paging, promoted by the Relay project for Web clients. Since having a Relay-compatible GraphQL server is often an objective, the Relay paging convention has become fairly popular.

In this chapter, we will explore this convention, what it looks like from a GraphQL standpoint, and how to consume this sort of page-at-a-time results from an Apollo-Android client.

Taking a Page from GitHub

GitHub’s GraphQL API makes use of paged queries quite a bit, in part because GitHub has geared its API to handle arbitrarily complex scenarios, including huge projects with lots of contributors and tons of code.

So, let’s start by invoking a GitHub paged query to get the first page of results.

Starring Some Repositories

The specific query that we are going to invoke retrieves the starredRepositories field for the viewer. As the name suggests, starredRepositories represents repositories starred by the logged-in user.

To make this query be effective — both for interactive exploration and for use with this chapter’s sample app — it will help if you have a bunch of starred repositories. Ideally, you have 11 or more.

You can see your starred repositories from your GitHub profile page, in the “Stars” tab:

GitHub Profile, Showing Starred Repositories
Figure 39: GitHub Profile, Showing Starred Repositories

To star a repository, on any GitHub page for that repository, find the button bar towards the top of any GitHub repo page:

GitHub Button Bar
Figure 40: GitHub Button Bar

Click the “Star” button to star it, at which point the button’s caption will change to “Unstar”.

Getting a Page of Results

Once you have some starred repositories, visit the GitHub GraphQL API Explorer and fill in the following GraphQL document in the GraphiQL pane:

query myStars($first: Int, $after: String) {
  viewer {
    login
    starredRepositories(first: $first, after: $after) {
      edges {
        cursor
        node {
          id
          name
        }
      }
      pageInfo {
        hasNextPage
      }
    }
  }
}

Then, in the Query Variables pane, fill in:

{
  "first": 5
}

(we can skip the after variable, as it will default to null, which is what we want here for now)

Then, execute the query using the GraphiQL run button. This should give you a JSON output showing 5 of your starred repositories:

{
  "data": {
    "viewer": {
      "login": "commonsguy",
      "starredRepositories": {
        "edges": [
          {
            "cursor": "Y3Vyc29yOnYyOpHOAC50Bg==",
            "node": {
              "id": "MDEwOlJlcG9zaXRvcnkxNzc2MjA=",
              "name": "em-proxy"
            }
          },
          {
            "cursor": "Y3Vyc29yOnYyOpHOAC50Bw==",
            "node": {
              "id": "MDEwOlJlcG9zaXRvcnkyNTU5MjU=",
              "name": "cwac-task"
            }
          },
          {
            "cursor": "Y3Vyc29yOnYyOpHOAC50CA==",
            "node": {
              "id": "MDEwOlJlcG9zaXRvcnkyNTU5NDI=",
              "name": "cwac-bus"
            }
          },
          {
            "cursor": "Y3Vyc29yOnYyOpHOAC50CQ==",
            "node": {
              "id": "MDEwOlJlcG9zaXRvcnkyNTU5NTA=",
              "name": "cwac-cache"
            }
          },
          {
            "cursor": "Y3Vyc29yOnYyOpHOAC50Cg==",
            "node": {
              "id": "MDEwOlJlcG9zaXRvcnkyNTU5ODE=",
              "name": "cwac-custmenu"
            }
          }
        ],
        "pageInfo": {
          "hasNextPage": true
        }
      }
    }
  }
}

Examining the Query and Its Results

The preview of this section is [REDACTED].

Please, GitHub, Can I Have Some More?

The preview of this section is out seeking fame and fortune as the Dread Pirate Roberts.

Paging with Apollo-Android

The preview of this section was whisked away by a shark-infested tornado.