0.1.1 • Published 6 years ago

apollo-link-local-cache v0.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Apollo Local Cache Link

Build Status coverage npm version sponsored by Taller

Purpose

An Apollo Link that facilitates locally storing query results via interception of responses.

Installation

yarn add apollo-link-local-cache

Usage

import { LocalCacheLink } from 'apollo-link-local-cache'

const link = new LocalCacheLink()

Options

LocalCacheLink takes an object with the following options to allow customization of it's behavior:

namevaluedefaultrequired
shouldCachebooleanFunctiontruefalse
generateKeyFunctionoperation.toKeyfalse
storageObjectFunctionlocalStorage in the browserfalse
normalizeFunctionJSON.stringifyfalse

shouldCache

The cache policy. When set to true defaults to cache all. When set to a callback, will receive the operation as argument, and should return true or false. Defaults to true.

generateKey

A callback to generate a key for an operation. Defaults to using operation.toKey.

storage

The Storage in use. Will default to window.localStorage when available. Can also be a callback, in which case it will be called with the current operation and should retrieve a valid storage.

normalize

Normalization callback, which receives the operation. Executed prior to storing a query result.

denormalize

Denormalization callback, which receives the operation. Executed after retrieving a cached query result from the store.

hasLocalDirective

A special shouldCache implementation is available for enabling caching of queries which use the @local directive. Use it as follows:

import {
  LocalCacheLink,
  hasLocalDirective as shouldCache,
} from 'apollo-link-local-cache'

const link = new LocalCacheLink({ shouldCache })

Context

The LocalCacheLink does not use the context for anything, but customization of the config options will often receive the full operation, meaning they can achieve their purpose by using the context if necessary.