Caching Your GraphQL Queries in Deno Runtime

Jessica R Balding
4 min readNov 3, 2022

--

Introducing DenoCacheQL

We are excited to announce the launch of DenoCacheQL, a GraphQL caching tool and GraphQL query playground for the Deno runtime environment. DenoCacheQL is an open source product developed in partnership with the tech accelerator, OS Labs.

Why Use DenoCacheQL?

Caching is a ubiquitous technique used to improve application efficiency. With caching, a developer stores information likely to be fetched multiple times in a temporary location that the application can quickly access. Because GraphQL receives the query as a string in a POST request, this makes caching GraphQL queries more complicated compared to setting the headers options available in traditional RESTful API architecture. While there are many GraphQL tools in the technology space that developers can use to solve this problem, Deno as a newer runtime environment lacks many of these tools.

This is why we decided to create DenoCacheQL. With DenoCacheQL, a developer can quickly and easily cache their GraphQL queries on their Redis server for more efficient queries. The DenoCacheQL playground allows a developer to test their GraphQL queries, receiving the responses, the response times, and the source of their queries (cache or database), all displayed with attractive visualization. Read on for more information about the technologies and how to use this tool!

What is Deno?

Deno is a contemporary runtime environment for JavaScript and TypeScript built with Rust on Google’s V8 engine. Deno was written by Ryan Dahl, the creator of Node.js, addressing issues that Ryan felt Node.js lacked including security and dependencies modules, to name a few. Since Deno works with TypeScript out of the box and offers an increased security by decentralizing package management, this aligns Deno with a more modern runtime environment. For JavaScript developers, Deno is exciting territory and we look forward to Deno’s trajectory.

What is GraphQL?

In 2012, Facebook wanted to address some of the problems with RESTful APIs such as over-fetching and under-fetching of data. They created GraphQL, Graph Query Language, an open-source data query and manipulation language for APIs. GraphQL has the ability to request the exact data the programmer needs which reduces the problem RESTful architecture may potentially have. Additionally, GraphQL can be used in numerous languages such as JavaScript, Python, Java, to name a few.

What is Redis?

Redis (Remote Dictionary Server), is an open-source in-memory data store. It is commonly used as a database, cache, message broker, and queue. As Redis data resides in memory, it provides greater performance improvements since no reading/writing to disk is needed. The average read/write operation on Redis takes less than a millisecond and Redis also supports a variety of data structures. These benefits allow Redis to be a potent caching solution as it can decrease data access latency and ease the load off of the application and database.

DenoCacheQL Sounds Like a Great Tool! How Do I Use It?

To set up your server to use DenoCacheQL:

  • Add the following line to your deno.config (or tsconfig.json) to use the playground for testing your queries.
"include": ["dql_playground/**/*"]
  • Import DenoCacheQL, your resolvers, and your typeDefs.
  • Make sure the Redis server is up and running.
  • Create a new instance of DenoCacheQL.
  • Configure the server to use DenoCacheQL routes.
     //importing DenoCacheQL and your schemaimport  DenoCacheQL  from 'https://deno.land/x/denocacheql'import {resolvers, typeDefs} from "./schema.ts"    //creating a new instanceconst dc = new DenoCacheQL({  typeDefs,  resolvers,  redisInfo: {    hostname: HOST_NAME,    port: PORT,    password: OPTIONAL_PASSWORD,  };});    //using DC routesapp.use(dc.routes());app.use(dc.allowedMethods());    //exporting to use in your resolver logicexport { dc };

Once you’ve imported the module and created your DenoCacheQL instance, you’ll be able to access the DenoCacheQL functions from the context argument. Because DenoCacheQL implementation is modular, you can choose the specific resolvers in which you want to use the caching functionality. One easy way to implement the caching functionality is by wrapping your resolver logic as a callback inside the DenoCacheQL cache function, as demonstrated below.

const resolvers = {  Query: {    myQuery: async (parent, arg, context, info) => {      return await context.dc.cache({parent, arg, context, info},   
async() => {
//put your resolver logic here.... }); };};

Using the Front-End Playground

To use the front-end playground use the URL endpoint /graphql.

We made the front-end playground as intuitive as possible by allowing developers to input queries and mutations with the same syntax they expect from GraphQl. After submitting a query, the returned response will be displayed to the right of the query.

We built the bottom half of the playground to visualize the caching times. Each query response is stored in the table and is recorded with the source of the data coming back (either the database or the cache), as well as the latency. A chart is also rendered and updated with each query to give a full picture of the efficiency of the cache.

Conclusion

We’ve really enjoyed working on this tool and hope you find it useful. DenoCacheQL is an open source product in active development through OS Labs available on DenoLand and Github. If you have any feedback, contributions, or questions, please contact us there.

DenoCacheQL Team LinkedIn Github DenoLand

Jessica Balding LinkedIn Github

Michelle Hindt LinkedIn Github

Regina Kwan LinkedIn Github

ZiHan (Han) Li LinkedIn Github

--

--