@mktcodelib/graphql-fetch-all
v0.11.2
Published
Utility to fetch all nodes in a paginated graphql query.
Downloads
1
Readme
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.