Skip to content

Commit 8453fbb

Browse files
wodCZwtrocki
authored andcommitted
Document storage wrappers
1 parent 51a3601 commit 8453fbb

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

README.md

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ short debounce interval).
2424
```js
2525
import AsyncStorage from '@react-native-community/async-storage';
2626
import { InMemoryCache } from '@apollo/client/core';
27-
import { persistCache } from 'apollo3-cache-persist';
27+
import { persistCache, AsyncStorageWrapper } from 'apollo3-cache-persist';
2828

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

3131
// await before instantiating ApolloClient, else queries might run before the cache is persisted
3232
await persistCache({
3333
cache,
34-
storage: AsyncStorage,
34+
storage: new AsyncStorageWrapper(AsyncStorage),
3535
});
3636

3737
// Continue setting up Apollo as usual.
@@ -46,14 +46,14 @@ const client = new ApolloClient({
4646

4747
```js
4848
import { InMemoryCache } from '@apollo/client/core';
49-
import { persistCache } from 'apollo3-cache-persist';
49+
import { persistCache, LocalStorageWrapper } from 'apollo3-cache-persist';
5050

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

5353
// await before instantiating ApolloClient, else queries might run before the cache is persisted
5454
await persistCache({
5555
cache,
56-
storage: window.localStorage,
56+
storage: new LocalStorageWrapper(window.localStorage),
5757
});
5858

5959
// Continue setting up Apollo as usual.
@@ -78,7 +78,7 @@ persistCache({
7878
// Reference to your Apollo cache.
7979
cache: ApolloCache<TSerialized>,
8080

81-
// Reference to your storage provider.
81+
// Reference to your storage provider wrapped in a storage wrapper implementing PersistentStorage interface.
8282
storage: PersistentStorage,
8383

8484
/**
@@ -183,14 +183,16 @@ const trigger = persist => {
183183
184184
## Storage Providers
185185
186-
The following storage providers work 'out of the box', with no additional
187-
dependencies:
186+
`apollo3-cache-persist` provides wrappers for the following storage providers, with no additional dependencies:
188187
189-
- `AsyncStorage` on React Native
190-
- `window.localStorage` on web
191-
- `window.sessionStorage` on web
192-
- [`localForage`](https://github.com/localForage/localForage) on web
193-
- [`Ionic storage`](https://ionicframework.com/docs/building/storage) on web and mobile
188+
| Storage provider | Platform | Wrapper class |
189+
|--- |--- |--- |
190+
| [`AsyncStorage`](https://github.com/react-native-async-storage/async-storage)* | React Native | `AsyncStorageWrapper` |
191+
| `window.localStorage` | web | `LocalStorageWrapper` |
192+
| `window.sessionStorage` | web | `SessionStorageWrapper` |
193+
| [`localForage`](https://github.com/localForage/localForage) | web | `LocalForageWrapper` |
194+
| [`Ionic storage`](https://ionicframework.com/docs/building/storage) | web and mobile | `IonicStorageWrapper` |
195+
| [`MMKV Storage`](https://github.com/ammarahm-ed/react-native-mmkv-storage) | React Native | `MMKVStorageWrapper` |
194196
195197
`apollo3-cache-persist` uses the same storage provider API as
196198
[`redux-persist`](https://github.com/rt2zz/redux-persist), so you can also make
@@ -202,12 +204,20 @@ including:
202204
- [`redux-persist-fs-storage`](https://github.com/leethree/redux-persist-fs-storage)
203205
- [`redux-persist-cookie-storage`](https://github.com/abersager/redux-persist-cookie-storage)
204206
205-
If you're using React Native and set a `maxSize` in excess of 2 MB, you must use
206-
a filesystem-based storage provider, such as
207-
[`redux-persist-fs-storage`](https://github.com/leethree/redux-persist-fs-storage).
208-
`AsyncStorage`
207+
*`AsyncStorage`
209208
[does not support](https://github.com/facebook/react-native/issues/12529#issuecomment-345326643)
210-
individual values in excess of 2 MB on Android.
209+
individual values in excess of 2 MB on Android. If you set `maxSize` to more than 2 MB or to `false`,
210+
use a different storage provider, such as
211+
[`react-native-mmkv-storage`](https://github.com/ammarahm-ed/react-native-mmkv-storage) or
212+
[`redux-persist-fs-storage`](https://github.com/leethree/redux-persist-fs-storage).
213+
214+
### Using other storage providers
215+
216+
`apollo3-cache-persist` supports stable versions of storage providers mentioned above.
217+
If you want to use other storage provider, or there's a breaking change in `next` version of supported provider,
218+
you can create your own wrapper. For an example of a simple wrapper have a look at [`AsyncStorageWrapper`](./src/storageWrappers/AsyncStorageWrapper.ts).
219+
220+
If you found that stable version of supported provider is no-longer compatible, please [submit an issue or a Pull Request](https://github.com/apollographql/apollo-cache-persist/blob/master/CONTRIBUTING.md#issues).
211221
212222
## Common Questions
213223
@@ -242,7 +252,7 @@ examples from other frameworks are welcome.
242252
import React, { Component } from 'react';
243253
import { ApolloProvider } from '@apollo/client/react';
244254
import { InMemoryCache } from '@apollo/client/core';
245-
import { persistCache } from 'apollo3-cache-persist';
255+
import { persistCache, LocalStorageWrapper } from 'apollo3-cache-persist';
246256

247257
class App extends Component {
248258
state = {
@@ -264,7 +274,7 @@ class App extends Component {
264274
// See above for additional options, including other storage providers.
265275
await persistCache({
266276
cache,
267-
storage: window.localStorage,
277+
storage: new LocalStorageWrapper(window.localStorage),
268278
});
269279
} catch (error) {
270280
console.error('Error restoring Apollo cache', error);
@@ -299,7 +309,7 @@ import React,{ useState, useEffect } from 'react';
299309
import { ApolloClient } from 'apollo-client';
300310
import { InMemoryCache } from '@apollo/client/core';
301311
import { ApolloProvider } from "@apollo/react-hooks"
302-
import { persistCache } from 'apollo3-cache-persist';
312+
import { persistCache, LocalStorageWrapper } from 'apollo3-cache-persist';
303313

304314
const App: React.FC = () => {
305315
const [client, setClient] = useState(undefined);
@@ -318,7 +328,7 @@ const App: React.FC = () => {
318328
// See above for additional options, including other storage providers.
319329
persistCache({
320330
cache,
321-
storage: window.localStorage
331+
storage: new LocalStorageWrapper(window.localStorage)
322332
}).then(() => {
323333
client.onResetStore(async () => cache.writeData({ data: initData }));
324334
setClient(client);
@@ -344,13 +354,13 @@ apollo-cache-persist offers alternative `persistCacheSync` method that should be
344354
345355
```js
346356
import { InMemoryCache } from '@apollo/client/core';
347-
import { persistCacheSync } from 'apollo3-cache-persist';
357+
import { persistCacheSync, LocalStorageWrapper } from 'apollo3-cache-persist';
348358

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

351361
persistCacheSync({
352362
cache,
353-
storage: window.localStorage,
363+
storage: new LocalStorageWrapper(window.localStorage),
354364
});
355365
```
356366
@@ -401,7 +411,7 @@ Here's an example of how this could look:
401411
```js
402412
import AsyncStorage from '@react-native-community/async-storage';
403413
import { InMemoryCache } from '@apollo/client/core';
404-
import { CachePersistor } from 'apollo3-cache-persist';
414+
import { CachePersistor, AsyncStorageWrapper } from 'apollo3-cache-persist';
405415

406416
const SCHEMA_VERSION = '3'; // Must be a string.
407417
const SCHEMA_VERSION_KEY = 'apollo-schema-version';
@@ -411,7 +421,7 @@ async function setupApollo() {
411421

412422
const persistor = new CachePersistor({
413423
cache,
414-
storage: AsyncStorage,
424+
storage: new AsyncStorageWrapper(AsyncStorage),
415425
});
416426

417427
// Read the current schema version from AsyncStorage.

0 commit comments

Comments
 (0)