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

cackle-js

v0.1.6

Published

A library to batch and optimize graphql queries and mutations

Downloads

8

Readme

cackle-js

npm version

A lightweight library to batch and optimize graphql queries and mutations.

Usage

// Install Cackle
npm i cackle-js

//Install Graphql Request or any other request client you prefer
npm i graphql-request
import { RequestManager } from 'cackle-js';
import { request } from 'graphql-request';

// Our graphql endpoint
const url = 'http://localhost:8080/graphql';

// The function that takes a Graphql Query or Mutation and returns a promise of the result
const requestCreator = (queryOrMutation: string) => request(url, queryOrMutation);

// The optional duration in ms the we want to wait before sending a request to batch them together
const batchTimeout = 200; // Default = 100

// The Cackle request manager which will handle all the batching and optimization
const requestManager = new RequestManager(requestCreator, batchTimeout);

// Our queries
const query1 = `{
  todos {
    name
  }
}`;
const query2 = `{
  todos {
    isComplete
  }
}`;

// Cackle will take the two queries and optimize them into one request with the query:
// {
//   todos {
//     name
//     isComplete
//   }
// }

(async () => {
  const promise1 = requestManager.createQuery(query1);
  const promise2 = requestManager.createQuery(query2);

  const [result1, result2] = await Promise.all([promise1, promise2]);
  console.log(JSON.stringify({ result1, result2 }, null, 2));

  // OUTPUT using test-schema (src/test-schema.ts). Note that each request gets the payload back that they requested
  // {
  //   "result1": {
  //     "todos": [
  //       {
  //         "name": "Brush Teeth"
  //       }
  //     ]
  //   },
  //   "result2": {
  //     "todos": [
  //       {
  //         "isComplete": true
  //       }
  //     ]
  //   }
  // }
})();

What it does

Let's imagine an example scenario where you are building a social media app. You have a users component that queries for users and a posts component that queries for posts.

Users component query:

{
  users {
    firstName
    lastName
  }
}

Posts component query:

{
  posts {
    title
    message
    time
  }
}

So you make two separate requests within a very short time of each other right as the page loads. You could create a new query that does both in one and distributes the result to each component, or you could use Cackle.

Cackle waits for a certain amount of time to pass before making a request (100ms by default), then batches any queries together so that only one request is made. So if we used cackle in the exact same scenario that we started with, we would have one request that looks like:

{
  users {
    firstName
    lastName
  }
  posts {
    title
    message
    time
  }
}

This is called batching and it eliminates the extra overhead and allows you to have each component worry only about retrieving it's own data.

On top of that, Cackle also optimizes the batched queries. So lets say we have a component that retrieves the users' names, and another component that retrieves the users posts. Normally we would have two requests and two queries that look like:

Users' names query:

{
  users {
    firstName
    lastName
  }
}

Users' posts query:

{
  users {
    firstName
    posts {
      title
      message
      time
    }
  }
}

So now, we're querying for the same data (firstName) in both places, and we're making two requests. Cackle will optimize these queries into one request that tries to avoid requesting the same data twice. The result would look like:

{
  users {
    firstName
    lastName
    posts {
      title
      message
      time
    }
  }
}

Graphql Request

Zeus

Todo/Roadmap (In descending priority)

  • Flesh out tests to 100% code coverage
  • Add 100% type safety
  • Add easy support for Apollo with an example
  • Add easy support for URQL with an example
  • Breakdown several of the util functions and the processQueries/processMutations functions into more readable pieces
  • Add contributor guidelines (For now, just report any issues please)

License

This project is licensed under the MIT License - see the LICENSE file for details

Special Thanks

This library was sponsored by reddit user /u/minuit1984 as a result of a post he created on /r/javascript. Thanks so much for reaching out to the community and offering to sponsor an open source project.