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

Query.md

499 lines10.1 KB

1---
2id: Query
3title: Query
4---
5​
6Defined in: [packages/query-core/src/query.ts:225](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L225)
7​
8Represents a single cached query. A `Query` holds the query's key, options,
9state (data/error/status), and the observers currently subscribed to it.
10​
11Instances are created and managed internally by `QueryCache`; application
12code typically interacts with queries indirectly through `QueryClient` or
13a framework hook like `useQuery`. Direct access to a `Query` instance is
14possible via `queryCache.find()`/`findAll()` for inspecting cache state.
15​
16## Example
17​
18```ts
19const queryCache = queryClient.getQueryCache()
20const query = queryCache.find({ queryKey: ['posts'] })
21​
22if (query) {
23 console.log(query.state.dataUpdatedAt)
25```
26​
27## Extends
28​
29- `Removable`
30​
31## Type Parameters
32​
33### TQueryFnData
34​
35`TQueryFnData` = `unknown`
36​
37### TError
38​
39`TError` = [`DefaultError`](../type-aliases/DefaultError.md)
40​
41### TData
42​
43`TData` = `TQueryFnData`
44​
45### TQueryKey
46​
47`TQueryKey` *extends* [`QueryKey`](../type-aliases/QueryKey.md) = [`QueryKey`](../type-aliases/QueryKey.md)
48​
49## Constructors
50​
51### Constructor
52​
53```ts
54new Query<TQueryFnData, TError, TData, TQueryKey>(config: QueryConfig<TQueryFnData, TError, TData, TQueryKey>): Query<TQueryFnData, TError, TData, TQueryKey>;
55```
56​
57Defined in: [packages/query-core/src/query.ts:246](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L246)
58​
59#### Parameters
60​
61##### config
62​
63`QueryConfig`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
64​
65#### Returns
66​
67`Query`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
68​
69#### Overrides
70​
71```ts
72Removable.constructor
73```
74​
75## Properties
76​
77### gcTime
78​
79```ts
80gcTime: number;
81```
82​
83Defined in: [packages/query-core/src/removable.ts:11](https://github.com/TanStack/query/blob/main/packages/query-core/src/removable.ts#L11)
84​
85#### Inherited from
86​
87```ts
88Removable.gcTime
89```
90​
91***
92​
93### observers
94​
95```ts
96observers: QueryObserver<any, any, any, any, any>[];
97```
98​
99Defined in: [packages/query-core/src/query.ts:242](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L242)
100​
101***
102​
103### options
104​
105```ts
106options: QueryOptions<TQueryFnData, TError, TData, TQueryKey>;
107```
108​
109Defined in: [packages/query-core/src/query.ts:233](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L233)
110​
111***
112​
113### queryHash
114​
115```ts
116queryHash: string;
117```
118​
119Defined in: [packages/query-core/src/query.ts:232](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L232)
120​
121***
122​
123### queryKey
124​
125```ts
126queryKey: TQueryKey;
127```
128​
129Defined in: [packages/query-core/src/query.ts:231](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L231)
130​
131***
132​
133### state
134​
135```ts
136state: QueryState<TData, TError>;
137```
138​
139Defined in: [packages/query-core/src/query.ts:234](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L234)
140​
141## Accessors
142​
143### meta
144​
145#### Get Signature
146​
147```ts
148get meta(): Record<string, unknown> | undefined;
149```
150​
151Defined in: [packages/query-core/src/query.ts:264](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L264)
152​
153The `meta` object passed in the query's options, if any.
154​
155##### Returns
156​
157`Record`\<`string`, `unknown`\> \| `undefined`
158​
159***
160​
161### promise
162​
163#### Get Signature
164​
165```ts
166get promise(): Promise<TData> | undefined;
167```
168​
169Defined in: [packages/query-core/src/query.ts:277](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L277)
170​
171The promise for the currently in-flight fetch, if the query is fetching.
172`undefined` when the query is not fetching.
173​
174##### Returns
175​
176`Promise`\<`TData`\> \| `undefined`
177​
178## Methods
179​
180### cancel()
181​
182```ts
183cancel(options?: CancelOptions): Promise<void>;
184```
185​
186Defined in: [packages/query-core/src/query.ts:348](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L348)
187​
188Cancels the query's currently in-flight fetch, if any.
189- Returns a promise that resolves once the cancellation has settled.
190- If no fetch is in progress, resolves immediately.
191​
192#### Parameters
193​
194##### options?
195​
196[`CancelOptions`](../interfaces/CancelOptions.md)
197​
198#### Returns
199​
200`Promise`\<`void`\>
201​
202#### Example
203​
204```ts
205await query.cancel()
206```
207​
208***
209​
210### destroy()
211​
212```ts
213destroy(): void;
214```
215​
216Defined in: [packages/query-core/src/query.ts:361](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L361)
217​
218Clears the query's garbage collection timeout and silently cancels any
219in-flight fetch. Called by `QueryCache` when the query is removed from
220the cache.
221​
222#### Returns
223​
224`void`
225​
226#### See
227​
228[Query#cancel](#cancel)
229​
230#### Overrides
231​
232```ts
233Removable.destroy
234```
235​
236***
237​
238### fetch()
239​
240```ts
241fetch(options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey, never>, fetchOptions?: FetchOptions<TQueryFnData>): Promise<TData>;
242```
243​
244Defined in: [packages/query-core/src/query.ts:590](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L590)
245​
246Fetches the query, i.e. runs its `queryFn` (through any configured
247retryer/behavior) and updates the query's state with the result.
248- If a fetch is already in flight, returns its promise instead of
249 starting a new one, unless `fetchOptions.cancelRefetch` is set and the
250 query already has data, in which case the current fetch is silently
251 cancelled first.
252- If `options` is passed, it replaces the query's current options
253 before fetching.
254​
255#### Parameters
256​
257##### options?
258​
259`QueryOptions`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `never`\>
260​
261##### fetchOptions?
262​
263`FetchOptions`\<`TQueryFnData`\>
264​
265#### Returns
266​
267`Promise`\<`TData`\>
268​
269***
270​
271### getObserversCount()
272​
273```ts
274getObserversCount(): number;
275```
276​
277Defined in: [packages/query-core/src/query.ts:560](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L560)
278​
279Returns the number of observers currently subscribed to this query.
280​
281#### Returns
282​
283`number`
284​
285#### Example
286​
287```ts
288if (query.getObserversCount() === 0) {
289 // no component is currently watching this query
291```
292​
293***
294​
295### invalidate()
296​
297```ts
298invalidate(): void;
299```
300​
301Defined in: [packages/query-core/src/query.ts:574](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L574)
302​
303Marks the query as invalidated, unless it is already invalidated. This
304updates `state.isInvalidated` and notifies observers, but does not by
305itself trigger a refetch.
306​
307#### Returns
308​
309`void`
310​
311#### Example
312​
313```ts
314query.invalidate()
315```
316​
317***
318​
319### isActive()
320​
321```ts
322isActive(): boolean;
323```
324​
325Defined in: [packages/query-core/src/query.ts:386](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L386)
326​
327Returns `true` if the query has at least one observer for which `enabled`
328does not resolve to `false`.
329​
330#### Returns
331​
332`boolean`
333​
334***
335​
336### isDisabled()
337​
338```ts
339isDisabled(): boolean;
340```
341​
342Defined in: [packages/query-core/src/query.ts:400](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L400)
343​
344Returns `true` if the query is disabled, meaning it will not fetch
345automatically.
346- If the query has observers, it is disabled when none of them are active
347 (see `isActive`).
348- If the query has no observers, it is disabled when its `queryFn` is
349 `skipToken` or it has never been fetched.
350​
351#### Returns
352​
353`boolean`
354​
355***
356​
357### isFetched()
358​
359```ts
360isFetched(): boolean;
361```
362​
363Defined in: [packages/query-core/src/query.ts:412](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L412)
364​
365Returns `true` if the query has been fetched, i.e. it has resolved with
366either data or an error at least once.
367​
368#### Returns
369​
370`boolean`
371​
372***
373​
374### isStale()
375​
376```ts
377isStale(): boolean;
378```
379​
380Defined in: [packages/query-core/src/query.ts:447](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L447)
381​
382Returns `true` if the query is stale.
383- If the query has observers, defers to whether any observer's current
384 result reports `isStale` (which accounts for each observer's own
385 `staleTime` and `enabled` state).
386- If the query has no observers, it is considered stale when it has no
387 data or has been invalidated.
388​
389#### Returns
390​
391`boolean`
392​
393#### See
394​
395[Query#isStaleByTime](#isstalebytime)
396​
397#### Example
398​
399```ts
400if (query.isStale()) {
401 // refetch or otherwise treat the cached data as outdated
403```
404​
405***
406​
407### isStaleByTime()
408​
409```ts
410isStaleByTime(staleTime: number | "static"): boolean;
411```
412​
413Defined in: [packages/query-core/src/query.ts:473](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L473)
414​
415Returns `true` if the query's data is stale relative to the given
416`staleTime` (defaults to `0`).
417- A query with no data is always stale.
418- `staleTime: 'static'` is never stale.
419- An invalidated query is always stale.
420- Otherwise, staleness is based on elapsed time since `dataUpdatedAt`.
421​
422#### Parameters
423​
424##### staleTime
425​
426`number` | `"static"`
427​
428#### Returns
429​
430`boolean`
431​
432#### See
433​
434[Query#isStale](#isstale)
435​
436#### Example
437​
438```ts
439const isStale = query.isStaleByTime(1000 * 60)
440```
441​
442***
443​
444### isStatic()
445​
446```ts
447isStatic(): boolean;
448```
449​
450Defined in: [packages/query-core/src/query.ts:420](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L420)
451​
452Returns `true` if the query has at least one observer configured with
453`staleTime: 'static'`, meaning it is treated as never stale.
454​
455#### Returns
456​
457`boolean`
458​
459***
460​
461### reset()
462​
463```ts
464reset(): void;
465```
466​
467Defined in: [packages/query-core/src/query.ts:377](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L377)
468​
469Resets the query back to its initial state (the state it had when it was
470first created, e.g. any `initialData`), destroying it first to cancel any
471in-flight fetch.
472​
473#### Returns
474​
475`void`
476​
477***
478​
479### setState()
480​
481```ts
482setState(state: Partial<QueryState<TData, TError>>): void;
483```
484​
485Defined in: [packages/query-core/src/query.ts:334](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L334)
486​
487Merges the given partial state directly into this query's state, notifying observers. Used
488by persistence and broadcast plugins to restore a state snapshot, and by devtools to let a
489user manually trigger a loading/error state or edit the cached data.
490​
491#### Parameters
492​
493##### state
494​
495`Partial`\<[`QueryState`](../interfaces/QueryState.md)\<`TData`, `TError`\>\>
496​
497#### Returns
498​
499`void`