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

QueryCache.md

468 lines9.2 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:126](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L126)
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:126](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L126)
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:147](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L147)
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​
100##### options
101​
102[`WithRequired`](../type-aliases/WithRequired.md)\<`QueryOptions`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `never`\>, `"queryKey"`\>
103​
104##### state?
105​
106[`QueryState`](../interfaces/QueryState.md)\<`TData`, `TError`\>
107​
108#### Returns
109​
110[`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
111​
112#### Example
113​
114```ts
115const queryCache = queryClient.getQueryCache()
116​
117const query = queryCache.build(queryClient, {
118 queryKey: ['posts'],
119 queryFn: fetchPosts,
120})
121```
122​
123#### Inherited from
124​
125```ts
126QC.build
127```
128​
129***
130​
131### clear()
132​
133```ts
134clear(): void;
135```
136​
137Defined in: [packages/query-core/src/queryCache.ts:228](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L228)
138​
139Removes all queries from the cache.
140​
141#### Returns
142​
143`void`
144​
145#### Example
146​
147```ts
148const queryCache = queryClient.getQueryCache()
149​
150queryCache.clear()
151```
152​
153#### Inherited from
154​
155```ts
156QC.clear
157```
158​
159***
160​
161### find()
162​
163```ts
164find<TQueryFnData, TError, TData>(filters: MaybeRefDeep<WithRequired<QueryFilters<readonly unknown[]>, "queryKey">>):
165 | Query<TQueryFnData, TError, TData, readonly unknown[]>
166 | undefined;
167```
168​
169Defined in: [packages/vue-query/src/queryCache.ts:17](https://github.com/TanStack/query/blob/main/packages/vue-query/src/queryCache.ts#L17)
170​
171A slightly more advanced method that can be used to get an existing query instance from the
172cache. This instance not only contains all the state for the query, but all of the instances,
173and underlying guts of the query as well. If the query does not exist, `undefined` is
174returned.
175​
176This is not typically needed for most applications, but can come in handy when needing more
177information about a query in rare scenarios (e.g. looking at `query.state.dataUpdatedAt` to
178decide whether a query is fresh enough to be used as an initial value).
179​
180#### Type Parameters
181​
182##### TQueryFnData
183​
184`TQueryFnData` = `unknown`
185​
186##### TError
187​
188`TError` = `Error`
189​
190##### TData
191​
192`TData` = `TQueryFnData`
193​
194#### Parameters
195​
196##### filters
197​
198`MaybeRefDeep`\<[`WithRequired`](../type-aliases/WithRequired.md)\<[`QueryFilters`](../interfaces/QueryFilters.md)\<readonly `unknown`[]\>, `"queryKey"`\>\>
199​
200#### Returns
201​
202 \| [`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, readonly `unknown`[]\>
203 \| `undefined`
204​
205#### See
206​
207[QueryCache#findAll](#findall)
208​
209#### Example
210​
211```ts
212const queryCache = queryClient.getQueryCache()
213​
214const query = queryCache.find({ queryKey: ['posts'] })
215```
216​
217#### Overrides
218​
219```ts
220QC.find
221```
222​
223***
224​
225### findAll()
226​
227```ts
228findAll(filters: MaybeRefDeep<QueryFilters<readonly unknown[]>>): Query<unknown, Error, unknown, readonly unknown[]>[];
229```
230​
231Defined in: [packages/vue-query/src/queryCache.ts:23](https://github.com/TanStack/query/blob/main/packages/vue-query/src/queryCache.ts#L23)
232​
233An even more advanced method that can be used to get existing query instances from the cache
234that partially match a query key. If no queries match, an empty array is returned.
235​
236This is not typically needed for most applications, but can come in handy when needing more
237information about queries in rare scenarios.
238​
239#### Parameters
240​
241##### filters
242​
243`MaybeRefDeep`\<[`QueryFilters`](../interfaces/QueryFilters.md)\<readonly `unknown`[]\>\> = `{}`
244​
245#### Returns
246​
247[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
248​
249#### See
250​
251[QueryCache#find](#find)
252​
253#### Example
254​
255```ts
256const queryCache = queryClient.getQueryCache()
257​
258const queries = queryCache.findAll({ queryKey: ['posts'] })
259```
260​
261#### Overrides
262​
263```ts
264QC.findAll
265```
266​
267***
268​
269### get()
270​
271```ts
272get<TQueryFnData, TError, TData, TQueryKey>(queryHash: string):
273 | Query<TQueryFnData, TError, TData, TQueryKey>
274 | undefined;
275```
276​
277Defined in: [packages/query-core/src/queryCache.ts:250](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L250)
278​
279Returns the `Query` instance stored under the given `queryHash`, or `undefined` if none
280exists. Unlike [QueryCache#find](#find), this looks up by the already-computed hash rather
281than by `QueryFilters`. Used by plugins (e.g. broadcast/hydration) that already have a hash
282to look up directly.
283​
284#### Type Parameters
285​
286##### TQueryFnData
287​
288`TQueryFnData` = `unknown`
289​
290##### TError
291​
292`TError` = `Error`
293​
294##### TData
295​
296`TData` = `TQueryFnData`
297​
298##### TQueryKey
299​
300`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
301​
302#### Parameters
303​
304##### queryHash
305​
306`string`
307​
308#### Returns
309​
310 \| [`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
311 \| `undefined`
312​
313#### Example
314​
315```ts
316const queryCache = queryClient.getQueryCache()
317const queryHash = hashKey(['posts'])
318​
319const query = queryCache.get(queryHash)
320```
321​
322#### Inherited from
323​
324```ts
325QC.get
326```
327​
328***
329​
330### getAll()
331​
332```ts
333getAll(): Query<unknown, Error, unknown, readonly unknown[]>[];
334```
335​
336Defined in: [packages/query-core/src/queryCache.ts:272](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L272)
337​
338Returns all queries within the cache.
339​
340#### Returns
341​
342[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
343​
344#### Example
345​
346```ts
347const queryCache = queryClient.getQueryCache()
348​
349const queries = queryCache.getAll()
350```
351​
352#### Inherited from
353​
354```ts
355QC.getAll
356```
357​
358***
359​
360### hasListeners()
361​
362```ts
363hasListeners(): boolean;
364```
365​
366Defined in: [packages/query-core/src/subscribable.ts:41](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L41)
367​
368Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
369​
370#### Returns
371​
372`boolean`
373​
374#### Inherited from
375​
376```ts
377QC.hasListeners
378```
379​
380***
381​
382### remove()
383​
384```ts
385remove(query: Query<any, any, any, any>): void;
386```
387​
388Defined in: [packages/query-core/src/queryCache.ts:208](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L208)
389​
390Destroys the given `Query` and removes it from the cache, notifying subscribers with a
391`'removed'` event. A no-op if the query is no longer the one currently stored under its hash
392(e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror
393removals across `QueryCache` instances.
394​
395#### Parameters
396​
397##### query
398​
399[`Query`](Query.md)\<`any`, `any`, `any`, `any`\>
400​
401#### Returns
402​
403`void`
404​
405#### Example
406​
407```ts
408const queryCache = queryClient.getQueryCache()
409const query = queryCache.find({ queryKey: ['posts'] })
410​
411if (query) {
412 queryCache.remove(query)
414```
415​
416#### Inherited from
417​
418```ts
419QC.remove
420```
421​
422***
423​
424### subscribe()
425​
426```ts
427subscribe(listener: QueryCacheListener): () => void;
428```
429​
430Defined in: [packages/query-core/src/subscribable.ts:27](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L27)
431​
432Registers a listener to be called on every update this object notifies about. Returns a function
433that removes the listener again β€” call it to stop listening. The base class never drops a listener
434on its own, though some subclasses clear all of theirs in `destroy()`.
435​
436#### Parameters
437​
438##### listener
439​
440`QueryCacheListener`
441​
442Called on each update, with whatever the subclass passes to its subscribers.
443​
444#### Returns
445​
446```ts
447(): void;
448```
449​
450##### Returns
451​
452`void`
453​
454#### Example
455​
456```ts
457const unsubscribe = subscribable.subscribe(() => {
458 // react to the update
459})
460​
461unsubscribe()
462```
463​
464#### Inherited from
465​
466```ts
467QC.subscribe
468```