From 76a10dd69e3347fb827554e4e513539cfa458faf Mon Sep 17 00:00:00 2001 From: Dariusz Porowski <3431813+DariuszPorowski@users.noreply.github.com> Date: Fri, 23 Jun 2023 01:29:44 -0700 Subject: [PATCH 1/2] feat: add plugin-paginate-graphql --- package-lock.json | 15 +++++++++++++++ package.json | 1 + src/index.ts | 1 + src/octokit.ts | 6 ++++++ 4 files changed, 23 insertions(+) diff --git a/package-lock.json b/package-lock.json index b7721d04f5..1024aee900 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@octokit/app": "^13.1.5", "@octokit/core": "^4.2.1", "@octokit/oauth-app": "^4.2.1", + "@octokit/plugin-paginate-graphql": "^2.0.3", "@octokit/plugin-paginate-rest": "^6.1.0", "@octokit/plugin-rest-endpoint-methods": "^7.1.1", "@octokit/plugin-retry": "^4.1.3", @@ -1963,6 +1964,14 @@ "resolved": "https://registry.npmjs.org/@octokit/openapi-types/-/openapi-types-17.2.0.tgz", "integrity": "sha512-MazrFNx4plbLsGl+LFesMo96eIXkFgEtaKbnNpdh4aQ0VM10aoylFsTYP1AEjkeoRNZiiPe3T6Gl2Hr8dJWdlQ==" }, + "node_modules/@octokit/plugin-paginate-graphql": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@octokit/plugin-paginate-graphql/-/plugin-paginate-graphql-2.0.3.tgz", + "integrity": "sha512-gMZN0bEksY1d67rN4oBdU9Jor682tGt7sbEiidDLHJtsKmhWYEPJgWmPG1e6omrxE5S52VZxSRm5UY/utYje5w==", + "peerDependencies": { + "@octokit/core": ">=4" + } + }, "node_modules/@octokit/plugin-paginate-rest": { "version": "6.1.2", "resolved": "https://registry.npmjs.org/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-6.1.2.tgz", @@ -14745,6 +14754,12 @@ "resolved": "https://registry.npmjs.org/@octokit/openapi-types/-/openapi-types-17.2.0.tgz", "integrity": "sha512-MazrFNx4plbLsGl+LFesMo96eIXkFgEtaKbnNpdh4aQ0VM10aoylFsTYP1AEjkeoRNZiiPe3T6Gl2Hr8dJWdlQ==" }, + "@octokit/plugin-paginate-graphql": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@octokit/plugin-paginate-graphql/-/plugin-paginate-graphql-2.0.3.tgz", + "integrity": "sha512-gMZN0bEksY1d67rN4oBdU9Jor682tGt7sbEiidDLHJtsKmhWYEPJgWmPG1e6omrxE5S52VZxSRm5UY/utYje5w==", + "requires": {} + }, "@octokit/plugin-paginate-rest": { "version": "6.1.2", "resolved": "https://registry.npmjs.org/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-6.1.2.tgz", diff --git a/package.json b/package.json index be336e41fa..7d43883605 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "@octokit/app": "^13.1.5", "@octokit/core": "^4.2.1", "@octokit/oauth-app": "^4.2.1", + "@octokit/plugin-paginate-graphql": "^2.0.3", "@octokit/plugin-paginate-rest": "^6.1.0", "@octokit/plugin-rest-endpoint-methods": "^7.1.1", "@octokit/plugin-retry": "^4.1.3", 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 378a11ca61..5d8efe7e9b 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({ From b7b373a7b8029de0b119db50348a4a9de2fdfebe Mon Sep 17 00:00:00 2001 From: Dariusz Porowski <3431813+DariuszPorowski@users.noreply.github.com> Date: Sat, 22 Jul 2023 12:01:31 -0700 Subject: [PATCH 2/2] docs: GraphQL pagination --- README.md | 35 +++++++++++++++++++++++++++++++++++ package-lock.json | 12 ++++++++---- package.json | 1 + 3 files changed, 44 insertions(+), 4 deletions(-) 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 f2c99ba05a..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", @@ -1968,11 +1969,14 @@ "integrity": "sha512-V8GImKs3TeQRxRtXFpG2wl19V7444NIOTDF24AWuIbmNaNYOQMWRbjcGDXV5B+0n887fgDcuMNOmlul+k+oJtw==" }, "node_modules/@octokit/plugin-paginate-graphql": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/@octokit/plugin-paginate-graphql/-/plugin-paginate-graphql-2.0.3.tgz", - "integrity": "sha512-gMZN0bEksY1d67rN4oBdU9Jor682tGt7sbEiidDLHJtsKmhWYEPJgWmPG1e6omrxE5S52VZxSRm5UY/utYje5w==", + "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": ">=4" + "@octokit/core": ">=5" } }, "node_modules/@octokit/plugin-paginate-rest": { 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",