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

QueryCache.md

493 lines10.0 KB

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