From 74856f65bd19a1ca95483d41ee92b0cb44888b45 Mon Sep 17 00:00:00 2001 From: Dariusz Porowski <3431813+DariuszPorowski@users.noreply.github.com> Date: Wed, 26 Jul 2023 09:39:21 -0700 Subject: [PATCH] feat: add plugin-paginate-graphql (#2487) * feat: add plugin-paginate-graphql * docs: GraphQL pagination --- README.md | 35 +++++++++++++++++++++++++++++++++++ package-lock.json | 12 ++++++++++++ package.json | 1 + src/index.ts | 1 + src/octokit.ts | 6 ++++++ 5 files changed, 55 insertions(+) diff --git a/README.md b/README.md index ebcb999dd4..3e83daf899 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ The `octokit` package integrates the three main Octokit libraries - [Media Type formats](#media-type-formats) - [Request error handling](#request-error-handling) - [GraphQL API queries](#graphql-api-queries) + - [Pagination](#pagination-1) - [Schema previews](#schema-previews) - [App client](#app-client) - [GitHub App](#github-app) @@ -626,6 +627,40 @@ const { lastIssues } = await octokit.graphql( ); ``` +#### Pagination + +GitHub's GraphQL API returns a maximum of 100 items. If you want to retrieve all items, you can use the pagination API. + +Example: get all issues + +```js +const { allIssues } = await octokit.graphql.paginate( + ` + query allIssues($owner: String!, $repo: String!, $num: Int = 10, $cursor: String) { + repository(owner: $owner, name: $repo) { + issues(first: $num, after: $cursor) { + edges { + node { + title + } + } + pageInfo { + hasNextPage + endCursor + } + } + } + } + `, + { + owner: "octokit", + repo: "graphql.js", + }, +); +``` + +Learn more about [GitHub's GraphQL Pagination](https://github.com/octokit/plugin-paginate-graphql.js#readme) usage. + #### Schema previews Previews can be enabled using the `{mediaType: previews: [] }` option. diff --git a/package-lock.json b/package-lock.json index 7e70ab8a97..3f63b82923 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@octokit/app": "^14.0.0", "@octokit/core": "^5.0.0", "@octokit/oauth-app": "^6.0.0", + "@octokit/plugin-paginate-graphql": "^4.0.0", "@octokit/plugin-paginate-rest": "^8.0.0", "@octokit/plugin-rest-endpoint-methods": "^9.0.0", "@octokit/plugin-retry": "^6.0.0", @@ -1967,6 +1968,17 @@ "resolved": "https://registry.npmjs.org/@octokit/openapi-types/-/openapi-types-18.0.0.tgz", "integrity": "sha512-V8GImKs3TeQRxRtXFpG2wl19V7444NIOTDF24AWuIbmNaNYOQMWRbjcGDXV5B+0n887fgDcuMNOmlul+k+oJtw==" }, + "node_modules/@octokit/plugin-paginate-graphql": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@octokit/plugin-paginate-graphql/-/plugin-paginate-graphql-4.0.0.tgz", + "integrity": "sha512-7HcYW5tP7/Z6AETAPU14gp5H5KmCPT3hmJrS/5tO7HIgbwenYmgw4OY9Ma54FDySuxMwD+wsJlxtuGWwuZuItA==", + "engines": { + "node": ">= 18" + }, + "peerDependencies": { + "@octokit/core": ">=5" + } + }, "node_modules/@octokit/plugin-paginate-rest": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-8.0.0.tgz", diff --git a/package.json b/package.json index bcf121837d..246d1d145f 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "@octokit/app": "^14.0.0", "@octokit/core": "^5.0.0", "@octokit/oauth-app": "^6.0.0", + "@octokit/plugin-paginate-graphql": "^4.0.0", "@octokit/plugin-paginate-rest": "^8.0.0", "@octokit/plugin-rest-endpoint-methods": "^9.0.0", "@octokit/plugin-retry": "^6.0.0", diff --git a/src/index.ts b/src/index.ts index 0f6f6e6066..98d8ba5793 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export { Octokit, RequestError } from "./octokit"; +export type { PageInfoForward, PageInfoBackward } from "./octokit"; export { App, OAuthApp, createNodeMiddleware } from "./app"; diff --git a/src/octokit.ts b/src/octokit.ts index 580afbcf76..40d1a2be61 100644 --- a/src/octokit.ts +++ b/src/octokit.ts @@ -1,5 +1,6 @@ import { Octokit as OctokitCore } from "@octokit/core"; import { paginateRest } from "@octokit/plugin-paginate-rest"; +import { paginateGraphql } from "@octokit/plugin-paginate-graphql"; import { restEndpointMethods } from "@octokit/plugin-rest-endpoint-methods"; import { retry } from "@octokit/plugin-retry"; import { throttling } from "@octokit/plugin-throttling"; @@ -7,10 +8,15 @@ import { throttling } from "@octokit/plugin-throttling"; import { VERSION } from "./version"; export { RequestError } from "@octokit/request-error"; +export type { + PageInfoForward, + PageInfoBackward, +} from "@octokit/plugin-paginate-graphql"; export const Octokit = OctokitCore.plugin( restEndpointMethods, paginateRest, + paginateGraphql, retry, throttling, ).defaults({