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

graphql-query-batcher

v1.0.1

Published

light weight query batcher for graphql

Downloads

25,596

Readme

Graphql Query Batcher

A light weight (<700 bytes) graphql query batcher for javascript.

Docs

QueryBatcher

Create a batcher client.

Parameters

  • fetcher Fetcher The url to the graphql endpoint you are targeting.
  • options Options the options to be used by client (optional, default {})
    • options.batchInterval integer duration (in MS) of each batch window. (default 6) (optional, default 6)
    • options.shouldBatch boolean should the client batch requests. (default true) (optional, default true)
    • options.maxBatchSize integer max number of requests in a batch. (default 0) (optional, default 0)
    • options.defaultHeaders boolean default headers to include with every request

Examples

const fetcher = batchedQuery => fetch('path/to/graphql', {
   method: 'post',
   headers: {
     Accept: 'application/json',
     'Content-Type': 'application/json',
   },
   body: JSON.stringify(batchedQuery),
   credentials: 'include',
})
.then(response => response.json())

const client = new QueryBatcher(fetcher, { maxBatchSize: 10 })

fetch

Fetch will send a graphql request and return the parsed json.

Parameters

  • query string the graphql query.
  • variables Variables any variables you wish to inject as key/value pairs.
  • operationName [string] the graphql operationName.
  • overrides Options the client options overrides. (optional, default {})

Examples

client.fetch(`
   query getHuman($id: ID!) {
     human(id: $id) {
       name
       height
     }
   }
`, { id: "1001" }, 'getHuman')
   .then(human => {
     // do something with human
     console.log(human);
   });

Returns promise resolves to parsed json of server response

forceFetch

Fetch will send a graphql request and return the parsed json.

Parameters

  • query string the graphql query.
  • variables Variables any variables you wish to inject as key/value pairs.
  • operationName [string] the graphql operationName.
  • overrides Options the client options overrides. (optional, default {})

Examples

client.forceFetch(`
   query getHuman($id: ID!) {
     human(id: $id) {
       name
       height
     }
   }
`, { id: "1001" }, 'getHuman')
   .then(human => {
     // do something with human
     console.log(human);
   });

Returns Promise<Array<Result>> resolves to parsed json of server response

Default Options

const options = {
  shouldBatch: true,  // should we batch queries?
  batchInterval: 6,   // duration of each batch window (in MS)
  maxBatchSize: 0,    // max number of requests in a batch (0 = no max)
};

Requirements

The graphql implementation you are using must suppoprt batching! This means that your graphql endpoint should be able to take an array of requests and return an array of results.

To learn more read this: https://dev-blog.apollodata.com/query-batching-in-apollo-63acfd859862

To see an expmaple implementation in Golang, see https://github.com/nicksrandall/batched-graphql-handler

Alternatives