Defined in: packages/query-core/src/query.ts:224
Represents a single cached query. A Query holds the query's key, options, state (data/error/status), and the observers currently subscribed to it.
Instances are created and managed internally by QueryCache; application code typically interacts with queries indirectly through QueryClient or a framework hook like useQuery. Direct access to a Query instance is possible via queryCache.find()/findAll() for inspecting cache state.
const queryCache = queryClient.getQueryCache()
const query = queryCache.find({ queryKey: ['posts'] })
if (query) {
console.log(query.state.dataUpdatedAt)
}TQueryFnData = unknown
TError = DefaultError
TData = TQueryFnData
TQueryKey extends QueryKey = QueryKey
new Query<TQueryFnData, TError, TData, TQueryKey>(config: QueryConfig<TQueryFnData, TError, TData, TQueryKey>): Query<TQueryFnData, TError, TData, TQueryKey>;Defined in: packages/query-core/src/query.ts:245
QueryConfig<TQueryFnData, TError, TData, TQueryKey>
Query<TQueryFnData, TError, TData, TQueryKey>
Removable.constructorgcTime: number;Defined in: packages/query-core/src/removable.ts:11
Removable.gcTimeobservers: QueryObserver<any, any, any, any, any>[];Defined in: packages/query-core/src/query.ts:241
options: QueryOptions<TQueryFnData, TError, TData, TQueryKey>;Defined in: packages/query-core/src/query.ts:232
queryHash: string;Defined in: packages/query-core/src/query.ts:231
queryKey: TQueryKey;Defined in: packages/query-core/src/query.ts:230
state: QueryState<TData, TError>;Defined in: packages/query-core/src/query.ts:233
get meta(): Record<string, unknown> | undefined;Defined in: packages/query-core/src/query.ts:264
The meta object passed in the query's options, if any.
Record<string, unknown> | undefined
The query's meta, or undefined if none was set.
get promise(): Promise<TData> | undefined;Defined in: packages/query-core/src/query.ts:281
The promise for the currently in-flight fetch, if the query is fetching. undefined when the query is not fetching.
Promise<TData> | undefined
The promise of the in-flight fetch, or undefined.
cancel(options?: CancelOptions): Promise<void>;Defined in: packages/query-core/src/query.ts:364
Cancels the query's currently in-flight fetch, if any.
Set revert to restore the state from before the fetch started, and silent to suppress the cancellation error when a new fetch replaces the cancelled one.
Promise<void>
A promise that resolves once the cancellation has settled.
await query.cancel()destroy(): void;Defined in: packages/query-core/src/query.ts:376
Clears the query's garbage collection timeout and silently cancels any in-flight fetch. Called by QueryCache when the query is removed from the cache.
void
Removable.destroyfetch(options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey, never>, fetchOptions?: FetchOptions<TQueryFnData>): Promise<TData>;Defined in: packages/query-core/src/query.ts:629
Fetches the query, i.e. runs its queryFn (through any configured retryer/behavior) and updates the query's state with the result.
QueryOptions<TQueryFnData, TError, TData, TQueryKey, never>
Query options that replace the query's current options before fetching. They are not applied when an in-flight fetch is reused.
FetchOptions<TQueryFnData>
Set cancelRefetch to cancel an in-flight fetch first (only if the query already has data), and meta to pass extra information to the query's behavior.
Promise<TData>
A promise that resolves with the fetched data, or rejects with the fetch error. If the fetch is cancelled with revert while the query has data, it resolves with the restored data instead.
getObserversCount(): number;Defined in: packages/query-core/src/query.ts:593
Returns the number of observers currently subscribed to this query.
number
The number of observers.
if (query.getObserversCount() === 0) {
// no component is currently watching this query
}invalidate(): void;Defined in: packages/query-core/src/query.ts:606
Marks the query as invalidated, unless it is already invalidated. This updates state.isInvalidated and notifies observers, but does not by itself trigger a refetch.
void
query.invalidate()isActive(): boolean;Defined in: packages/query-core/src/query.ts:405
Returns true if the query has at least one observer for which enabled does not resolve to false.
boolean
true if the query has an enabled observer.
isDisabled(): boolean;Defined in: packages/query-core/src/query.ts:420
Returns true if the query is disabled, meaning it will not fetch automatically.
boolean
true if the query is disabled.
isFetched(): boolean;Defined in: packages/query-core/src/query.ts:433
Returns true if the query has been fetched, i.e. it has resolved with either data or an error at least once.
boolean
true if the query has been fetched.
isStale(): boolean;Defined in: packages/query-core/src/query.ts:469
Returns true if the query is stale.
boolean
true if the query is stale.
if (query.isStale()) {
// refetch or otherwise treat the cached data as outdated
}isStaleByTime(staleTime: number | "static"): boolean;Defined in: packages/query-core/src/query.ts:497
Returns true if the query's data is stale relative to the given staleTime (defaults to 0).
The time, in milliseconds, after which data is considered stale, or 'static' to never treat existing data as stale. A query without data is stale either way.
number | "static"
boolean
true if the query's data is stale.
const isStale = query.isStaleByTime(1000 * 60)isStatic(): boolean;Defined in: packages/query-core/src/query.ts:442
Returns true if the query has at least one observer configured with staleTime: 'static', meaning it is treated as never stale.
boolean
true if the query is static.
reset(): void;Defined in: packages/query-core/src/query.ts:395
Resets the query back to its initial state (the state it had when it was first created, e.g. any initialData), destroying it first to cancel any in-flight fetch.
void
setState(state: Partial<QueryState<TData, TError>>): void;Defined in: packages/query-core/src/query.ts:348
Merges the given partial state directly into this query's state, notifying observers. Used by persistence and broadcast plugins to restore a state snapshot, and by devtools to let a user manually trigger a loading/error state or edit the cached data.
Partial<QueryState<TData, TError>>
The partial state to merge into the query's state.
void