All Projects → TallerWebSolutions → apollo-cache-instorage

TallerWebSolutions / apollo-cache-instorage

Licence: MIT license
Apollo Cache implementation that facilitates locally storing resources

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to apollo-cache-instorage

Ddp Apollo
DDP link for Apollo with GraphQL Subscriptions support
Stars: ✭ 163 (+66.33%)
Mutual labels:  apollo, apollo-client
ctrip-apollo
The most delightful and handy Node.js client for ctrip apollo configuration service.
Stars: ✭ 56 (-42.86%)
Mutual labels:  apollo, apollo-client
Agollo
An elegant Go client for Ctrip Apollo
Stars: ✭ 167 (+70.41%)
Mutual labels:  apollo, apollo-client
Apollo Php Client
携程Apollo配置中心PHP客户端
Stars: ✭ 147 (+50%)
Mutual labels:  apollo, apollo-client
matters-web
Website of Matters.News, built with Next.js.
Stars: ✭ 70 (-28.57%)
Mutual labels:  apollo, apollo-client
Reactql
Universal React+GraphQL starter kit: React 16, Apollo 2, MobX, Emotion, Webpack 4, GraphQL Code Generator, React Router 4, PostCSS, SSR
Stars: ✭ 1,833 (+1770.41%)
Mutual labels:  apollo, apollo-client
Kit
ReactQL starter kit (use the CLI)
Stars: ✭ 232 (+136.73%)
Mutual labels:  apollo, apollo-client
React Graphql Github Apollo
🚀 A React + Apollo + GraphQL GitHub Client. Your opportunity to learn about these technologies in a real world application.
Stars: ✭ 1,563 (+1494.9%)
Mutual labels:  apollo, apollo-client
GitHunt-Polymer
An example of a client-side app built with Polymer and Apollo Client.
Stars: ✭ 13 (-86.73%)
Mutual labels:  apollo, apollo-client
instagram-clone
Instagram clone using apollo, react and graphQl (hasura-graphQl engine)
Stars: ✭ 44 (-55.1%)
Mutual labels:  apollo, apollo-cache
Graphql Directive
Use custom directives in your GraphQL schema and queries 🎩
Stars: ✭ 142 (+44.9%)
Mutual labels:  apollo, apollo-client
nap
[Deprecated] NextJS + Apollo + PassportJS
Stars: ✭ 52 (-46.94%)
Mutual labels:  apollo, apollo-client
React Redux Graphql Apollo Bootstrap Webpack Starter
react js + redux + graphQL + Apollo + react router + hot reload + devTools + bootstrap + webpack starter
Stars: ✭ 127 (+29.59%)
Mutual labels:  apollo, apollo-client
Next Graphql Blog
🖊 A Blog including a server and a client. Server is built with Node, Express & a customized GraphQL-yoga server. Client is built with React, Next js & Apollo client.
Stars: ✭ 152 (+55.1%)
Mutual labels:  apollo, apollo-client
Awesome Apollo Graphql
A curated list of amazingly awesome things regarding Apollo GraphQL ecosystem 🌟
Stars: ✭ 126 (+28.57%)
Mutual labels:  apollo, apollo-client
Apollo Cache Redux
Redux cache for Apollo Client 2.0. This project is no longer maintained.
Stars: ✭ 179 (+82.65%)
Mutual labels:  apollo, apollo-client
Guide
📖 The GraphQL Guide website
Stars: ✭ 104 (+6.12%)
Mutual labels:  apollo, apollo-client
Apollo Link
🔗 Interface for fetching and modifying control flow of GraphQL requests
Stars: ✭ 1,434 (+1363.27%)
Mutual labels:  apollo, apollo-client
bookshelf
My GraphQL playground
Stars: ✭ 64 (-34.69%)
Mutual labels:  apollo, apollo-client
boilerplate
Boilerplate for @prisma-cms
Stars: ✭ 22 (-77.55%)
Mutual labels:  apollo, apollo-client

Apollo InStorageCache

Build Status coverage npm version sponsored by Taller

apollo-cache-instorage is an extension to apollo-cache-inmemory that allows for granular cacheability of resources, in a storage of choice.

Purpose

The most famous implementation of a persistence layer for Apollo Client is apollo-cache-persist. The main caveats with that project is the fastly growing size of the cache, and the incapability of chosing what needs and needs not to be cached. apollo-cache-instorage solves that, while reducing the complexity on the setup and limiting interaction points between the caching solution and the Apollo Client multiple services.

Installation

yarn add apollo-cache-instorage

Usage

InStorageCache is an extension of InMemoryCache, so initialization is not so different than the other:

import { InStorageCache } from 'apollo-cache-instorage'
import { HttpLink } from 'apollo-link-http'
import ApolloClient from 'apollo-client'

const cache = new InStorageCache({
  storage: window.localStorage,
})

const client = new ApolloClient({
  link: new HttpLink(),
  cache,
})

Configuration

The InStorageCache constructor takes a config object with all the options available for InMemoryCache plus the following customization properties:

name type default required
storage Object true
shouldPersist Function () => true false
normalize Function JSON.stringify false
denormalize Function JSON.parse false
prefix String '' false

storage

A Web Storage complient storage implementation.

shouldPersist

shouldPersist(
  operation: String,
  dataId: String,
  value: ?Object
)

Callback to determine if a given data object should be cached. Takes three arguments:

  • operation: the ongoing storage operation. Will either be get, set, or delete;
  • dataId: a data object ID as resolved by dataIdFromObject;
  • value: the persisting data object, in case the operation is set.

normalize

normalize(
  value: Object
)

Normalization executed against a data object before attaching to the storage for persistence. Defaults to JSON.stringify.

denormalize

Denormalization executed against a persisted data after retrieving from the storage. Defaults to JSON.parse.

prefix

A prefix to use when persisting data to the storage. Useful for cases when the storage is shared between other application needs.

@persist directive

To facilititate persistance opt-in, this package also provides a mechanism to identify parts of a query that should be persisted using a @persist directive. To enable that, you must:

  1. Configure InStorageCache with an extra key addPersistField set to true;
  2. Use a provided special implementation of shouldPersist;
  3. Add the PersistLink to the chain of links.
import { ApolloLink } from 'apollo-link'
import { createHttpLink } from 'apollo-link-http'
import { InStorageCache, PersistLink } from 'apollo-cache-instorage'

const cache = new InStorageCache({
  addPersistField: true,
  shouldPersist: PersistLink.shouldPersist,
})

const link = ApolloLink.from([
  new PersistLink(),
  createHttpLink({ uri: '/graphql' }),
])

Then, you can mark query selections for persisting using the directive:

query SomeQuery {
  nonPersistingSelection {
    field
  }

  persistingSelection @persist {
    field
  }

  deepPersistingSelection {
    persistingSelection @persist {
      field
    }
  }
}

Caveats

ROOT_QUERY

Most of the cache consumption in Apollo starts off on the ROOT_QUERY special key. Make sure that if you implement shouldPersist you always allow the storage to persist the data related to this key, such as follows:

const shouldPersist = (operation, dataId, value) => {
  if (dataId === 'ROOT_QUERY') return true
  // ... other logic here.
}

cache.restore()

When restoring the cache (SSR hydration, for instance), keep in mind that any value inserted via hydrating will have precedence over the persisted data.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].