UNPKG

@open-condo/apollo

Version:

A wrapper over @apollo/client that allows you to use persistent cache from local storage, configure TTL, invalidate cache, and use a single configuration for getServerSideProps, SSR, and CSR

42 lines (41 loc) 1.77 kB
import { CachePersistor } from 'apollo3-cache-persist'; import type { NormalizedCacheObject, ApolloCache } from '@apollo/client'; export type { CachePersistor } from 'apollo3-cache-persist'; /** * Creates new cache persistor, which links localStorage with existing cache * @example * const loaderPersistor = createPersistor(cache) * await loaderPersistor.restore() // cache will be filled with localStorage data * await loaderPersistor.persist() // localStorage will be filled with cache data */ export declare function createPersistor(cache: ApolloCache<NormalizedCacheObject>): CachePersistor<NormalizedCacheObject>; type CachePersistorContextType = { persistor?: CachePersistor<NormalizedCacheObject>; }; /** * Basic React Context to pass persistor, obtained from useApollo hook down to your application * @example * const { client, cachePersistor } = useApollo(pageProps) * * return ( * <ApolloProvider client={client}> * <CachePersistorContext.Provider value={{ persistor: cachePersistor }}> * <Component {...pageProps} /> * <CachePersistorContext.Provider/> * <ApolloProvider/> * ) */ export declare const CachePersistorContext: import("react").Context<CachePersistorContextType>; /** * Basic react context consumer, which can be used, to obtain persistor in your app logic * @example * const { persistor } = useCachePersistor() * const { data: readingData, loading: readingLoading } = useGetLatestMeterReadingQuery({ * variables: { * meterId: meter.id, * }, * onError, * skip: !persistor, // Don't fetch data while localStorage is loading (no persistor), since it can be cached there * }) */ export declare function useCachePersistor(): CachePersistorContextType;