StashQL is a light-weight, open-source npm package that improves the speed of your GraphQL queries in your application.

Related tags

Database graphql
Overview

logo


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

You might also like...

Yaade is an open-source, self-hosted, collaborative API development environment.

Yaade is an open-source, self-hosted, collaborative API development environment.

Yaade - Yet Another API Development Environment Yaade is an open-source, self-hosted, collaborative API development environment. 🤔 Why did you develo

Dec 28, 2022

open source ffxiv community discord bot that's incredibly easy to self-host

Venat Venat is an open-source Discord bot for the Final Fantasy XIV community that is incredibly easy to self-host. Description We aim to offer the fo

Jun 9, 2022

Workshop to illustrate how to use GraphQL

Workshop to illustrate how to use GraphQL

🎓 Netflix Clone using Astra DB and GraphQL 50 minutes, Intermediate, Start Building A simple ReactJS Netflix homepage clone running on Astra DB that

Jan 4, 2023

Learn GraphQL PIAIC CNC Class code

GraphQL using React! Steps (for 01 - react-graphql) Generate and copy Access Token from Github Personal Acess Token Create .env file in project folder

Jan 2, 2023

Código desenvolvido na mentoria do Hiring Coders utilizando Express e GraphQL

hiringcoders-graphql Código desenvolvido na mentoria do Hiring Coders utilizando Express e GraphQL Contribuições A ideia do repositório é continuar si

Dec 29, 2022

A lightweight way to cache on graphQL servers

A lightweight way to cache on graphQL servers

cacheflowQL What is cacheflowQL? CacheflowQL is an npm package with complex caching algorithms that provide developers deep insights into their GraphQ

Nov 16, 2022

A template for WebSockets powered Cloudflare Worker project using graphql-ws

🚡 graphql-ws on Cloudflare Workers A template for WebSockets powered Cloudflare Worker project using graphql-ws. The worker serves the following rout

Dec 18, 2022

Unofficial API Documentation for the Axie Infinity's GraphQL Endpoint.

Axie GraphQL Documentation API Documentation for the Axie Infinity GraphQL Endpoint. Customization Guide This site is usings rocketseat. You may refer

Nov 24, 2022

A Crypto App built to pracitse GraphQL Federation

Getting Started To start this project please first create a .env file in the root of your project with the following: REACT_APP_ASTRA_TOKEN={your_astr

Dec 28, 2022
Conjure SQL from GraphQL queries 🧙🔮✨

Sqlmancer Conjure SQL from your GraphQL queries ?? ?? ✨ ⚠️ This project is currently on hiatus. I am hoping to resume working on Sqlmancer once I have

Daniel Rearden 132 Oct 30, 2022
curl for GraphQL with autocomplete, subscriptions and GraphiQL. Also a dead-simple universal javascript GraphQL client.

graphqurl graphqurl is a curl like CLI for GraphQL. It's features include: CLI for making GraphQL queries. It also provisions queries with autocomplet

Hasura 3.2k Jan 3, 2023
GraphQL Fastify Server is an implementation of GraphQL.

GraphQL Fastify Server Installation Usage Using cache Middlewares Liveness & Readiness Contributing License Installation npm install --save graphql-fa

Rui Silva 33 Dec 19, 2022
Nodeparse - A lightweight, vanilla replacement for Express framework when parsing the HTTP body's data or parsing the URL parameters and queries with NodeJS.

nodeparse A lightweight, vanilla replacement for Express framework when parsing the HTTP body's data or parsing the URL parameters and queries with No

Trần Quang Kha 1 Jan 8, 2022
Run SPARQL/SQL queries directly on Virtuoso database with connection pool support.

?? virtuoso-connector Package that allows you to create a direct connection to the Virtuoso database and run queries on it. Connection can be used to

Tomáš Dvořák 6 Nov 15, 2022
Validate and auto-generate TypeScript types from raw SQL queries in PostgreSQL.

SafeQL Write SQL Queries With Confidence • Get started Install I would first recommend follow the instructions in the documentation. npm install --sav

null 747 Dec 28, 2022
Application made to show the basic concepts of GraphQL with Apollo Server

graphql-insta-example Application made to show the basic concepts of GraphQL with Apollo Server. Getting Started Run npm install Run npm run dev Go to

Ana Julia Bittencourt 26 Aug 26, 2022
Execute one command (or mount one Node.js middleware) and get an instant high-performance GraphQL API for your PostgreSQL database!

PostGraphile Instant lightning-fast GraphQL API backed primarily by your PostgreSQL database. Highly customisable and extensible thanks to incredibly

Graphile 11.7k Jan 4, 2023
An open source cybersecurity protocol for syncing decentralized graph data.

GUN is an ecosystem of tools that let you build community run and encrypted applications. Currently, Internet Archive and HackerNoon run GUN in produc

Mark Nadal 16.7k Dec 27, 2022
Open source code of classic.minecraft.net

Minecraft classic Open source This Project is a clone for classic.minecraft.net. Owner of this Repo (CodeWhiteWeb) doesn't have any rights to this pro

CodeWhiteWeb 4 Dec 16, 2022