Open History to see the last commit
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)
24}
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
290}
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
402}
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`