|
1 |
| -CacheIQL |
| 1 | +# cacheiql-server |
2 | 2 |
|
3 |
| -CacheIQL is a server-side caching solution for GraphQL applications designed to enhance performance by reducing redundant query executions. It integrates seamlessly into your GraphQL server, leveraging Redis to cache responses and intelligently manage cache invalidation. |
| 3 | +**cacheiql-server** is a middleware that is simple to implement, providing a server-side caching solution for GraphQL applications designed to enhance performance by reducing redundant query executions. It seamlessly integrates into your GraphQL server, caching queries and intelligently invalidating them after a mutation occurs, all while leveraging Redis to manage cache responses efficiently. |
4 | 4 |
|
5 |
| -Features |
6 | 5 |
|
7 |
| -Server-Side Caching: Cache GraphQL query results in Redis to reduce database load. |
| 6 | +--- |
8 | 7 |
|
9 |
| -Cache Invalidation: Invalidate cache entries intelligently for mutations. |
| 8 | +## Installation |
10 | 9 |
|
11 |
| -Middleware Integration: Easy-to-use middleware for GraphQL resolvers. |
| 10 | +### Installing and Connecting a Redis Server |
12 | 11 |
|
13 |
| -Customizable TTL: Configure time-to-live (TTL) for cached entries. |
| 12 | +#### Redis Installation |
14 | 13 |
|
| 14 | +- **Mac (Homebrew):** |
| 15 | + 1. Open your terminal and run: |
| 16 | + ```bash |
| 17 | + brew install redis |
| 18 | + ``` |
| 19 | + 2. Start the Redis server with: |
| 20 | + ```bash |
| 21 | + redis-server |
| 22 | + ``` |
| 23 | + 3. Note the port number where the Redis server is listening. |
15 | 24 |
|
| 25 | +- **Linux or Non-Homebrew:** |
| 26 | + 1. Download the appropriate version of Redis from [redis.io/download](https://redis.io/download). |
| 27 | + 2. Follow the installation instructions provided for your system. |
| 28 | + 3. Start the Redis server and note the port number on which it is listening. |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +### Install CacheIQL-Server |
| 33 | + |
| 34 | +Install the package by running **`npm i cacheiql-server`** in your terminal. This will add **`cacheiql-server`** as a dependency in your package.json file. |
| 35 | + |
| 36 | + |
| 37 | +## Implementation |
| 38 | + |
| 39 | +1. **Import CacheIQL-Server** |
| 40 | + |
| 41 | + Add the following import to your Node.js/Express file: |
| 42 | + |
| 43 | + - **CommonJS:** |
| 44 | + ```js |
| 45 | + const { cacheMiddleware } = require('cacheiql-server'); |
| 46 | + ``` |
| 47 | + - **ES6+:** |
| 48 | + ```js |
| 49 | + import { cacheMiddleware } from 'cacheiql-server'; |
| 50 | + ``` |
| 51 | +
|
| 52 | +2. **Instantiate Cache Middleware** |
| 53 | +
|
| 54 | + Create an instance of `cacheMiddleware` for your GraphQL endpoint by passing in the following parameters: |
| 55 | + |
| 56 | + - `rootValue`: Your GraphQL resolvers. |
| 57 | + - `TTL_IN_SECONDS`: Number of seconds data should persist in the Redis cache. |
| 58 | + - `graphqlSchema`: The GraphQL schema defined using the `graphql-JS` library. |
| 59 | +
|
| 60 | + Example: |
| 61 | + ```js |
| 62 | + const TTL_IN_SECONDS = 1000; |
| 63 | + const cachedRootValue = cacheMiddleware(rootValue, TTL_IN_SECONDS, graphqlSchema); |
| 64 | +3. **Add Cache Middleware to Express Route** |
| 65 | +
|
| 66 | +Attach the `cachedRootValue` to the GraphQL route in your Express app like this: |
| 67 | +
|
| 68 | +```js |
| 69 | +app.use( |
| 70 | + "/graphql", |
| 71 | + graphqlHTTP({ |
| 72 | + schema: graphqlSchema, |
| 73 | + rootValue: cachedRootValue, |
| 74 | + graphiql: true, |
| 75 | + }) |
| 76 | +); |
| 77 | +``` |
| 78 | +This sets up the middleware to cache GraphQL query responses and invalidate them when a mutation occurs. |
| 79 | +
|
| 80 | +4. **Ensure Redis is Running** |
| 81 | +
|
| 82 | +Make sure Redis is installed and running on your machine. |
| 83 | +Start the server using: |
| 84 | +```bash |
| 85 | +redis-server |
| 86 | +``` |
| 87 | +5. **Start the Server** |
| 88 | +
|
| 89 | +Run your Express server: |
| 90 | +```bash |
| 91 | +node index.js |
| 92 | +``` |
| 93 | +Visit [http://localhost:3000/graphql](http://localhost:3000/graphql) to access GraphiQL and start testing the caching behavior. |
| 94 | +
|
| 95 | +6. **Example Implementation** |
| 96 | +
|
| 97 | +Your Express server file should look something like this: |
| 98 | +
|
| 99 | +```js |
| 100 | +const express = require('express'); |
| 101 | +const { graphqlHTTP } = require('express-graphql'); |
| 102 | +const { cacheMiddleware } = require('cacheiql-server'); |
| 103 | +const graphqlSchema = require('./schema/schema'); |
| 104 | +const rootValue = require('./schema/resolvers'); |
| 105 | +
|
| 106 | +const app = express(); |
| 107 | +
|
| 108 | +const TTL_IN_SECONDS = 1000; |
| 109 | +const cachedRootValue = cacheMiddleware(rootValue, TTL_IN_SECONDS, graphqlSchema); |
| 110 | +
|
| 111 | +app.use( |
| 112 | + "/graphql", |
| 113 | + graphqlHTTP({ |
| 114 | + schema: graphqlSchema, |
| 115 | + rootValue: cachedRootValue, |
| 116 | + graphiql: true, |
| 117 | + }) |
| 118 | +); |
| 119 | +
|
| 120 | +app.listen(3000, () => console.log('listening on 3000')); |
| 121 | +``` |
| 122 | +This sets up cacheMiddleware to cache query responses and intelligently invalidate them after mutations, enhancing the performance of your GraphQL application. |
0 commit comments