|
1 | 1 | # GraphQL Pagination
|
2 | 2 |
|
| 3 | +## Queries and Pagination |
| 4 | + |
| 5 | +It took me a bit to understand how *pagination* works under the GitHub's implementation of GraphQL. Honestly it isn't that difficult. But the available documentation does not seem to provide any *workable* examples or a very clear explanation. |
| 6 | + |
| 7 | +The GitHub GraphQL uses the *Complete Connection Model* method of pagination. Which is partially descrbed [here](https://graphql.github.io/learn/pagination/#complete-connection-model). |
| 8 | + |
| 9 | +Pagination Examples: |
| 10 | + |
| 11 | +* **`/queries/user-verified-pagination-starred_repos.graphql`** : retrieves the validated user's list of starred repos where pagination can applied. |
| 12 | + |
| 13 | +### Pagination in Detail |
| 14 | + |
| 15 | +Using the **`/queries/user-verified-pagination-starred_repos.graphql`** file I will attempt to explain how pagination works under the GitHub GraphQL API. |
| 16 | + |
| 17 | +First, open a GraphQL *explorer*. This example should also work with GitHub's [GraphQL Explorer](https://docs.github.com/en/free-pro-team@latest/graphql/overview/explorer). Open the Explorer and *paste* the following into it: |
| 18 | + |
| 19 | +The query: |
| 20 | + |
| 21 | +``` |
| 22 | +query($fst:Int, $lst:Int, $aft:String, $bef:String) { |
| 23 | + viewer { |
| 24 | + login |
| 25 | + name |
| 26 | + starredRepositories(orderBy: {direction: DESC, field: STARRED_AT}, |
| 27 | + first: $fst, |
| 28 | + last: $lst, |
| 29 | + after: $aft, |
| 30 | + before: $bef) { |
| 31 | + totalCount |
| 32 | + pageInfo { |
| 33 | + hasNextPage |
| 34 | + endCursor |
| 35 | + hasPreviousPage |
| 36 | + startCursor |
| 37 | + } |
| 38 | + edges { |
| 39 | + starredAt |
| 40 | + node { |
| 41 | + name |
| 42 | + stargazerCount |
| 43 | + primaryLanguage { |
| 44 | + name |
| 45 | + color |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +Variables needed by the query, paste this into the *query variables* pane : |
| 55 | + |
| 56 | +``` |
| 57 | +{ |
| 58 | + "fst": 3, |
| 59 | + "lst": null, |
| 60 | + "aft": null, |
| 61 | + "bef": null |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +To see how pagination works use these steps: |
| 66 | + |
| 67 | + 1) Run the query with the data as shown below |
| 68 | + 2) After the successful query, copy the string from the |
| 69 | + result at `pageInfo.endCursor` into the `aft` data item |
| 70 | + 3) Run the query again |
| 71 | + 4) The next "page" will be returned |
| 72 | + |
| 73 | + Steps 2 - 4 can be repeated until `pageInfo.hasNextPage` |
| 74 | + becomes `false`. |
| 75 | + |
| 76 | + 5) To go to the previous page copy the string from the result |
| 77 | + at `pageInfo.startCursor` into the `bef` data item |
| 78 | + 6) Set `aft` to `null` |
| 79 | + 7) Set `fst` to `null` |
| 80 | + 8) Set `lst` to 3 |
| 81 | + 9) Run the query again |
| 82 | + 10) The previous "page" will be returned |
| 83 | + |
| 84 | + Steps 6 - 9 can be repeated until `pageInfo.hasPreviousPage` |
| 85 | + becomes `false`. |
| 86 | + |
| 87 | +**NOTE**: You will need to have some starred repositories under your account. The minimum recommended for this demonstration is 12. If you have a lot of starred repos change the value used in `"fst"` and `"lst"` to a larger value. For example, if you had 100 starred repos you could change that value from `3` to `25`. That would give you 4 pages of 25 items. |
0 commit comments