GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing graphql.org — Pagination | GraphQL LearnPagination PaginationTraverse lists of objects with a consistent field pagination model A common use case in GraphQL is traversing the relationship between sets of objects. There are different ways that these relationships can be exposed in GraphQL, giving a varying set of capabilities to the client developer. On this page, we’ll explore how fields may be paginated using a cursor-based connection model. PluralsThe simplest way to expose a connection between objects is with a field that returns a plural List type. For example, if we wanted to get a list of R2-D2’s friends, we could just ask for all of them: Operation Response SlicingQuickly, though, we realize that there are additional behaviors a client might want. A client might want to be able to specify how many friends they want to fetch—maybe they only want the first two. So we’d want to expose something like this: But if we just fetched the first two, we might want to paginate through the list as well; once the client fetches the first two friends, they might want to send a second request to ask for the next two friends. How can we enable that behavior? Pagination and edgesThere are several ways we could do pagination:
The approach described in the first bullet is classic offset-based pagination. However, this style of pagination can have performance and security downsides, especially for larger data sets. Additionally, if new records are added to the database after the user has made a request for a page of results, then offset calculations for subsequent pages may become ambiguous. In general, we’ve found that cursor-based pagination is the most powerful of those designed. Especially if the cursors are opaque, either offset or ID-based pagination can be implemented using cursor-based pagination (by making the cursor the offset or the ID), and using cursors gives additional flexibility if the pagination model changes in the future. As a reminder that the cursors are opaque and their format should not be relied upon, we suggest base64 encoding them. But that leads us to a problem—how do we get the cursor from the object? We wouldn’t want the cursor to live on the The concept of an edge also proves useful if there is information that is specific to the edge, rather than to one of the objects. For example, if we wanted to expose “friendship time” in the API, having it live on the edge is a natural place to put it. End-of-list, counts, and connectionsNow we can paginate through the connection using cursors, but how do we know when we reach the end of the connection? We have to keep querying until we get an empty list back, but we’d like for the connection to tell us when we’ve reached the end so we don’t need that additional request. Similarly, what if we want additional information about the connection itself, for example, how many friends does R2-D2 have in total? To solve both of these problems, our Note that we also might include Complete connection modelClearly, this is more complex than our original design of just having a plural! But by adopting this design, we’ve unlocked several capabilities for the client:
To see this in action, there’s an additional field in the example schema, called You can try it out in the example query. Try removing the Operation Response Connection specificationTo ensure a consistent implementation of this pattern, the Relay project has a formal specification you can follow for building GraphQL APIs that use a cursor-based connection pattern - whether or not you use Relay. RecapTo recap these recommendations for paginating fields in a GraphQL schema:
next lesson Schema DesignLearn how to design clear, adaptable schemas — including versioning and thoughtful use of nullability. Go to next lesson |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |