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

stashql

v1.0.8

Published

A nimble, lightweight solution to GraphQL's caching issues

Downloads

7

Readme


Table of Contents


What is StashQL?

StashQL provides GraphQL users with an easy way to cache their data and customize how they want to update the cache when a mutation occurs in the database. StashQL will also log the queries and mutations sent, the data received back from the database (or errors), and the run time of each query/mutation. The logged information can be accessed directly from the logs folder created by StashQL or through StashQL’s CLI.

Install StashQL

npm install stashql

Implementation

Create a StashQL class using the stashql() method that will require the user to pass in their GraphQL schema and Redis Cache, and an optional third integer parameter to set the Time to Live of the cached data.

const StashQL = new stashql(schema, redisCache, 1000);

With the StashQL class, you can use its methods like any other middleware functions

app.use('/graphql', StashQL.queryHandler, (req, res) => {
	return res.status(200).json(res.locals.data);
});

Queries

When sending any GraphQL queries or mutations, you can use StashQL’s queryHandler() method. Upon running a query for the first time, the queryHandler() method will run the query as normal and then cache the query along with the returned data into your Redis cache. If the query is already stored in your cache, the queryHandler() will simply retrieve the data from the cache, avoiding any unnecessary trips to the database. If you ever need to clear your cache, StashQL also offers the clearCache() method, which will go into your cache and flush out all cached data.

Mutations

StashQL provides two methods to update your cache so that you never have to worry about stale data being returned from your queries: the refillCache() method and the clearRelatedFields() method. Both of these methods can be passed in as optional arguments for your mutation types.

refillCache

The refillCache argument will take in a string value, which will be any field you want to update after running a mutation. The queryHandler() method will see this argument, take its string value, and update any cached data relevant to the passed-in string value by re-running their corresponding queries to get the most up-to-date information from the database.

mutation{ 
	addAuthor ( name:"John Smith", refillCache:"authors"){
		name 
	}
}

You will want to use refillCache if you are running a mutation and you only have a few queries in your cache that deal with the field passed in, then you can run this refillCacheHandler function. Since this refillCacheHandler function will re-run all queries in your cache that deal with the field passed in, it will make multiple network requests at once (in order to re-run your queries and get the most up-to-date data). It is important to note that using this function could result in database failure if your cache has too many matches to the passed-in field, as the system will be overloaded with network requests

clearRelatedFields

Similar to the refillCache argument, the clearRelatedFields argument will also take in a string value which will be any field you want to update after a mutation. Upon seeing this argument, instead of updating the cached data relevant to the passed-in string value by re-running their corresponding queries, the queryHandler() method will clear the relevant data from the cache. Now the next time the inputted query is run it will go to the database, send back the updated data, and then cache it.

exampleQuery = 
`
query{
	author{
		name
	}
}
`
mutation{ 
	addAuthor ( name:"John Smith", refillCache:exampleQuery){
		name 
	}
}

A good example of when you would want to utilize clearRelatedFields is if you know that your cache will have a ton of queries that matches the field. It will clear your cache so that the next time you run a query, it will simply re-run ONLY that query, NOT ALL queries that match the field.

Logging

When a query or mutation is send using StashQL it will save the query, its data(or error), and runtime in a logs file. This file can be accessed by naviagting to the logs folder that StashQL creats for you. You can also access the information in the logs folder using StashQL's CLI.

  • View all logged data:
stashql all logs
  • Clear logs
stashql clear logs

The Team

Simon Chen | GitHub | LinkedIn Hakudo Ueno | GitHub | LinkedIn Ian Madden | GitHub | LinkedIn Louie Mendez | GitHub | LinkedIn