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

QueryCache.md

433 lines9.3 KB

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