# apollo-link-local-cache

> Apollo Link that facilitates locally storing query results

Latest version **0.1.1** (published 2018-10-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install apollo-link-local-cache
pnpm add apollo-link-local-cache
yarn add apollo-link-local-cache
bun add apollo-link-local-cache
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2018-10-17 |
| First published | 2018-10-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 111.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Lucas Constantino Silva |
| Maintainers | lucasconstantino |
| Keywords | graphql, apollo link, apollo, local, cache, storage |

## Links

- npm: https://www.npmjs.com/package/apollo-link-local-cache
- Repository: https://github.com/TallerWebSolutions/apollo-link-local-cache
- Homepage: https://github.com/TallerWebSolutions/apollo-link-local-cache#readme
- Issues: https://github.com/TallerWebSolutions/apollo-link-local-cache/issues
- npm.io page: https://npm.io/package/apollo-link-local-cache

## Dependencies (2)

- [apollo-link](https://npm.io/package/apollo-link.md) ^1.2.3
- [localStorage](https://npm.io/package/localStorage.md) ^1.0.4

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 0.1.1 (latest) — 2018-10-17
- 0.1.0 — 2018-10-17

## README

# Apollo Local Cache Link

[![Build Status](https://travis-ci.org/TallerWebSolutions/apollo-link-local-cache.svg?branch=master)](https://travis-ci.org/TallerWebSolutions/apollo-link-local-cache)
[![coverage](https://img.shields.io/codecov/c/github/TallerWebSolutions/apollo-link-local-cache.svg?style=flat-square)](https://codecov.io/github/TallerWebSolutions/apollo-link-local-cache)
[![npm version](https://img.shields.io/npm/v/apollo-link-local-cache.svg?style=flat-square)](https://www.npmjs.com/package/apollo-link-local-cache)
[![sponsored by Taller](https://raw.githubusercontent.com/TallerWebSolutions/tallerwebsolutions.github.io/master/sponsored-by-taller.png)](https://taller.net.br/en/)

## Purpose

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

## Installation

`yarn add apollo-link-local-cache`

## Usage

```js
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`](https://www.apollographql.com/docs/link/overview.html) | 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:

```js
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.

---
_Source: https://npm.io/package/apollo-link-local-cache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
