apollo-link-local-cache
v0.1.1
Published
Apollo Link that facilitates locally storing query results
Downloads
7
Maintainers
Readme
Apollo Local Cache Link
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:
| name | value | default | required |
| ----------- | -------- | -------------------------------------------------------------------------- | ----------------------------- |
| shouldCache | boolean | Function | true
| false |
| generateKey | Function | operation.toKey
| false |
| storage | Object | Function | localStorage
in the browser | false |
| normalize | Function | JSON.stringify
| false |
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.