Core Cache Persistence
gqless offers easy to use Cache persistence.
client.backupPersistencegets all the cache and returns a string, and optionally accepts aversionstring parameter, that allows for invalidating previous versions of cache, and ignoring them.client.restorePersistenceaccepts a possible string that should have been stored persistently in some way, and optionally accepts a secondversionstring paramater, that should be the same as the one used inbackupPersistence.
Data Revalidation#
For an effective usage of Cache persistence it's almost always required some kind of automatic data refetching,
and for that reason we recommend you to use:
React
- useQuery and graphql HOC
staleWhileRevalidate, - useTransactionQuery and useLazyQuery
cache-and-networkfetchPolicy,
And setting them by default in your defaults.
- useQuery and graphql HOC
Core
- resolved's onCacheData function.
Examples#
In both examples we use a debounce function exported from gqless that reduces the amount calls to the storages, improving the user experience.
Here you can read a quick explanation of debouncing
localStorage#
In this example, for simplicity we are using localStorage to offer cache persistence,
but for most serious applications we recommend using async solutions like localForage.
Async Storages#
We recommend using localForage or @react-native-async-storage/async-storage