apollo-cache-persist

Simple persistence for all Apollo Client 2.0 cache implementations, including
InMemoryCache and Hermes.
Supports web and React Native. See all storage providers.
Basic Usage
To get started, simply pass your Apollo cache and an
underlying storage provider to persistCache.
By default, the contents of your Apollo cache will be immediately restored
(asynchronously, see how to persist data before rendering), and will be persisted upon every write to the cache (with a
short debounce interval).
Examples
React Native
import AsyncStorage from '@react-native-community/async-storage';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { persistCache } from 'apollo-cache-persist';
const cache = new InMemoryCache({...});
await persistCache({
cache,
storage: AsyncStorage,
});
const client = new ApolloClient({
cache,
...
});
Web
import { InMemoryCache } from 'apollo-cache-inmemory';
import { persistCache } from 'apollo-cache-persist';
const cache = new InMemoryCache({...});
await persistCache({
cache,
storage: window.localStorage,
});
const client = new ApolloClient({
cache,
...
});
Additional Options
persistCache and the constructor for CachePersistor accept the following
options:
persistCache({
cache: ApolloCache<TSerialized>,
storage: PersistentStorage<TPersisted>,
trigger?: 'write' | 'background' | function | false,
debounce?: number,
key?: string,
serialize?: boolean,
maxSize?: number | false,
debug?: boolean,
}): Promise<void>;
Development package
apollo-cache-persist represents stable release that is being used by wider apollo community.
To minimize impact and bring new changes to apollo-cache-persist we have created separate package
that is being updated everytime we merge community PR.
apollo-cache-persist-dev will contain latest changes, package updates and features driven by community,
but it might contain breaking changes or experiments that will not be included in apollo-cache-persist in the future.
Using development version
npm uninstall apollo-cache-persist
npm install apollo-cache-persist-dev
Advanced Usage
Using CachePersistor
Instead of using persistCache, you can instantiate a CachePersistor, which
will give you fine-grained control of persistence.
CachePersistor accepts the same options as persistCache and returns an
object with the following methods:
const persistor = new CachePersistor({...});
persistor.restore();
persistor.persist();
persistor.purge();
persistor.pause();
persistor.resume();
persistor.remove();
persistor.getLogs(print);
persistor.getSize();
Custom Triggers
For control over persistence timing, provide a function to the trigger option.
The custom trigger should accept one argument (a persist callback function),
and it should return a function that can be called to uninstall the trigger.
The TypeScript signature for this function is as follows:
(persist: () => void) => (() => void)
For example, this custom trigger will persist every 10 seconds:
const trigger = persist => {
const interval = setInterval(persist, 10000);
return () => clearInterval(interval);
};
Storage Providers
The following storage providers work 'out of the box', with no additional
dependencies:
AsyncStorage on React Native
window.localStorage on web
window.sessionStorage on web
localForage on web
Ionic storage on web and mobile
apollo-cache-persist uses the same storage provider API as
redux-persist, so you can also make
use of the providers
listed here,
including:
If you're using React Native and set a maxSize in excess of 2 MB, you must use
a filesystem-based storage provider, such as
redux-persist-fs-storage.
AsyncStorage
does not support
individual values in excess of 2 MB on Android.
Common Questions
Why is the 'background' trigger only available for React Native?
Quite simply, because mobile apps are different than web apps.
Mobile apps are rarely terminated before transitioning to the background. This
is helped by the fact that an app is moved to the background whenever the user
returns home, activates the multitasking view, or follows a link to another app.
There's almost always an opportunity to persist.
On web, we could support a 'background' trigger with the
Page Visibility API;
however, data would be lost if the user closed the tab/window directly. Given
this prevalence of this user behavior and the substantially better performance
of the 'write' trigger on web, we've omitted a 'background' trigger on web.
How do I wait for the cache to be restored before rendering my app?
persistCache (as well as persistor.restore()) returns a promise that will
resolve once the cache has been restored, which you can await before rendering
your app.
This library, like Apollo Client, is framework agnostic; however, since many
people have asked, here's an example of how to handle this in React. PRs with
examples from other frameworks are welcome.
React
import React, { Component } from 'react';
import { ApolloProvider } from 'react-apollo';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { persistCache } from 'apollo-cache-persist';
class App extends Component {
state = {
client: null,
loaded: false,
};
async componentDidMount() {
const cache = new InMemoryCache({...});
const client = new ApolloClient({
cache,
...
});
try {
await persistCache({
cache,
storage: window.localStorage,
});
} catch (error) {
console.error('Error restoring Apollo cache', error);
}
this.setState({
client,
loaded: true,
});
}
render() {
const { client, loaded } = this.state;
if (!loaded) {
return <div>Loading...</div>;
}
return (
<ApolloProvider client={client}>
{/* the rest of your app goes here */}
</ApolloProvider>
);
}
}
React Using Hooks
import React,{ useState, useEffect } from 'react';
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloProvider } from "@apollo/react-hooks"
import { persistCache } from 'apollo-cache-persist';
const App: React.FC = () => {
const [client, setClient] = useState(undefined);
useEffect(() => {
const cache = new InMemoryCache({...});
const client = new ApolloClient({
cache,
...
});
const initData = {
{}
};
cache.writeData({ data: initData })
persistCache({
cache,
storage: window.localStorage
}).then(() => {
client.onResetStore(async () => cache.writeData({ data: initData }));
setClient(client);
});
return () => {};
}, []);
if (client === undefined) return <div>Loading...</div>;
return (
<ApolloProvider client={client}>
{/* the rest of your app goes here */}
</ApolloProvider>
);
};
export default App;
Using Synchronous Storage API
persistCache method is asynchronous to conform to production ready storage interfaces
which offer only asynchronous API.
Apollo-cache-persist offers alternative persistCacheSync method that should be used only with small cache sizes and synchronous storage provider (e.g. window.localStorage). persistCacheSync is best suited for demo applications because it blocks UI rendering until the cache is restored.
import { InMemoryCache } from 'apollo-cache-inmemory';
import { persistCacheSync } from 'apollo-cache-persist';
const cache = new InMemoryCache({...});
persistCacheSync({
cache,
storage: window.localStorage,
});
persistCacheSync works by instantiating subclasses of CachePeristor, Persistor, and Storage that implement a method for restoring the cache synchronously.
I need to ensure certain data is not persisted. How do I filter my cache?
Unfortunately, this is not yet possible. You can only persist and restore the
cache in its entirety.
This library depends upon the extract and persist methods defined upon the
cache interface in Apollo Client 2.0. The payload returned and consumed by these
methods is opaque and differs from cache to cache. As such, we cannot reliably
transform the output.
Alternatives have been recommended in
#2,
including using logic in your UI to filter potentially-outdated information.
Furthermore, the maxSize option and
methods on CachePersistor provide facilities to
manage the growth of the cache.
For total control over the cache contents, you can setup a background task to
periodically reset the cache to contain only your app’s most important data. (On
the web, you can use a service worker; on React Native, there’s
react-native-background-task.)
The background task would start with an empty cache, query the most important
data from your GraphQL API, and then persist. This strategy has the added
benefit of ensuring the cache is loaded with fresh data when your app launches.
Finally, it's worth mentioning that the Apollo community is in the early stages
of designing fine-grained cache controls, including the ability to utilize
directives and metadata to control cache policy on a per-key basis, so the
answer to this question will eventually change.
I've had a breaking schema change. How do I migrate or purge my cache?
For the same reasons given in the preceding answer, it's not possible to migrate
or transform your persisted cache data. However, by using the
methods on CachePersistor, it's simple to reset the
cache upon changes to the schema.
Simply instantiate a CachePersistor and only call restore() if the app's
schema hasn't change. (You'll need to track your schema version yourself.)
Here's an example of how this could look:
import AsyncStorage from '@react-native-community/async-storage';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { CachePersistor } from 'apollo-cache-persist';
const SCHEMA_VERSION = '3';
const SCHEMA_VERSION_KEY = 'apollo-schema-version';
async function setupApollo() {
const cache = new InMemoryCache({...});
const persistor = new CachePersistor({
cache,
storage: AsyncStorage,
});
const currentVersion = await AsyncStorage.getItem(SCHEMA_VERSION_KEY);
if (currentVersion === SCHEMA_VERSION) {
await persistor.restore();
} else {
await persistor.purge();
await AsyncStorage.setItem(SCHEMA_VERSION_KEY, SCHEMA_VERSION);
}
}
I'm seeing errors on Android.
Specifically, this error:
BaseError: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
This is the result of a 2 MB per key limitation of AsyncStorage on Android.
Set a smaller maxSize or switch to a filesystem-based storage provider, such
as
redux-persist-fs-storage.
Cache persist and changing user context
In some cases like user logout we want to wipe out application cache.
To do it effectively with Apollo Cache Persist please use client.clearStore() method that will
eventually reset persistence layer.