Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

QueryCache.md

483 lines8.6 KB

1---
2id: QueryCache
3title: QueryCache
4---
5​
6Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:85
7​
8The `QueryCache` is the storage mechanism for TanStack Query. It stores all the data, meta
9information, and state of the queries it contains.
10​
11Normally, you will not interact with the `QueryCache` directly and instead use a `QueryClient`
12for a specific cache. You can subscribe to it (inherited from `Subscribable`) to be informed of
13safe/known updates to the cache, such as queries being added, removed, or updated β€” updates made
14outside of the cache's own tracked mechanisms (e.g. mutating a query's state object directly) do
15not notify subscribers.
16​
17## Example
18​
19```ts
20const unsubscribe = queryCache.subscribe((event) => {
21 console.log(event.type, event.query)
22})
23```
24​
25## Extends
26​
27- `Subscribable`\<`QueryCacheListener`\>
28​
29## Constructors
30​
31### Constructor
32​
33```ts
34new QueryCache(config?: QueryCacheConfig): QueryCache;
35```
36​
37Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:88
38​
39#### Parameters
40​
41##### config?
42​
43[`QueryCacheConfig`](../interfaces/QueryCacheConfig.md)
44​
45#### Returns
46​
47`QueryCache`
48​
49#### Overrides
50​
51```ts
52Subscribable<QueryCacheListener>.constructor
53```
54​
55## Properties
56​
57### config
58​
59```ts
60config: QueryCacheConfig;
61```
62​
63Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:87
64​
65***
66​
67### listeners
68​
69```ts
70protected listeners: Set<QueryCacheListener>;
71```
72​
73Defined in: packages/query-core/dist-ts/src/subscribable.d.ts:2
74​
75#### Inherited from
76​
77```ts
78Subscribable.listeners
79```
80​
81## Methods
82​
83### build()
84​
85```ts
86build<TQueryFnData, TError, TData, TQueryKey>(
87 client: QueryClient,
88 options: WithRequired<QueryOptions<TQueryFnData, TError, TData, TQueryKey, never>, "queryKey">,
89state?: QueryState<TData, TError>): Query<TQueryFnData, TError, TData, TQueryKey>;
90```
91​
92Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:105
93​
94Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or
95builds and adds a new one to the cache if none exists yet. Used by framework adapters and
96plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing
97the reactive `QueryObserver` machinery.
98​
99#### Type Parameters
100​
101##### TQueryFnData
102​
103`TQueryFnData` = `unknown`
104​
105##### TError
106​
107`TError` = `Error`
108​
109##### TData
110​
111`TData` = `TQueryFnData`
112​
113##### TQueryKey
114​
115`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
116​
117#### Parameters
118​
119##### client
120​
121[`QueryClient`](QueryClient.md)
122​
123##### options
124​
125[`WithRequired`](../type-aliases/WithRequired.md)\<[`QueryOptions`](../interfaces/QueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `never`\>, `"queryKey"`\>
126​
127##### state?
128​
129[`QueryState`](../interfaces/QueryState.md)\<`TData`, `TError`\>
130​
131#### Returns
132​
133[`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
134​
135#### Example
136​
137```ts
138const queryCache = queryClient.getQueryCache()
139​
140const query = queryCache.build(queryClient, {
141 queryKey: ['posts'],
142 queryFn: fetchPosts,
143})
144```
145​
146***
147​
148### clear()
149​
150```ts
151clear(): void;
152```
153​
154Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:135
155​
156Removes all queries from the cache.
157​
158#### Returns
159​
160`void`
161​
162#### Example
163​
164```ts
165const queryCache = queryClient.getQueryCache()
166​
167queryCache.clear()
168```
169​
170***
171​
172### find()
173​
174```ts
175find<TQueryFnData, TError, TData>(filters: WithRequired<QueryFilters<readonly unknown[]>, "queryKey">):
176 | Query<TQueryFnData, TError, TData, readonly unknown[]>
177 | undefined;
178```
179​
180Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:180
181​
182A slightly more advanced method that can be used to get an existing query instance from the
183cache. This instance not only contains all the state for the query, but all of the instances,
184and underlying guts of the query as well. If the query does not exist, `undefined` is
185returned.
186​
187This is not typically needed for most applications, but can come in handy when needing more
188information about a query in rare scenarios (e.g. looking at `query.state.dataUpdatedAt` to
189decide whether a query is fresh enough to be used as an initial value).
190​
191#### Type Parameters
192​
193##### TQueryFnData
194​
195`TQueryFnData` = `unknown`
196​
197##### TError
198​
199`TError` = `Error`
200​
201##### TData
202​
203`TData` = `TQueryFnData`
204​
205#### Parameters
206​
207##### filters
208​
209[`WithRequired`](../type-aliases/WithRequired.md)\<[`QueryFilters`](../interfaces/QueryFilters.md)\<readonly `unknown`[]\>, `"queryKey"`\>
210​
211#### Returns
212​
213 \| [`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, readonly `unknown`[]\>
214 \| `undefined`
215​
216#### See
217​
218[QueryCache#findAll](#findall)
219​
220#### Example
221​
222```ts
223const queryCache = queryClient.getQueryCache()
224​
225const query = queryCache.find({ queryKey: ['posts'] })
226```
227​
228***
229​
230### findAll()
231​
232```ts
233findAll(filters?: QueryFilters<any>): Query<unknown, Error, unknown, readonly unknown[]>[];
234```
235​
236Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:196
237​
238An even more advanced method that can be used to get existing query instances from the cache
239that partially match a query key. If no queries match, an empty array is returned.
240​
241This is not typically needed for most applications, but can come in handy when needing more
242information about queries in rare scenarios.
243​
244#### Parameters
245​
246##### filters?
247​
248[`QueryFilters`](../interfaces/QueryFilters.md)\<`any`\>
249​
250#### Returns
251​
252[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
253​
254#### See
255​
256[QueryCache#find](#find)
257​
258#### Example
259​
260```ts
261const queryCache = queryClient.getQueryCache()
262​
263const queries = queryCache.findAll({ queryKey: ['posts'] })
264```
265​
266***
267​
268### get()
269​
270```ts
271get<TQueryFnData, TError, TData, TQueryKey>(queryHash: string):
272 | Query<TQueryFnData, TError, TData, TQueryKey>
273 | undefined;
274```
275​
276Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:150
277​
278Returns the `Query` instance stored under the given `queryHash`, or `undefined` if none
279exists. Unlike [QueryCache#find](#find), this looks up by the already-computed hash rather
280than by `QueryFilters`. Used by plugins (e.g. broadcast/hydration) that already have a hash
281to look up directly.
282​
283#### Type Parameters
284​
285##### TQueryFnData
286​
287`TQueryFnData` = `unknown`
288​
289##### TError
290​
291`TError` = `Error`
292​
293##### TData
294​
295`TData` = `TQueryFnData`
296​
297##### TQueryKey
298​
299`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
300​
301#### Parameters
302​
303##### queryHash
304​
305`string`
306​
307#### Returns
308​
309 \| [`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
310 \| `undefined`
311​
312#### Example
313​
314```ts
315const queryCache = queryClient.getQueryCache()
316const queryHash = hashKey(['posts'])
317​
318const query = queryCache.get(queryHash)
319```
320​
321***
322​
323### getAll()
324​
325```ts
326getAll(): Query<unknown, Error, unknown, readonly unknown[]>[];
327```
328​
329Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:161
330​
331Returns all queries within the cache.
332​
333#### Returns
334​
335[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
336​
337#### Example
338​
339```ts
340const queryCache = queryClient.getQueryCache()
341​
342const queries = queryCache.getAll()
343```
344​
345***
346​
347### hasListeners()
348​
349```ts
350hasListeners(): boolean;
351```
352​
353Defined in: packages/query-core/dist-ts/src/subscribable.d.ts:5
354​
355#### Returns
356​
357`boolean`
358​
359#### Inherited from
360​
361```ts
362Subscribable.hasListeners
363```
364​
365***
366​
367### onSubscribe()
368​
369```ts
370protected onSubscribe(): void;
371```
372​
373Defined in: packages/query-core/dist-ts/src/subscribable.d.ts:6
374​
375#### Returns
376​
377`void`
378​
379#### Inherited from
380​
381```ts
382Subscribable.onSubscribe
383```
384​
385***
386​
387### onUnsubscribe()
388​
389```ts
390protected onUnsubscribe(): void;
391```
392​
393Defined in: packages/query-core/dist-ts/src/subscribable.d.ts:7
394​
395#### Returns
396​
397`void`
398​
399#### Inherited from
400​
401```ts
402Subscribable.onUnsubscribe
403```
404​
405***
406​
407### remove()
408​
409```ts
410remove(query: Query<any, any, any, any>): void;
411```
412​
413Defined in: packages/query-core/dist-ts/src/queryCache.d.ts:124
414​
415Destroys the given `Query` and removes it from the cache, notifying subscribers with a
416`'removed'` event. A no-op if the query is no longer the one currently stored under its hash
417(e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror
418removals across `QueryCache` instances.
419​
420#### Parameters
421​
422##### query
423​
424[`Query`](Query.md)\<`any`, `any`, `any`, `any`\>
425​
426#### Returns
427​
428`void`
429​
430#### Example
431​
432```ts
433const queryCache = queryClient.getQueryCache()
434const query = queryCache.find({ queryKey: ['posts'] })
435​
436if (query) {
437 queryCache.remove(query)
439```
440​
441***
442​
443### subscribe()
444​
445```ts
446subscribe(listener: QueryCacheListener): () => void;
447```
448​
449Defined in: packages/query-core/dist-ts/src/subscribable.d.ts:4
450​
451#### Parameters
452​
453##### listener
454​
455`QueryCacheListener`
456​
457Called on each update, with whatever the subclass passes to its subscribers.
458​
459#### Returns
460​
461```ts
462(): void;
463```
464​
465##### Returns
466​
467`void`
468​
469#### Example
470​
471```ts
472const unsubscribe = subscribable.subscribe(() => {
473 // react to the update
474})
475​
476unsubscribe()
477```
478​
479#### Inherited from
480​
481```ts
482Subscribable.subscribe
483```