0.11.2 • Published 11 months ago

@mktcodelib/graphql-fetch-all v0.11.2

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

GraphQL Fetch All

⚠️ The better name for this package would currently be "github-fetch-all", since it is still tied to certain GitHub specific pagination fields. It might become more generic and deserve its own name in the future though.

This package helps to fetch all nodes of paginated fields in a GraphQL query.

npm i @mktcodelib/graphql-fetch-all
import { graphqlFetchAll } from "@mktcodelib/graphql-fetch-all";

const GITHUB_USER_FOLLOWERS_QUERY = gql`query ($login: String!, $first: Int!, $after: String) { 
  user (login: $login) {
    followers (first: $first, after: $after) {
      totalCount
      pageInfo {
        hasNextPage
        endCursor
      }
      nodes {
        login
      }
    }
  }
}`;

const fetchAllFollowers = await graphqlFetchAll({
  url: "https://api.github.com/graphql",
  headers: { Authorization: `Bearer ${process.env.GITHUB_TOKEN}` },
  query: GITHUB_USER_FOLLOWERS_QUERY,
  variables: {
    login,
    first: 100 // initial. adjusted automatically, depending on how many nodes need to be fetched
  },
});

for await (const { data, paginators, variables } of fetchAllFollowers) {
  // data: the current, aggregated response data
  // paginators: info about the individual paginated fields
  // variables: the updated variables used for the current request
}

⚠️ Paginated properties MUST contain a limit and a cursor argument (first|last and before|after) and totalCount, pageInfo and nodes fields as shown in the example.

Multiple Paginators

You can have multiple paginated fields in your query but they must be on the same level and only the highest level will be fetched.

const GITHUB_USER_FOLLOWERS_QUERY = gql`query (
  $login: String!,
  $firstFollowers: Int!,
  $afterFollower: String
  $firstRepos: Int!,
  $afterRepo: String
  $lastIssues: Int = 10,
  $beforeIssue: String
) { 
  user (login: $login) {
    followers (first: $firstFollowers, after: $afterFollower) {
      totalCount
      pageInfo {
        hasNextPage
        endCursor
      }
      nodes {
        login
      }
    }
    repositories (first: $firstRepos, after: $afterRepo) {
      totalCount
      pageInfo {
        hasNextPage
        endCursor
      }
      nodes {
        issues (last: $lastIssues, before: $beforeIssue) {
          totalCount
          totalCount
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            title
            number
          }
        }
      }
    }
  }
}`;

This query fetches all followers and all repositories but only the last 10 issues of each repository.

0.11.0

1 year ago

0.11.1

1 year ago

0.11.2

11 months ago

0.10.2

1 year ago

0.10.1

1 year ago

0.10.0

1 year ago

0.8.0

1 year ago

0.7.0

1 year ago

0.5.0

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

0.2.2

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago