A lightweight way to cache on graphQL servers

Related tags

Database cacheflow
Overview

cacheflowQL

What is cacheflowQL?

CacheflowQL is an npm package with complex caching algorithms that provide developers deep insights into their GraphQL queries and metrics about their cached data, allowing for in-depth effective runtime and query analysis.

Who is cacheflowQL for?

CacheflowQL is for any developer looking for a lightweight way to cache data on GraphQL servers and receive in-depth metrics concerning the perfomance of their application in order to optimize run-time by utilizing server-side and Redis caching solutions.

Installing cacheflowQL

Download our NPM Package Here

  1. Install your npm dependencies: run npm install cacheflowql in your terminal

Require cacheflowQL in your server file

const { initCache, cache } = require('cacheflowql');

Set up initcache

Local:

  1. checkExpire: Determines the interval that cacheflow checks for expired data
  2. globalThreshold: A threshold value that users can set to prevent caching until input value is met

Redis:

  1. To connect to a redis store provide the host ip, port, and password
initCache({
  local: {
    checkExpire: 10,
    //How often to check if data should be deleted, in seconds

    globalThreshold: 10,
    //How often data needs to be requested to be cached, in seconds
  },
  redis: {
    host: '127.0.0.1',
    port: '6379',
    password: <'redis store password'>
  },
});

Choose which of your resolvers you want to cache

Wrap resolver within the callback function and return data from inside callback function

resolver(parent, args, ctx, info) {
      return cache(
        { location: 'local', maxAge: 60, smartCache: true },
        info,
        function callback() { return data };
      );
    },

Set up cacheConfig object

configObj = {
  location: 'local',
  //either 'local' or 'redis'
  maxAge: 60,
  //How long data will remain in local cache, in seconds
  smartCache: true,
  //boolean, determine if smart cache should be enabled
};

location

  • Set cache location; either local or on redis. Local and/or redis must be initiated using initCache function if either is being used.

maxAge

  • How long data will remain in local cache. After maxAge (in seconds) is passed, data is marked as expired and will be deleted.

smartCache

  • If you want to incorporate the smartCache capabilities of cacheflowQL you need to include a parameter in your cacheConfig object called smartCache and set it equal to true. Smartcache will prevent caching until thresholds are met. Thresholds are established by comparing metrics from specific resolvers to average metrics for all of the user's cached data.

Mutations

  • If you want the cache function to work with mutations you need to wrap mutation resolvers in cache function as well. In the cache config object include a parameter 'mutate' whose value is equal to the name of the query resolver you want to have update.
resolver(parent, args, context, info) {
      return cache(
        { location: 'redis', maxAge: 10, mutate: 'name of resolver to mutate' },
        info,
        function callback() {
          return data;
        }
      );
    },

Other inputs for cache function

The other two parameters in the cache function are info and a callback.

  1. The info parameter is the info parameter from the resolver itself
  2. The callback parameter will be whatever was in your resolver before using cacheflowQL

Simply return the call to cache with your three input parameters and you are set to cache!

return cache(cacheConfigObject, info, callback);

Terminal Commands

To run the terminal commands run node node_modules/cacheflowql/metricsTerminal.js. The terminal will then ask whether you want to view local or global metrics. If you want to see data about all data cached using the cache function input 'Global Metrics.' If you want to see data about a specific resolver simply enter the name of the resolver.

Screen Shot 2021-07-19 at 12 04 50 PM

Screen Shot 2021-07-19 at 12 10 06 PM

Next Steps

We’re off to a great start but are always looking to add new features! Send your comments, questions, and suggestions to [email protected] or simply fork our repo, checkout to a feature branch and get to work improving our product. Happy Caching!

cacheflowQL is a beta product developed under OSLabs

You might also like...

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 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

A GraphQL Generator for Mongo and CosmosDB

A GraphQL Generator for Mongo and CosmosDB

A GraphQL Function Starter Kit for Cosmos DB This is a starter kit for rapid development of a GraphQL API using the Mongo driver for Cosmos DB. You cr

Nov 12, 2021

Very easy graphQL example made by Bobby Chao

Very easy graphQL example made by Bobby Chao. The folder has been organized, the module has been split, and it can be directly used as a development scratch. It using graphQL + node.js + express, and MySQL as datasource.

Sep 18, 2022

Graphql & Apollo

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts After fetch the project, please install n

Dec 21, 2021
Owner
OSLabs Beta
OSLabs Beta
just a graphql example created by typescript + fastify + mikro-orm(postgresql) + mercurius(graphql adaptor) + type-graphql

fastify-mikro-orm-mercurius-graphql-example A MikroORM boilerplate for GraphQL made with Fastify, Mercurius, Typescript using TypeGraphQL ?? Packages

Vigen 10 Aug 28, 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
Ping both Minecraft Bedrock and Java servers.

mineping Collect information about Minecraft (both Java and Bedrock) using Node.js. Description mineping is a Javasript library thar provides Minecraf

null 9 Oct 14, 2022
Bluzelle is a smart, in-memory data store. It can be used as a cache or as a database.

SwarmDB ABOUT SWARMDB Bluzelle brings together the sharing economy and token economy. Bluzelle enables people to rent out their computer storage space

Bluzelle 225 Dec 31, 2022
NodeJS PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.

Node Postgres Extras NodeJS port of Heroku PG Extras with several additions and improvements. The goal of this project is to provide powerful insights

Paweł Urbanek 68 Nov 14, 2022
A remote nodejs Cached sqlite Database Server, for you to have your perfect MAP Cache Saved and useable remotely.

A remote nodejs Cached sqlite Database Server, for you to have your perfect MAP Cache Saved and useable remotely. Easy Server and Client Creations, fast, stores the Cache before stopping and restores it again! it uses ENMAP

Tomato6966 6 Dec 18, 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
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
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