Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@apollo/utils.keyvadapter

Package Overview
Dependencies
Maintainers
4
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@apollo/utils.keyvadapter

Keyv adapter implementing Apollo's KeyValueCache interface

  • 4.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
4
Created
Source

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

Note: The latest version of this library (v4 and up) only works with Keyv version 5. In order to work with the older version 4 of Keyv, version 3 of this library can be used instead.

In Apollo Server v3:

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

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

In Apollo Server v4:

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

new ApolloServer({
  cache: new KeyvAdapter(new Keyv({ store: new KeyvRedis("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({ store: new KeyvRedis("redis://...") }), {
    disableBatchReads: true,
  }),
});

Keywords

FAQs

Package last updated on 22 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc