You are viewing documentation for a previous version of this software.

Switch to the latest stable version.

Pagination


Often, you will have some views in your application where you need to display a list that contains too much data to be either fetched or displayed at once. Pagination is the most common solution to this problem, and Apollo Client has built-in functionality that makes it quite easy to do.

There are basically two ways of fetching paginated data: numbered pages, and cursors. There are also two ways for displaying paginated data: discrete pages, and infinite scrolling. For a more in-depth explanation of the difference and when you might want to use one vs. the other, we recommend that you read our blog post on the subject: Understanding Pagination.

In this article, we'll cover the technical details of using Apollo to implement both approaches.

Using fetchMore

In Apollo, the easiest way to do pagination is with a function called fetchMore, which is included in the result object returned by the useQuery Hook. This basically allows you to do a new GraphQL query and merge the result into the original result.

You can specify what query and variables to use for the new query, and how to merge the new query result with the existing data on the client. How exactly you do that will determine what kind of pagination you are implementing.

Offset-based

Offset-based pagination — also called numbered pages — is a very common pattern, found on many websites, because it is usually the easiest to implement on the backend. In SQL for example, numbered pages can easily be generated by using OFFSET and LIMIT.

JavaScript
JavaScript
(hooks)
1const FEED_QUERY = gql`
2  query Feed($type: FeedType!, $offset: Int, $limit: Int) {
3    currentUser {
4      login
5    }
6    feed(type: $type, offset: $offset, limit: $limit) {
7      id
8      # ...
9    }
10  }
11`;
12
13const FeedData({ match }) {
14  const { data, fetchMore } = useQuery(
15    FEED_QUERY,
16    {
17      variables: {
18        type: match.params.type.toUpperCase() || "TOP",
19        offset: 0,
20        limit: 10
21      },
22      fetchPolicy: "cache-and-network"
23    }
24  );
25
26  return (
27    <Feed
28      entries={data.feed || []}
29      onLoadMore={() =>
30        fetchMore({
31          variables: {
32            offset: data.feed.length
33          },
34          updateQuery: (prev, { fetchMoreResult }) => {
35            if (!fetchMoreResult) return prev;
36            return Object.assign({}, prev, {
37              feed: [...prev.feed, ...fetchMoreResult.feed]
38            });
39          }
40        })
41      }
42    />
43  );
44}

As you can see, fetchMore is accessible through the useQuery Hook result object. By default, fetchMore will use the original query, so we just pass in new variables. Once the new data is returned from the server, the updateQuery function is used to merge it with the existing data, which will cause a re-render of your UI component with an expanded list.

The above approach works great for limit/offset pagination. One downside of pagination with numbered pages or offsets is that an item can be skipped or returned twice when items are inserted into or removed from the list at the same time. That can be avoided with cursor-based pagination.

Note that in order for the UI component to receive an updated loading prop after fetchMore is called, you must set notifyOnNetworkStatusChange to true in your Query component's props.

Cursor-based

In cursor-based pagination, a "cursor" is used to keep track of where in the data set the next items should be fetched from. Sometimes the cursor can be quite simple and just refer to the ID of the last object fetched, but in some cases — for example lists sorted according to some criteria — the cursor needs to encode the sorting criteria in addition to the ID of the last object fetched.

Implementing cursor-based pagination on the client isn't all that different from offset-based pagination, but instead of using an absolute offset, we keep a reference to the last object fetched and information about the sort order used.

In the example below, we use a fetchMore query to continuously load new comments, which will be prepended to the list. The cursor to be used in the fetchMore query is provided in the initial server response, and is updated whenever more data is fetched.

JavaScript
JavaScript
(hooks)
1const MORE_COMMENTS_QUERY = gql`
2  query MoreComments($cursor: String) {
3    moreComments(cursor: $cursor) {
4      cursor
5      comments {
6        author
7        text
8      }
9    }
10  }
11`;
12
13function CommentsWithData() {
14  const { data: { comments, cursor }, loading, fetchMore } = useQuery(
15    MORE_COMMENTS_QUERY
16  );
17
18  return (
19    <Comments
20      entries={comments || []}
21      onLoadMore={() =>
22        fetchMore({
23          // note this is a different query than the one used in the
24          // Query component
25          query: MORE_COMMENTS_QUERY,
26          variables: { cursor: cursor },
27          updateQuery: (previousResult, { fetchMoreResult }) => {
28            const previousEntry = previousResult.entry;
29            const newComments = fetchMoreResult.moreComments.comments;
30            const newCursor = fetchMoreResult.moreComments.cursor;
31
32            return {
33              // By returning `cursor` here, we update the `fetchMore` function
34              // to the new cursor.
35              cursor: newCursor,
36              entry: {
37                // Put the new comments in the front of the list
38                comments: [...newComments, ...previousEntry.comments]
39              },
40              __typename: previousEntry.__typename
41            };
42          }
43        })
44      }
45    />
46  );
47}

Relay-style cursor pagination

Relay, another popular GraphQL client, is opinionated about the input and output of paginated queries, so people sometimes build their server's pagination model around Relay's needs. If you have a server that is designed to work with the Relay Cursor Connections spec, you can also call that server from Apollo Client with no problems.

Using Relay-style cursors is very similar to basic cursor-based pagination. The main difference is in the format of the query response which affects where you get the cursor.

Relay provides a pageInfo object on the returned cursor connection which contains the cursor of the first and last items returned as the properties startCursor and endCursor respectively. This object also contains a boolean property hasNextPage which can be used to determine if there are more results available.

The following example specifies a request of 10 items at a time and that results should start after the provided cursor. If null is passed for the cursor relay will ignore it and provide results starting from the beginning of the data set which allows the use of the same query for both initial and subsequent requests.

JavaScript
JavaScript
(hooks)
1const COMMENTS_QUERY = gql`
2  query Comments($cursor: String) {
3    Comments(first: 10, after: $cursor) {
4      edges {
5        node {
6          author
7          text
8        }
9      }
10      pageInfo {
11        endCursor
12        hasNextPage
13      }
14    }
15  }
16`;
17
18function CommentsWithData() {
19  const { data: { Comments: comments }, loading, fetchMore } = useQuery(
20    COMMENTS_QUERY
21  );
22
23  return (
24    <Comments
25      entries={comments || []}
26      onLoadMore={() =>
27        fetchMore({
28          variables: {
29            cursor: comments.pageInfo.endCursor
30          },
31          updateQuery: (previousResult, { fetchMoreResult }) => {
32            const newEdges = fetchMoreResult.comments.edges;
33            const pageInfo = fetchMoreResult.comments.pageInfo;
34
35            return newEdges.length
36              ? {
37                  // Put the new comments at the end of the list and update `pageInfo`
38                  // so we have the new `endCursor` and `hasNextPage` values
39                  comments: {
40                    __typename: previousResult.comments.__typename,
41                    edges: [...previousResult.comments.edges, ...newEdges],
42                    pageInfo
43                  }
44                }
45              : previousResult;
46          }
47        })
48      }
49    />
50  );
51}

The @connection directive

When using paginated queries, results from accumulated queries can be hard to find in the store, as the parameters passed to the query are used to determine the default store key but are usually not known outside the piece of code that executes the query. This is problematic for imperative store updates, as there is no stable store key for updates to target. To direct Apollo Client to use a stable store key for paginated queries, you can use the optional @connection directive to specify a store key for parts of your queries. For example, if we wanted to have a stable store key for the feed query earlier, we could adjust our query to use the @connection directive:

JavaScript
1const FEED_QUERY = gql`
2  query Feed($type: FeedType!, $offset: Int, $limit: Int) {
3    currentUser {
4      login
5    }
6    feed(type: $type, offset: $offset, limit: $limit) @connection(key: "feed", filter: ["type"]) {
7      id
8      # ...
9    }
10  }
11`;

This would result in the accumulated feed in every query or fetchMore being placed in the store under the feed key, which we could later use for imperative store updates. In this example, we also use the @connection directive's optional filter argument, which allows us to include some arguments of the query in the store key. In this case, we want to include the type query argument in the store key, which results in multiple store values that accumulate pages from each type of feed.

Feedback

Edit on GitHub

Forums