-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathREADME.md
285 lines (232 loc) · 7.35 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# plugin-paginate-graphql.js
> Octokit plugin to paginate GraphQL API endpoint responses
[![@latest](https://img.shields.io/npm/v/@octokit/plugin-paginate-graphql.svg)](https://www.npmjs.com/package/@octokit/plugin-paginate-graphql)
[![Build Status](https://github.com/octokit/plugin-paginate-graphql.js/workflows/Test/badge.svg)](https://github.com/octokit/plugin-paginate-graphql.js/actions?workflow=Test)
## Usage
<table>
<tbody valign=top align=left>
<tr><th>
Browsers
</th><td width=100%>
Load `@octokit/plugin-paginate-graphql` and [`@octokit/core`](https://github.com/octokit/core.js) (or core-compatible module) directly from [esm.sh](https://esm.sh)
```html
<script type="module">
import { Octokit } from "https://esm.sh/@octokit/core";
import { paginateGraphql } from "https://esm.sh/@octokit/plugin-paginate-graphql";
</script>
```
</td></tr>
<tr><th>
Node
</th><td>
Install with `npm install @octokit/core @octokit/plugin-paginate-graphql`. Optionally replace `@octokit/core` with a core-compatible module
```js
const { Octokit } = require("@octokit/core");
const { paginateGraphql } = require("@octokit/plugin-paginate-graphql");
```
</td></tr>
</tbody>
</table>
```js
const MyOctokit = Octokit.plugin(paginateGraphql);
const octokit = new MyOctokit({ auth: "secret123" });
const { repository } = await octokit.graphql.paginate(
`query paginate($cursor: String) {
repository(owner: "octokit", name: "rest.js") {
issues(first: 10, after: $cursor) {
nodes {
title
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`,
);
console.log(`Found ${repository.issues.nodes.length} issues!`);
```
There are two convetions this plugin relies on:
1. The name of the cursor variable must be `$cursor`
2. You must include a valid `pageInfo` object in the paginated resource (see [Pagination Direction](#pagination-direction) for more info on what is considered valid)
## `octokit.graphql.paginate()`
The `paginateGraphql` plugin adds a new `octokit.graphql.paginate()` method which accepts a query with a single `$cursor` variable that is used to paginate.
The query gets passed over to the `octokit.graphql()`-function. The response is then scanned for the required `pageInfo`-object. If `hasNextPage` is `true`, it will automatically use the `endCursor` to execute the next query until `hasNextPage` is `false`.
While iterating, it ongoingly merges all `nodes` and/or `edges` of all responses and returns a combined response in the end.
> **Warning**
> Please note that this plugin only supports pagination of a single resource - so you can **not** execute queries with parallel or nested pagination. You can find more details in [the chapter below](#unsupported-nested-pagination).
## `octokit.graphql.paginate.iterator()`
If your target runtime environments supports async iterators (such as most modern browsers and Node 10+), you can iterate through each response:
```js
const pageIterator = octokit.graphql.paginate.iterator(
`query paginate($cursor: String) {
repository(owner: "octokit", name: "rest.js") {
issues(first: 10, after: $cursor) {
nodes {
title
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`,
);
for await (const response of pageIterator) {
const issues = response.repository.issues;
console.log(`${issues.length} issues found.`);
}
```
### Variables
Just like with [octokit/graphql.js](https://github.com/octokit/graphql.js/#variables), you can pass your own variables as a second parameter to the `paginate` or `iterator` function.
```js
await octokit.graphql.paginate(
`
query paginate($cursor: String, $organization: String!) {
repository(owner: $organization, name: "rest.js") {
issues(first: 10, after: $cursor) {
nodes {
title
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`,
{
organization: "octokit",
},
);
```
You can also use this to pass a initial cursor value:
```js
await octokit.graphql.paginate(
`
query paginate($cursor: String, $organization: String!) {
repository(owner: $organization, name: "rest.js") {
issues(first: 10, after: $cursor) {
nodes {
title
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`,
{
organization: "octokit",
cursor: "initialValue",
},
);
```
### Pagination Direction
You can control the pagination direction by the properties deinfed in the `pageInfo` resource.
For a forward pagination, use:
```gql
pageInfo {
hasNextPage
endCursor
}
```
For a backwards pagination, use:
```gql
pageInfo {
hasPreviousPage
startCursor
}
```
If you provide all 4 properties in a `pageInfo`, the plugin will default to forward pagination.
### Unsupported: Nested pagination
Nested pagination with GraphlQL is complicated, so the following **is not supported**:
```js
await octokit.graphql.paginate((cursor) => {
const issuesCursor = cursor.create("issuesCursor");
const commentsCursor = cursor.create("issuesCursor");
return `{
repository(owner: "octokit", name: "rest.js") {
issues(first: 10, after: ${issuesCursor}) {
nodes {
title,
comments(first: 10, after: ${commentsCursor}) {
nodes: {
body
}
pageInfo {
hasNextPage
endCursor
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`;
});
```
There is a great video from GitHub Universe 2019 [Advanced patterns for GitHub's GraphQL API](https://www.youtube.com/watch?v=i5pIszu9MeM&t=719s) by [@ReaLoretta](https://github.com/ReaLoretta) that goes into depth why this is so hard to achieve and patterns and ways around it.
### TypeScript Support
You can type the response of the `paginateGraphql()` and `iterator()` functions like this:
```ts
await octokit.graphql.paginate<RepositoryIssueResponseType>((cursor) => {
return `{
repository(owner: "octokit", name: "rest.js") {
issues(first: 10, after: ${cursor.create()}) {
nodes {
title
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`;
});
```
You can utilize the `PageInfoForward` and `PageInfoBackward`-Interfaces exported from this library to construct your response-types:
```ts
import { PageInfoForward } from "@octokit/plugin-paginate-graphql";
type Issues = {
title: string;
};
type IssueResponseType = {
repository: {
issues: {
nodes: Issues[];
pageInfo: PageInfoForward;
};
};
};
// Response will be of type IssueResponseType
const response = await octokit.graphql.paginate<IssueResponseType>((cursor) => {
return `{
repository(owner: "octokit", name: "rest.js") {
issues(first: 10, after: ${cursor.create()}) {
nodes {
title
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`;
});
```
The `PageInfoBackward` contains the properties `hasPreviousPage` and `startCursor` and can be used accordingly when doing backwards pagination.
## Contributing
See [CONTRIBUTING.md](CONTRIBUTING.md)
## License
[MIT](LICENSE)