npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mktcodelib/graphql-fetch-all

v0.11.2

Published

Utility to fetch all nodes in a paginated graphql query.

Downloads

26

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.