Introducing Splache (/splæʃ/), an agile, user-friendly, and lightweight JavaScript library that efficiently caches GraphQL queries using the power of Redis. The Splache library is designed for improved performance and reduced load on your GraphQL server. Through leveraging the speed and scalability of Redis, Splache is able to provide an efficient and unique solution for caching GraphQL queries!
Launch Page : Learn More about Splache & Demo our package via an interactive sandbox pre-installation
Run npm install splache
to install the package to import SplacheCacheWhole
, ResolverCache
and SplacheCache
.
If you are new to Redis, here is the documentation to install redis and start a redis instance locally.
import splache from 'splache'
1. The Caching of Whole Queries
Simply provide SpacheCacheWhole your schema, redis host, redis port, and password (Only provide the password if your external redis instance is password protected. If not, omit the password. Additionally, omit host, port, and password arguments if connecting to local redis instance) and then direct your queries through the middleware as seen in the example below.
import { SplacheCacheWhole }
const cache = new SplacheCacheWhole(schema, host, post, password);
app.use('/graphql', cache.wholeCache, (req, res) => {
res.send(res.locals.queryResponse)
})
- The Caching of Resolvers
import { ResolverCache } from 'Splache'
const cache = new ResolverCache(host, port, password)
Upon importing ResolverCache from our package, create a new instance of ResolverCache to access the ‘checkCache’ method. From there, simply wrap your resolver functions with our pre-built functionality.
Here is an example:
planet : {
type: Planet,
args: {
id : {
description: 'id of the planet'
type: new GraphQLNonNull(GraphQLString)
}
},
resolve: ((parent, args, context, info) => cache.checkCache(parents, args, context, info, getPlanet))
}
- The Caching of Normalized Query Strings & Breakdown of Complex Nested Queries
Create a new instance of SplacheCache, passing in your schema, host, port, and password (omit host, port, and password if just connecting to local redis instance). By passing your query through our GQLquery method, it’ll generalize and split your query string and check the cache for these individual split queries. This reduces redundancy in your cache if existing cached queries are nested into a complex nested query.
import { SplacheCache } from 'splache'
const cache = newSplacheCache(schema, host, port, password);
app.use('/graphql', cacheGQLquery, (req, res) => {
res.send(res.locals.queryResponse)
})
Nicholas Cathcart | Nicolas Jackson | Jessica Wang | Nancy Zhang |
---|---|---|---|
Splache is an open-source product that is open to input and contributions from the community. After trying out the product, feel free to raise issues or submit a PR request.