CLI tool
apollographql/apollo-cache-persist avatar
apollographql/apollo-cache-persist

apollo3-cache-persist: Persistent Apollo Client 3 Cache

🎏 Simple persistence for all Apollo Cache implementations

1,418 stars123 forksTypeScriptMIT

At a glance

What is it?
apollo3-cache-persist restores and saves Apollo Client 3 cache data on web and React Native. It suits applications that reuse cached GraphQL results after restart, provided startup waits for restoration and the selected storage provider is suitable.
Who is it for?
Adopt apollo3-cache-persist when an Apollo Client 3 application needs to restore cached GraphQL data after restart, especially on web or React Native. Do not adopt it when runtime-only caching is sufficient or when local persistence requires server synchronization, authorization-aware invalidation, or freshness guarantees that the package does not document.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 2 days ago.
What is it written in?
Mainly TypeScript, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on October 20, 2026, and from our analysis. They are not legal advice.

Editorial analysis

Why apollo3-cache-persist Exists for Apollo Apps

apollo3-cache-persist addresses a specific limitation of an in-process Apollo Client cache: its contents normally disappear when the application process exits. For a GraphQL application, that can mean losing useful cached results and fetching them again after a reload. This package adds local persistence around Apollo Client 3 cache implementations so selected storage can retain cache contents between launches.

It is intended for developers building web or React Native applications with Apollo Client 3. The README specifically says it supports all Apollo Client 3.0 cache implementations. It names InMemoryCache as the main example and identifies apollo-cache-hermes as a supported cache. Its API centers on a configured Apollo cache rather than a particular React component, so the direct users are application developers integrating Apollo at startup.

The package does not manage GraphQL requests, mutations, network errors, or query policy. Apollo Client continues to own those responsibilities. apollo3-cache-persist is the layer between the cache and local storage: one side restores existing contents, while the other records cache writes. This narrower scope is useful when an application wants startup continuity without replacing its cache or query architecture.

Persistent cached data can also complicate application behavior. A restored entry may no longer reflect server state, and separate users may require different cache keys or namespaces. The README does not document freshness policies, cache partitioning, server reconciliation, or automatic eviction. Developers adopting the package still need to decide whether restored data is suitable for their correctness and privacy requirements.

persistCache Restoration and Debounced Persistence Flow

The central function is persistCache. According to the README, callers pass an Apollo cache and an underlying storage provider. On startup, the function restores the persisted cache contents asynchronously. Once restoration completes, the package persists the cache after writes, using a short debounce interval so frequent cache changes do not each require an immediate storage operation.

The data path has three parts. Apollo Client creates a cache such as InMemoryCache. A wrapper adapts a platform store, such as AsyncStorage in React Native or window.localStorage in a browser. persistCache connects them, reads persisted contents into the cache during startup, and coordinates later writes. ApolloClient receives that same cache after restoration and then handles application queries and mutations as usual.

The startup ordering matters. Both README examples instruct users to await persistCache before constructing ApolloClient. The reason given in the comments is that queries might run before the cache has been restored. A fire-and-forget call would create a race between asynchronous cache restoration and the application's first Apollo operations.

The debounce also affects persistence timing. The README states the default behavior but does not specify the duration, flush timing, or behavior during application termination. If the host process exits before a pending debounced write occurs, the latest state may not be represented in storage. Applications with strict save-on-exit requirements would need to inspect the advanced documentation or implementation before relying on a particular shutdown outcome.

Installing apollo3-cache-persist for npm and Yarn

The README gives separate installation commands for npm and Yarn. The npm package is `apollo3-cache-persist`, which differs from the GitHub repository name `apollo-cache-persist`. Applications should install the package identifier printed in the README and declared by package.json.

For an npm-managed application, run:

sh
npm install --save apollo3-cache-persist

This records `apollo3-cache-persist` in the application's npm dependency configuration. For a Yarn-managed application, use:

sh
yarn add apollo3-cache-persist

The repository's package.json identifies version `0.15.0`, declares TypeScript declaration output through `lib/index.d.ts`, and exposes both CommonJS and ES module entry points. It also includes build scripts such as `npm run build`, which compiles the TypeScript project and creates a Rollup bundle. Those scripts belong to working on the package. A consuming application normally installs the published dependency rather than running the repository build script.

Installing the package does not add persistence to an existing cache by itself. The application must select a wrapper, pass it with the cache to persistCache, and arrange startup around the returned promise. The README also links to `docs/storage-providers.md` for the provider list, but the main page only confirms the React Native and web examples described below.

First React Native Restore with AsyncStorageWrapper

The React Native example uses `@react-native-async-storage/async-storage` and adapts it with `AsyncStorageWrapper`. It creates an InMemoryCache instance, awaits persistCache, and then constructs ApolloClient with the restored cache. This is the first complete integration shown in the README.

js
import AsyncStorage from '@react-native-async-storage/async-storage';
import { InMemoryCache } from '@apollo/client/core';
import { persistCache, AsyncStorageWrapper } from 'apollo3-cache-persist';

const cache = new InMemoryCache({...});

await persistCache({
  cache,
  storage: new AsyncStorageWrapper(AsyncStorage),
});

const client = new ApolloClient({
  cache,
  ...
});

The `{...}` forms are placeholders copied from the README for application-specific configuration, so a real implementation must provide the cache configuration and the remaining ApolloClient settings. The fixed integration sequence should be preserved: construct the cache, call and await persistCache with `cache` and `storage`, then pass the cache into ApolloClient.

This example does not show how to await restoration inside a React Native entry file that has no top-level await, nor does it define error handling if AsyncStorage cannot be read or written. The linked advanced documentation may cover additional options, but the main README gives no error contract for the example. A team should confirm the application's startup structure and preferred rejection-handling approach before copying the example directly.

First Browser Restore with LocalStorageWrapper

For a browser application, the README uses `LocalStorageWrapper` around `window.localStorage`. The integration has the same ordering as the React Native case: create the Apollo cache, await persistence, and instantiate ApolloClient only after restoration.

js
import { InMemoryCache } from '@apollo/client/core';
import { persistCache, LocalStorageWrapper } from 'apollo3-cache-persist';

const cache = new InMemoryCache({...});

await persistCache({
  cache,
  storage: new LocalStorageWrapper(window.localStorage),
});

const client = new ApolloClient({
  cache,
  ...
});

As in the React Native example, `{...}` represents configuration that the application must supply. The code should run in a browser context where `window.localStorage` exists. The README does not address server-side rendering, storage access failures, quota errors, or environments in which browser storage is disabled. It also does not state whether LocalStorageWrapper performs any quota management beyond adapting the provided store.

Persisting an entire cache to local storage may create an application-specific storage burden, especially when GraphQL results contain substantial objects. The README does not publish cache-size guidance or describe selective persistence in its basic example. Applications with large caches should inspect their actual cache configuration and the advanced documentation rather than assuming the default wrapper imposes a size policy.

Version Boundaries and Maintenance Signals

The repository is not archived, and its last push was on 2026-09-15. That is more recent than the latest listed npm release, `0.15.0`, published on 2024-03-27. Repository activity after a release can contain source, documentation, configuration, or maintenance changes, so the push date alone does not identify the exact contents of those changes or establish compatibility with every Apollo Client release.

The package declares the MIT license. That identifies the repository's declared licensing model, but applications still need to review the `LICENSE` file and any dependency licenses relevant to their distribution process. This article does not offer legal advice about those obligations.

Compatibility is the most concrete upgrade concern visible in the documentation. The README frames support around Apollo Client 3.0 cache implementations, while package.json identifies the package as version `0.15.0`. The repository includes `CHANGELOG.md`, but its entries are not reproduced on the main README page. A team upgrading Apollo Client should check that changelog and the package's TypeScript declarations before replacing a working integration.

The package also has platform-specific dependency considerations. React Native applications need a storage implementation compatible with their environment, and browser applications need a compatible `window.localStorage` context. An npm upgrade can therefore affect more than the TypeScript code when peer dependencies, host frameworks, or storage packages differ across application versions.

Limitations and Direct Persistence Alternatives

The clearest limitation is that restored cache data does not automatically become server-authoritative. The README describes restoration and debounced persistence, but it does not describe background refresh, expiration timestamps, conflict resolution, user-specific cache isolation, or deletion after logout. An application can still issue normal Apollo queries, yet the developer must decide how stale local data interacts with those queries and mutations.

A direct alternative is to serialize and restore the Apollo cache in application code without apollo3-cache-persist. That approach can give the application full control over storage keys, migration, eviction, and user separation. Its cost is that developers must implement cache extraction, restoration, write observation, serialization, and write batching themselves. apollo3-cache-persist packages that coordination and provides documented wrappers for AsyncStorage and browser localStorage.

Another alternative is runtime-only caching. If cached results are not valuable after restart, an application can leave the cache in memory and avoid local storage entirely. This removes storage migration, quota, and stale-data concerns, but it does not provide immediate restoration after a reload.

A separate persistence mechanism attached to a different cache implementation may also fit an application whose cache requirements exceed this package's documented scope. The README mentions Hermes among supported Apollo Client 3 caches, but it does not compare behavior or synchronization features among cache implementations. The decision should therefore rest on documented cache semantics and application needs rather than on the assumption that persisted data is automatically consistent with the server.

Editorial conclusion

Adopt apollo3-cache-persist when an Apollo Client 3 application needs to restore cached GraphQL data after restart, especially on web or React Native. Do not adopt it when runtime-only caching is sufficient or when local persistence requires server synchronization, authorization-aware invalidation, or freshness guarantees that the package does not document. Before adoption, verify the storage wrapper against the target platform, await persistCache before constructing ApolloClient, and confirm compatibility between the installed Apollo Client version and the package.

Frequently asked questions

What does apollo3-cache-persist do?

It asynchronously restores Apollo Client 3 cache contents from local storage and persists cache writes with a short debounce. The README shows storage wrappers for React Native AsyncStorage and browser localStorage.

How do I install apollo3-cache-persist?

Use `npm install --save apollo3-cache-persist` or `yarn add apollo3-cache-persist`. The npm package name is `apollo3-cache-persist`, not the GitHub repository name `apollo-cache-persist`.

Should I await persistCache before creating ApolloClient?

Yes. The README warns that queries could run before the cache has been restored if ApolloClient is instantiated without awaiting persistCache.

Which storage wrappers are shown in the README?

The README shows `AsyncStorageWrapper` with `@react-native-async-storage/async-storage` for React Native and `LocalStorageWrapper` with `window.localStorage` for the web.

Official sources

  1. apollographql/apollo-cache-persist on GitHub
  2. Issues
  3. License: MIT
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/apollographql-apollo-cache-persist.svg)](https://hysenlabs.com/projects/apollographql-apollo-cache-persist)