GithubHelp home page GithubHelp logo

igwejk / plugin-paginate-graphql.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from octokit/plugin-paginate-graphql.js

0.0 1.0 0.0 857 KB

Octokit plugin to paginate GraphQL Query responses

License: MIT License

JavaScript 5.90% TypeScript 94.10%

plugin-paginate-graphql.js's Introduction

plugin-paginate-graphql.js

Octokit plugin to paginate GraphQL API endpoint responses

@latest Build Status

Usage

Browsers

Load @octokit/plugin-paginate-graphql and @octokit/core (or core-compatible module) directly from esm.sh

<script type="module">
  import { Octokit } from "https://esm.sh/@octokit/core";
  import { paginateGraphql } from "https://esm.sh/@octokit/plugin-paginate-graphql";
</script>
Node

Install with npm install @octokit/core @octokit/plugin-paginate-graphql. Optionally replace @octokit/core with a core-compatible module

const { Octokit } = require("@octokit/core");
const { paginateGraphql } = require("@octokit/plugin-paginate-graphql");
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 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.

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:

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, you can pass your own variables as a second parameter to the paginate or iterator function.

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:

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:

pageInfo {
  hasNextPage
  endCursor
}

For a backwards pagination, use:

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:

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 by @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:

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:

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

License

MIT

plugin-paginate-graphql.js's People

Contributors

renovate[bot] avatar gr2m avatar octokitbot avatar kfcampbell avatar davelosert avatar nickfloyd avatar igwejk avatar wolfy1339 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.