@apollo/utils.keyvadapter
TypeScript icon, indicating that this package has built-in type declarations

3.1.0 • Public • Published

KeyvAdapter class

The KeyvAdapter class is a simple wrapper for Keyv which implements the KeyValueCache interface from the @apollo/utils.keyvaluecache package. This class is intended for use by (but not limited to) Apollo Server's cache. Keyv supports a number of useful adapters like Redis, Mongo, SQLite, etc.

Usage

Here's an example of using a Redis store for your ApolloServer cache:

Install the necessary packages:

npm install @apollo/utils.keyvadapter keyv @keyv/redis

In Apollo Server v3:

import { ApolloServer } from "apollo-server";
import { KeyvAdapter } from "@apollo/utils.keyvadapter";
import Keyv from "keyv";

new ApolloServer({
  cache: new KeyvAdapter(new Keyv("redis://...")),
});

In Apollo Server v4:

import { ApolloServer } from "@apollo/server";
import { KeyvAdapter } from "@apollo/utils.keyvadapter";
import Keyv from "keyv";

new ApolloServer({
  cache: new KeyvAdapter(new Keyv("redis://...")),
});

Options

disableBatchReads

By default, KeyvAdapter will use DataLoader's batching functionality in order to request a list of keys when possible.

  • If your Keyv.store implements getMany, this will be called with the list of keys aggregated by DataLoader.
  • If your Keyv.store does not implement getMany, get will be called in parallel for each key (awaiting a Promise.all()).

Redis.Cluster from ioredis does not support mget (and thus, store.getMany is broken in this case), so batching should be disabled like so:

new ApolloServer({
  cache: new KeyvAdapter(new Keyv("redis://..."), { disableBatchReads: true }),
});

Package Sidebar

Install

npm i @apollo/utils.keyvadapter

Weekly Downloads

40,370

Version

3.1.0

License

MIT

Unpacked Size

15.2 kB

Total Files

10

Last publish

Collaborators

  • dkuc
  • glasser
  • trevor.scheer
  • apollo-bot