5. context
Naveen Mathew edited this page Apr 8, 2024
·
2 revisions
A Qwery has to be placed within a QweryContext
to utilize caching. QweryContext
provides CacheStore
and RequestManager
to all subscribers.
On the server, CacheStore
is a no-op cache though a custom one can be specified.
In the browser, CacheStore
is a Map
by default though a custom one can be specified.
A CacheStore
implements the following interfaces
interface CacheStore {
has: (queryKey: Serializable) => boolean;
get: (queryKey: Serializable) => void | CacheValue | Promise<CacheValue>;
set: (queryKey: Serializable, value: CacheValue) => void;
delete: (queryKey: Serializable) => void;
clear?: () => void;
}
type CacheValue = Record<string | symbol | number, any> | unknown[];
interface Serializable {
toString: () => string;
}
A RequestManager
implements the following interface
export interface QweryRequestManager {
has: (queryKey: Serializable) => boolean;
get: (queryKey: Serializable) => any;
set: (queryKey: Serializable, value: any) => void;
delete: (queryKey: Serializable) => void;
}
For the various frameworks, QweryContext
can be supplied using:
- React:
QweryContext
andQweryProvider
- Vue:
provideQweryContext
anduseQweryContext
- Svelte:
setQweryContext
anduseQweryContext
QweryContext
has the following options available:
-
store
: an optionalCacheStore
to be used, can be used to implement an asynchronousCacheStore
-
requestManager
: an optionalRequestManager
to deduplicate requests
-
QweryContextValue
: which provides access to both thecache
and therequestManager
- the
cache
can be used to store prefetched data- when setting prefetched data for GraphQL, making use of normalized caching requires a
__typename
field to be specified on each entity
- when setting prefetched data for GraphQL, making use of normalized caching requires a
- there is no use in using the
requestManager
directly
- the