Skip to content

Commit

Permalink
feat: v5 (#175)
Browse files Browse the repository at this point in the history
* maint: spelling (#167)

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>

BREAKING CHANGE: change spelling of `paginateGraphql` export to `paginateGraphQL`

* feat: package is now ESM

BREAKING CHANGE: package is now ESM

* fix: add return type annotation

---------

Co-authored-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
  • Loading branch information
wolfy1339 and jsoref committed Mar 4, 2024
1 parent 90f1218 commit 4d6ff45
Show file tree
Hide file tree
Showing 20 changed files with 126 additions and 113 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Expand Up @@ -56,6 +56,6 @@ Only one version number is bumped at a time, the highest version change trumps t
Besides publishing a new version to npm, semantic-release also creates a git tag and release
on GitHub, generates changelogs from the commit messages and puts them into the release notes.

Before the publish it runs the `npm run build` script which creates a `pkg/` folder with distributions for browsers, node and Typescript definitions. The contents of the `pkg/` folder are published to the npm registry.
Before the publish it runs the `npm run build` script which creates a `pkg/` folder with distributions for browsers, node and TypeScript definitions. The contents of the `pkg/` folder are published to the npm registry.

If the pull request looks good but does not follow the commit conventions, use the <kbd>Squash & merge</kbd> button.
20 changes: 10 additions & 10 deletions README.md
Expand Up @@ -18,7 +18,7 @@ Load `@octokit/plugin-paginate-graphql` and [`@octokit/core`](https://github.com
```html
<script type="module">
import { Octokit } from "https://esm.sh/@octokit/core";
import { paginateGraphql } from "https://esm.sh/@octokit/plugin-paginate-graphql";
import { paginateGraphQL } from "https://esm.sh/@octokit/plugin-paginate-graphql";
</script>
```

Expand All @@ -30,16 +30,16 @@ Node
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");
import { Octokit } from "@octokit/core";
import { paginateGraphQL } from "@octokit/plugin-paginate-graphql";
```

</td></tr>
</tbody>
</table>

```js
const MyOctokit = Octokit.plugin(paginateGraphql);
const MyOctokit = Octokit.plugin(paginateGraphQL);
const octokit = new MyOctokit({ auth: "secret123" });

const { repository } = await octokit.graphql.paginate(
Expand All @@ -61,18 +61,18 @@ const { repository } = await octokit.graphql.paginate(
console.log(`Found ${repository.issues.nodes.length} issues!`);
```

There are two convetions this plugin relies on:
There are two conventions 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 `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.
While iterating, it continually 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).
Expand Down Expand Up @@ -159,7 +159,7 @@ await octokit.graphql.paginate(

### Pagination Direction

You can control the pagination direction by the properties deinfed in the `pageInfo` resource.
You can control the pagination direction by the properties defined in the `pageInfo` resource.

For a forward pagination, use:

Expand All @@ -183,7 +183,7 @@ If you provide all 4 properties in a `pageInfo`, the plugin will default to forw

### Unsupported: Nested pagination

Nested pagination with GraphlQL is complicated, so the following **is not supported**:
Nested pagination with GraphQL is complicated, so the following **is not supported**:

```js
await octokit.graphql.paginate((cursor) => {
Expand Down Expand Up @@ -218,7 +218,7 @@ There is a great video from GitHub Universe 2019 [Advanced patterns for GitHub's

### TypeScript Support

You can type the response of the `paginateGraphql()` and `iterator()` functions like this:
You can type the response of the `paginateGraphQL()` and `iterator()` functions like this:

```ts
await octokit.graphql.paginate<RepositoryIssueResponseType>((cursor) => {
Expand Down
104 changes: 48 additions & 56 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 16 additions & 9 deletions package.json
Expand Up @@ -3,18 +3,18 @@
"publishConfig": {
"access": "public"
},
"type": "module",
"version": "0.0.0-development",
"description": "Octokit plugin to paginate GraphQL API endpoint responses",
"main": "index.js",
"scripts": {
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check '{src,test}/**/*' README.md package.json",
"lint:fix": "prettier --write '{src,test}/**/*' README.md package.json",
"pretest": "npm run -s lint",
"test": "jest --coverage",
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage",
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --strict test/typescript-validate.ts",
"test:watch": "jest --watch",
"test:e2e": "jest --testRegex test/*.e2e.ts"
"test:watch": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --watch",
"test:e2e": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --testRegex test/*.e2e.ts"
},
"repository": "github:octokit/plugin-paginate-graphql.js",
"keywords": [
Expand All @@ -25,12 +25,12 @@
],
"license": "MIT",
"peerDependencies": {
"@octokit/core": ">=5"
"@octokit/core": ">=6"
},
"devDependencies": {
"@octokit/core": "^5.0.0",
"@octokit/plugin-rest-endpoint-methods": "^10.0.0",
"@octokit/tsconfig": "^2.0.0",
"@octokit/core": "^6.0.0",
"@octokit/plugin-rest-endpoint-methods": "^11.0.0",
"@octokit/tsconfig": "^3.0.0",
"@types/fetch-mock": "^7.3.1",
"@types/jest": "^29.0.0",
"@types/node": "^20.0.0",
Expand All @@ -46,11 +46,15 @@
},
"jest": {
"preset": "ts-jest",
"extensionsToTreatAsEsm": [
".ts"
],
"transform": {
"^.+\\.(ts|tsx)$": [
"ts-jest",
{
"tsconfig": "test/tsconfig.test.json"
"tsconfig": "test/tsconfig.test.json",
"useESM": true
}
]
},
Expand All @@ -64,6 +68,9 @@
"functions": 100,
"lines": 100
}
},
"moduleNameMapper": {
"^(.+)\\.jsx?$": "$1"
}
},
"release": {
Expand Down
8 changes: 6 additions & 2 deletions scripts/build.mjs
Expand Up @@ -75,9 +75,13 @@ async function main() {
...pkg,
files: ["dist-*/**", "bin/**"],
main: "dist-node/index.js",
browser: "dist-web/index.js",
types: "dist-types/index.d.ts",
module: "dist-src/index.js",
exports: {
".": {
types: "./dist-types/index.d.ts",
import: "./dist-node/index.js",
}
},
sideEffects: false,
},
null,
Expand Down
2 changes: 1 addition & 1 deletion src/errors.ts
@@ -1,4 +1,4 @@
import type { CursorValue, PageInfoContext } from "./page-info";
import type { CursorValue, PageInfoContext } from "./page-info.js";

// Todo: Add link to explanation
const generateMessage = (path: string[], cursorValue: CursorValue): string =>
Expand Down
4 changes: 2 additions & 2 deletions src/extract-page-info.ts
@@ -1,5 +1,5 @@
import type { PageInfoContext } from "./page-info";
import { findPaginatedResourcePath, get } from "./object-helpers";
import type { PageInfoContext } from "./page-info.js";
import { findPaginatedResourcePath, get } from "./object-helpers.js";

const extractPageInfos = (responseData: any): PageInfoContext => {
const pageInfoPath = findPaginatedResourcePath(responseData);
Expand Down

0 comments on commit 4d6ff45

Please sign in to comment.