snapshot of https://github.com/TanStack/query.git (main, 2026-09-16; blobs > 131072B omitted)
Some checks failed
autofix.ci / autofix (push) Failing after 4s
Release / Release (push) Has been skipped
GitHub Actions Security Analysis / Run zizmor (push) Failing after 2s

This commit is contained in:
upstream-mirror
2026-09-16 01:13:08 +05:00
commit 3a013ccdfe
3570 changed files with 490194 additions and 0 deletions

View File

@@ -0,0 +1,273 @@
---
id: CancelledError
title: CancelledError
---
Defined in: [packages/query-core/src/retryer.ts:77](https://github.com/TanStack/query/blob/main/packages/query-core/src/retryer.ts#L77)
The error thrown by a `Retryer` (and surfaced to `query.promise`/`mutation`) when a fetch is cancelled, e.g. via
`query.cancel()`. `revert`, if `true`, tells the caller to restore the state the query was in before the fetch
started instead of surfacing the error. `silent`, if `true`, tells the caller to suppress this error and instead
resolve with the promise of the fetch that triggered the cancellation.
## Example
```ts
query.cancel()
try {
await query.promise
} catch (error) {
if (error instanceof CancelledError) {
// the fetch was cancelled, e.g. via `query.cancel()`
}
}
```
## Extends
- `Error`
## Constructors
### Constructor
```ts
new CancelledError(options?): CancelledError;
```
Defined in: [packages/query-core/src/retryer.ts:80](https://github.com/TanStack/query/blob/main/packages/query-core/src/retryer.ts#L80)
#### Parameters
##### options?
[`CancelOptions`](../interfaces/CancelOptions.md)
#### Returns
`CancelledError`
#### Overrides
```ts
Error.constructor
```
## Properties
### cause?
```ts
optional cause: unknown;
```
Defined in: node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es2022.error.d.ts:24
#### Inherited from
```ts
Error.cause
```
***
### message
```ts
message: string;
```
Defined in: node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1075
#### Inherited from
```ts
Error.message
```
***
### name
```ts
name: string;
```
Defined in: node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1074
#### Inherited from
```ts
Error.name
```
***
### revert?
```ts
optional revert: boolean;
```
Defined in: [packages/query-core/src/retryer.ts:78](https://github.com/TanStack/query/blob/main/packages/query-core/src/retryer.ts#L78)
***
### silent?
```ts
optional silent: boolean;
```
Defined in: [packages/query-core/src/retryer.ts:79](https://github.com/TanStack/query/blob/main/packages/query-core/src/retryer.ts#L79)
***
### stack?
```ts
optional stack: string;
```
Defined in: node\_modules/.pnpm/typescript@6.0.3/node\_modules/typescript/lib/lib.es5.d.ts:1076
#### Inherited from
```ts
Error.stack
```
***
### stackTraceLimit
```ts
static stackTraceLimit: number;
```
Defined in: node\_modules/.pnpm/@types+node@22.19.15/node\_modules/@types/node/globals.d.ts:68
The `Error.stackTraceLimit` property specifies the number of stack frames
collected by a stack trace (whether generated by `new Error().stack` or
`Error.captureStackTrace(obj)`).
The default value is `10` but may be set to any valid JavaScript number. Changes
will affect any stack trace captured _after_ the value has been changed.
If set to a non-number value, or set to a negative number, stack traces will
not capture any frames.
#### Inherited from
```ts
Error.stackTraceLimit
```
## Methods
### captureStackTrace()
```ts
static captureStackTrace(targetObject, constructorOpt?): void;
```
Defined in: node\_modules/.pnpm/@types+node@22.19.15/node\_modules/@types/node/globals.d.ts:52
Creates a `.stack` property on `targetObject`, which when accessed returns
a string representing the location in the code at which
`Error.captureStackTrace()` was called.
```js
const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack; // Similar to `new Error().stack`
```
The first line of the trace will be prefixed with
`${myObject.name}: ${myObject.message}`.
The optional `constructorOpt` argument accepts a function. If given, all frames
above `constructorOpt`, including `constructorOpt`, will be omitted from the
generated stack trace.
The `constructorOpt` argument is useful for hiding implementation
details of error generation from the user. For instance:
```js
function a() {
b();
}
function b() {
c();
}
function c() {
// Create an error without stack trace to avoid calculating the stack trace twice.
const { stackTraceLimit } = Error;
Error.stackTraceLimit = 0;
const error = new Error();
Error.stackTraceLimit = stackTraceLimit;
// Capture the stack trace above function b
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
throw error;
}
a();
```
#### Parameters
##### targetObject
`object`
##### constructorOpt?
`Function`
#### Returns
`void`
#### Inherited from
```ts
Error.captureStackTrace
```
***
### prepareStackTrace()
```ts
static prepareStackTrace(err, stackTraces): any;
```
Defined in: node\_modules/.pnpm/@types+node@22.19.15/node\_modules/@types/node/globals.d.ts:56
#### Parameters
##### err
`Error`
##### stackTraces
`CallSite`[]
#### Returns
`any`
#### See
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
#### Inherited from
```ts
Error.prepareStackTrace
```

View File

@@ -0,0 +1,665 @@
---
id: InfiniteQueryObserver
title: InfiniteQueryObserver
---
Defined in: [packages/query-core/src/infiniteQueryObserver.ts:41](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L41)
An `InfiniteQueryObserver` extends `QueryObserver` to observe and switch
between infinite queries. It augments the base `QueryObserverResult` with
infinite-query-specific fields and methods, such as `hasNextPage` and
`fetchNextPage`, and is the primitive that framework adapters (e.g.
`useInfiniteQuery`) build their hooks on top of.
## Example
```ts
const observer = new InfiniteQueryObserver(queryClient, {
queryKey: ['projects'],
queryFn: ({ pageParam }) => fetchProjects(pageParam),
initialPageParam: 0,
getNextPageParam: (lastPage) => lastPage.nextCursor,
})
const unsubscribe = observer.subscribe((result) => console.log(result))
```
## Extends
- [`QueryObserver`](QueryObserver.md)\<`TQueryFnData`, `TError`, `TData`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>
## Type Parameters
### TQueryFnData
`TQueryFnData` = `unknown`
### TError
`TError` = [`DefaultError`](../type-aliases/DefaultError.md)
### TData
`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`\>
### TQueryKey
`TQueryKey` *extends* [`QueryKey`](../type-aliases/QueryKey.md) = [`QueryKey`](../type-aliases/QueryKey.md)
### TPageParam
`TPageParam` = `unknown`
## Constructors
### Constructor
```ts
new InfiniteQueryObserver<TQueryFnData, TError, TData, TQueryKey, TPageParam>(client, options): InfiniteQueryObserver<TQueryFnData, TError, TData, TQueryKey, TPageParam>;
```
Defined in: [packages/query-core/src/infiniteQueryObserver.ts:83](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L83)
#### Parameters
##### client
`QueryClient`
##### options
[`InfiniteQueryObserverOptions`](../interfaces/InfiniteQueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
#### Returns
`InfiniteQueryObserver`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
#### Overrides
[`QueryObserver`](QueryObserver.md).[`constructor`](QueryObserver.md#constructor)
## Properties
### fetch
```ts
protected fetch: ReplaceReturnType<(fetchOptions) => Promise<QueryObserverResult<TData, TError>>, Promise<InfiniteQueryObserverResult<TData, TError>>>;
```
Defined in: [packages/query-core/src/infiniteQueryObserver.ts:72](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L72)
#### Overrides
```ts
QueryObserver.fetch
```
***
### getCurrentResult
```ts
getCurrentResult: ReplaceReturnType<() => QueryObserverResult<TData, TError>, InfiniteQueryObserverResult<TData, TError>>;
```
Defined in: [packages/query-core/src/infiniteQueryObserver.ts:60](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L60)
Returns the most recently computed `QueryObserverResult` for the
observed query. This is a point-in-time read; to be notified of updates
as they happen, subscribe to the observer instead (its inherited
`subscribe` method).
#### Example
```ts
const result = observer.getCurrentResult()
console.log(result.status, result.data)
```
#### Overrides
```ts
QueryObserver.getCurrentResult
```
***
### listeners
```ts
protected listeners: Set<QueryObserverListener<TData, TError>>;
```
Defined in: [packages/query-core/src/subscribable.ts:2](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L2)
#### Inherited from
[`QueryObserver`](QueryObserver.md).[`listeners`](QueryObserver.md#listeners)
***
### options
```ts
options: QueryObserverOptions<TQueryFnData, TError, TData, InfiniteData<TQueryFnData, TPageParam>, TQueryKey>;
```
Defined in: [packages/query-core/src/queryObserver.ts:89](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L89)
#### Inherited from
[`QueryObserver`](QueryObserver.md).[`options`](QueryObserver.md#options)
***
### subscribe()
```ts
subscribe: (listener) => () => void;
```
Defined in: [packages/query-core/src/infiniteQueryObserver.ts:55](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L55)
#### Parameters
##### listener
`InfiniteQueryObserverListener`
#### Returns
```ts
(): void;
```
##### Returns
`void`
#### Overrides
```ts
QueryObserver.subscribe
```
## Methods
### bindMethods()
```ts
protected bindMethods(): void;
```
Defined in: [packages/query-core/src/infiniteQueryObserver.ts:96](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L96)
#### Returns
`void`
#### Overrides
[`QueryObserver`](QueryObserver.md).[`bindMethods`](QueryObserver.md#bindmethods)
***
### createResult()
```ts
protected createResult(query, options): InfiniteQueryObserverResult<TData, TError>;
```
Defined in: [packages/query-core/src/infiniteQueryObserver.ts:201](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L201)
#### Parameters
##### query
[`Query`](Query.md)\<`TQueryFnData`, `TError`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>
##### options
[`InfiniteQueryObserverOptions`](../interfaces/InfiniteQueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
#### Returns
[`InfiniteQueryObserverResult`](../type-aliases/InfiniteQueryObserverResult.md)\<`TData`, `TError`\>
#### Overrides
[`QueryObserver`](QueryObserver.md).[`createResult`](QueryObserver.md#createresult)
***
### destroy()
```ts
destroy(): void;
```
Defined in: [packages/query-core/src/queryObserver.ts:161](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L161)
Stops observing the current query: clears all listeners, cancels the
stale and refetch-interval timers, and removes this observer from the
query it was observing.
#### Returns
`void`
#### Inherited from
[`QueryObserver`](QueryObserver.md).[`destroy`](QueryObserver.md#destroy)
***
### fetchNextPage()
```ts
fetchNextPage(options?): Promise<InfiniteQueryObserverResult<TData, TError>>;
```
Defined in: [packages/query-core/src/infiniteQueryObserver.ts:161](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L161)
Fetches the next page of the infinite query and returns a promise that
resolves with the resulting `InfiniteQueryObserverResult`. The page
param used for the fetch is determined by `getNextPageParam`, which
receives the current pages/page params and whose result also determines
`hasNextPage`.
#### Parameters
##### options?
[`FetchNextPageOptions`](../interfaces/FetchNextPageOptions.md)
#### Returns
`Promise`\<[`InfiniteQueryObserverResult`](../type-aliases/InfiniteQueryObserverResult.md)\<`TData`, `TError`\>\>
#### Example
```ts
const { hasNextPage } = observer.getCurrentResult()
if (hasNextPage) {
await observer.fetchNextPage()
}
```
#### See
[InfiniteQueryObserver#fetchPreviousPage](#fetchpreviouspage)
***
### fetchOptimistic()
```ts
fetchOptimistic(options): Promise<QueryObserverResult<TData, TError>>;
```
Defined in: [packages/query-core/src/queryObserver.ts:395](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L395)
Fetches a query defined by the given options without affecting this
observer's own tracked query or result, and returns a promise that
resolves with the `QueryObserverResult` for that fetch. This is useful
for prefetching data that another observer (e.g. a query about to be
navigated to) will need, ahead of time.
#### Parameters
##### options
[`QueryObserverOptions`](../interfaces/QueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>
#### Returns
`Promise`\<[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>\>
#### Example
```ts
const result = await observer.fetchOptimistic({
queryKey: ['posts', 2],
queryFn: () => fetchPost(2),
})
console.log(result.data)
```
#### Inherited from
[`QueryObserver`](QueryObserver.md).[`fetchOptimistic`](QueryObserver.md#fetchoptimistic)
***
### fetchPreviousPage()
```ts
fetchPreviousPage(options?): Promise<InfiniteQueryObserverResult<TData, TError>>;
```
Defined in: [packages/query-core/src/infiniteQueryObserver.ts:190](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L190)
Fetches the previous page of the infinite query and returns a promise
that resolves with the resulting `InfiniteQueryObserverResult`. The page
param used for the fetch is determined by `getPreviousPageParam`, which
receives the current pages/page params and whose result also determines
`hasPreviousPage`.
#### Parameters
##### options?
[`FetchPreviousPageOptions`](../interfaces/FetchPreviousPageOptions.md)
#### Returns
`Promise`\<[`InfiniteQueryObserverResult`](../type-aliases/InfiniteQueryObserverResult.md)\<`TData`, `TError`\>\>
#### Example
```ts
const { hasPreviousPage } = observer.getCurrentResult()
if (hasPreviousPage) {
await observer.fetchPreviousPage()
}
```
#### See
[InfiniteQueryObserver#fetchNextPage](#fetchnextpage)
***
### getCurrentQuery()
```ts
getCurrentQuery(): Query<TQueryFnData, TError, InfiniteData<TQueryFnData, TPageParam>, TQueryKey>;
```
Defined in: [packages/query-core/src/queryObserver.ts:357](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L357)
Returns the `Query` instance this observer is currently observing.
#### Returns
[`Query`](Query.md)\<`TQueryFnData`, `TError`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>
#### Inherited from
[`QueryObserver`](QueryObserver.md).[`getCurrentQuery`](QueryObserver.md#getcurrentquery)
***
### getOptimisticResult()
```ts
getOptimisticResult(options): InfiniteQueryObserverResult<TData, TError>;
```
Defined in: [packages/query-core/src/infiniteQueryObserver.ts:127](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L127)
The infinite-query counterpart of [QueryObserver#getOptimisticResult](QueryObserver.md#getoptimisticresult), marking the
options as an infinite query before delegating to it. Called by framework adapters (e.g.
`useInfiniteQuery`) ahead of subscribing, to compute the current `InfiniteQueryObserverResult`
synchronously.
#### Parameters
##### options
[`DefaultedInfiniteQueryObserverOptions`](../type-aliases/DefaultedInfiniteQueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
#### Returns
[`InfiniteQueryObserverResult`](../type-aliases/InfiniteQueryObserverResult.md)\<`TData`, `TError`\>
#### Overrides
[`QueryObserver`](QueryObserver.md).[`getOptimisticResult`](QueryObserver.md#getoptimisticresult)
***
### hasListeners()
```ts
hasListeners(): boolean;
```
Defined in: [packages/query-core/src/subscribable.ts:19](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L19)
#### Returns
`boolean`
#### Inherited from
[`QueryObserver`](QueryObserver.md).[`hasListeners`](QueryObserver.md#haslisteners)
***
### onSubscribe()
```ts
protected onSubscribe(): void;
```
Defined in: [packages/query-core/src/queryObserver.ts:110](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L110)
#### Returns
`void`
#### Inherited from
[`QueryObserver`](QueryObserver.md).[`onSubscribe`](QueryObserver.md#onsubscribe)
***
### onUnsubscribe()
```ts
protected onUnsubscribe(): void;
```
Defined in: [packages/query-core/src/queryObserver.ts:124](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L124)
#### Returns
`void`
#### Inherited from
[`QueryObserver`](QueryObserver.md).[`onUnsubscribe`](QueryObserver.md#onunsubscribe)
***
### refetch()
```ts
refetch(__namedParameters): Promise<QueryObserverResult<TData, TError>>;
```
Defined in: [packages/query-core/src/queryObserver.ts:371](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L371)
Refetches the observed query and returns a promise that resolves with
the resulting `QueryObserverResult`.
#### Parameters
##### \_\_namedParameters
[`RefetchOptions`](../interfaces/RefetchOptions.md) = `{}`
#### Returns
`Promise`\<[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>\>
#### Example
```ts
const result = await observer.refetch({ cancelRefetch: false })
console.log(result.data)
```
#### Inherited from
[`QueryObserver`](QueryObserver.md).[`refetch`](QueryObserver.md#refetch)
***
### setOptions()
```ts
setOptions(options): void;
```
Defined in: [packages/query-core/src/infiniteQueryObserver.ts:108](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L108)
Updates the observer's options. Behaves the same as
`QueryObserver.setOptions`, additionally marking the options as
belonging to an infinite query before delegating to the base
implementation.
#### Parameters
##### options
[`InfiniteQueryObserverOptions`](../interfaces/InfiniteQueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
#### Returns
`void`
#### Overrides
[`QueryObserver`](QueryObserver.md).[`setOptions`](QueryObserver.md#setoptions)
***
### shouldFetchOnReconnect()
```ts
shouldFetchOnReconnect(): boolean;
```
Defined in: [packages/query-core/src/queryObserver.ts:135](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L135)
Returns whether the observed query is currently stale and configured
(via the `refetchOnReconnect` option) to refetch when the network
reconnects.
#### Returns
`boolean`
#### Inherited from
[`QueryObserver`](QueryObserver.md).[`shouldFetchOnReconnect`](QueryObserver.md#shouldfetchonreconnect)
***
### shouldFetchOnWindowFocus()
```ts
shouldFetchOnWindowFocus(): boolean;
```
Defined in: [packages/query-core/src/queryObserver.ts:148](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L148)
Returns whether the observed query is currently stale and configured
(via the `refetchOnWindowFocus` option) to refetch when the window
regains focus.
#### Returns
`boolean`
#### Inherited from
[`QueryObserver`](QueryObserver.md).[`shouldFetchOnWindowFocus`](QueryObserver.md#shouldfetchonwindowfocus)
***
### trackProp()
```ts
trackProp(key): void;
```
Defined in: [packages/query-core/src/queryObserver.ts:350](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L350)
Records that the given `QueryObserverResult` property was read, so a subsequent update only
notifies this observer if a tracked property actually changed. Normally called indirectly via
[QueryObserver#trackResult](QueryObserver.md#trackresult)'s proxy; exposed directly for adapters that track property
access themselves (e.g. through their own reactivity system) instead of via the proxy.
#### Parameters
##### key
`"error"` | `"data"` | `"isError"` | `"isPending"` | `"isLoading"` | `"isLoadingError"` | `"isRefetchError"` | `"isSuccess"` | `"isPlaceholderData"` | `"status"` | `"dataUpdatedAt"` | `"errorUpdatedAt"` | `"failureCount"` | `"failureReason"` | `"errorUpdateCount"` | `"isFetched"` | `"isFetchedAfterMount"` | `"isFetching"` | `"isInitialLoading"` | `"isPaused"` | `"isRefetching"` | `"isStale"` | `"isEnabled"` | `"refetch"` | `"fetchStatus"`
#### Returns
`void`
#### Inherited from
[`QueryObserver`](QueryObserver.md).[`trackProp`](QueryObserver.md#trackprop)
***
### trackResult()
```ts
trackResult(result, onPropTracked?): QueryObserverResult<TData, TError>;
```
Defined in: [packages/query-core/src/queryObserver.ts:331](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L331)
Wraps a `QueryObserverResult` in a `Proxy` that records which properties are read, via
[QueryObserver#trackProp](QueryObserver.md#trackprop) (and an optional `onPropTracked` callback). Used by framework
adapters when `notifyOnChangeProps` is not set, to implement its default "only re-render on
properties you actually read" behavior.
#### Parameters
##### result
[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>
##### onPropTracked?
(`key`) => `void`
#### Returns
[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>
#### Inherited from
[`QueryObserver`](QueryObserver.md).[`trackResult`](QueryObserver.md#trackresult)
***
### updateResult()
```ts
updateResult(): void;
```
Defined in: [packages/query-core/src/queryObserver.ts:735](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L735)
Recomputes and stores the current result from the current query/options, notifying listeners
if it changed. Framework adapters call this right after subscribing to make sure no query
update was missed in the gap between creating the observer and subscribing to it.
#### Returns
`void`
#### Inherited from
[`QueryObserver`](QueryObserver.md).[`updateResult`](QueryObserver.md#updateresult)

View File

@@ -0,0 +1,330 @@
---
id: Mutation
title: Mutation
---
Defined in: [packages/query-core/src/mutation.ts:135](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L135)
Represents a single mutation attempt. A `Mutation` holds the mutation's
options, state (data/error/status), and the `MutationObserver`s currently
subscribed to it.
Instances are created and managed internally by `MutationCache`; application
code typically interacts with mutations indirectly through `QueryClient` or
a framework hook like `useMutation`. Direct access to a `Mutation` instance
is possible via `mutationCache.find()`/`getAll()` for inspecting cache state.
## Example
```ts
const mutationCache = queryClient.getMutationCache()
const mutation = mutationCache.find({ mutationKey: ['addPost'] })
```
## Extends
- `Removable`
## Type Parameters
### TData
`TData` = `unknown`
### TError
`TError` = [`DefaultError`](../type-aliases/DefaultError.md)
### TVariables
`TVariables` = `unknown`
### TOnMutateResult
`TOnMutateResult` = `unknown`
## Constructors
### Constructor
```ts
new Mutation<TData, TError, TVariables, TOnMutateResult>(config): Mutation<TData, TError, TVariables, TOnMutateResult>;
```
Defined in: [packages/query-core/src/mutation.ts:152](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L152)
#### Parameters
##### config
`MutationConfig`\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
#### Returns
`Mutation`\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
#### Overrides
```ts
Removable.constructor
```
## Properties
### gcTime
```ts
gcTime: number;
```
Defined in: [packages/query-core/src/removable.ts:7](https://github.com/TanStack/query/blob/main/packages/query-core/src/removable.ts#L7)
#### Inherited from
```ts
Removable.gcTime
```
***
### mutationId
```ts
readonly mutationId: number;
```
Defined in: [packages/query-core/src/mutation.ts:143](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L143)
***
### options
```ts
options: MutationOptions<TData, TError, TVariables, TOnMutateResult>;
```
Defined in: [packages/query-core/src/mutation.ts:142](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L142)
***
### state
```ts
state: MutationState<TData, TError, TVariables, TOnMutateResult>;
```
Defined in: [packages/query-core/src/mutation.ts:141](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L141)
## Accessors
### meta
#### Get Signature
```ts
get meta(): Record<string, unknown> | undefined;
```
Defined in: [packages/query-core/src/mutation.ts:179](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L179)
The `meta` object passed in the mutation's options, if any.
##### Returns
`Record`\<`string`, `unknown`\> \| `undefined`
## Methods
### clearGcTimeout()
```ts
protected clearGcTimeout(): void;
```
Defined in: [packages/query-core/src/removable.ts:32](https://github.com/TanStack/query/blob/main/packages/query-core/src/removable.ts#L32)
#### Returns
`void`
#### Inherited from
```ts
Removable.clearGcTimeout
```
***
### continue()
```ts
continue(): Promise<unknown>;
```
Defined in: [packages/query-core/src/mutation.ts:243](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L243)
Resumes a mutation that is currently paused or was restored from a
dehydrated, still-`pending` state.
- If this mutation has an active retryer (it paused mid-attempt, e.g. due
to the network mode or scope-based queuing), its retryer is resumed.
- Otherwise, if the mutation's status is still `pending` (e.g. it was
dehydrated while an attempt was in flight and never got a retryer in
this instance), `execute` is called again with the last known variables.
- Otherwise the mutation has already settled and this resolves immediately
without running anything again.
#### Returns
`Promise`\<`unknown`\>
#### Example
```ts
// typically driven by reconnect handling, e.g. queryClient.resumePausedMutations()
const mutation = mutationCache.find({ mutationKey: ['addPost'] })
await mutation?.continue()
```
#### See
[Mutation#execute](#execute)
***
### destroy()
```ts
destroy(): void;
```
Defined in: [packages/query-core/src/removable.ts:10](https://github.com/TanStack/query/blob/main/packages/query-core/src/removable.ts#L10)
#### Returns
`void`
#### Inherited from
```ts
Removable.destroy
```
***
### execute()
```ts
execute(variables): Promise<TData>;
```
Defined in: [packages/query-core/src/mutation.ts:284](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L284)
Runs the mutation function for the given variables through a retryer, and
drives the mutation's state and lifecycle callbacks through to settlement.
If this mutation's state is already `pending` when `execute` is called
(i.e. it was restored, still in-flight, from a dehydrated state), the
`onMutate` step is skipped and a `continue` action is dispatched to
unpause it; otherwise a `pending` action is dispatched first, then the
mutation cache's `onMutate` and the mutation's own `onMutate` option are
awaited in that order, and the resulting context is stored.
The mutation function is then run (subject to `retry`/`retryDelay`/
`networkMode`, and to the mutation cache's scope-based serialization).
On success, the cache's `onSuccess`/`onSettled` callbacks run before the
mutation's own `onSuccess`/`onSettled` options, a `success` action is
dispatched, and the resolved data is returned. On failure, the same
cache-then-option ordering is used for `onError`/`onSettled`, but each of
those four callbacks is individually caught so that a throwing callback
cannot mask the original error; an `error` action is then dispatched and
the original error is re-thrown.
#### Parameters
##### variables
`TVariables`
#### Returns
`Promise`\<`TData`\>
#### Example
```ts
// Called internally by `MutationObserver.mutate` and `Mutation.continue` —
// applications normally trigger mutations through those, not this method.
const data = await mutation.execute(variables)
```
#### See
[Mutation#continue](#continue)
***
### optionalRemove()
```ts
protected optionalRemove(): void;
```
Defined in: [packages/query-core/src/mutation.ts:212](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L212)
#### Returns
`void`
#### Overrides
```ts
Removable.optionalRemove
```
***
### scheduleGc()
```ts
protected scheduleGc(): void;
```
Defined in: [packages/query-core/src/removable.ts:14](https://github.com/TanStack/query/blob/main/packages/query-core/src/removable.ts#L14)
#### Returns
`void`
#### Inherited from
```ts
Removable.scheduleGc
```
***
### updateGcTime()
```ts
protected updateGcTime(newGcTime): void;
```
Defined in: [packages/query-core/src/removable.ts:24](https://github.com/TanStack/query/blob/main/packages/query-core/src/removable.ts#L24)
#### Parameters
##### newGcTime
`number` | `undefined`
#### Returns
`void`
#### Inherited from
```ts
Removable.updateGcTime
```

View File

@@ -0,0 +1,336 @@
---
id: MutationCache
title: MutationCache
---
Defined in: [packages/vue-query/src/mutationCache.ts:15](https://github.com/TanStack/query/blob/main/packages/vue-query/src/mutationCache.ts#L15)
Vue-aware subclass of `@tanstack/query-core`'s `MutationCache`. `find`/`findAll` also accept a
MaybeRefDeep filters object, so `ref`s can be passed directly without unwrapping. Access it via
`queryClient.getMutationCache()` — `QueryClient` constructs one of these by default.
## Extends
- `MutationCache`
## Constructors
### Constructor
```ts
new MutationCache(config): MutationCache;
```
Defined in: [packages/query-core/src/mutationCache.ts:129](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L129)
#### Parameters
##### config
[`MutationCacheConfig`](../interfaces/MutationCacheConfig.md) = `{}`
#### Returns
`MutationCache`
#### Inherited from
```ts
MC.constructor
```
## Properties
### config
```ts
config: MutationCacheConfig = {};
```
Defined in: [packages/query-core/src/mutationCache.ts:129](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L129)
#### Inherited from
```ts
MC.config
```
***
### listeners
```ts
protected listeners: Set<MutationCacheListener>;
```
Defined in: [packages/query-core/src/subscribable.ts:2](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L2)
#### Inherited from
```ts
MC.listeners
```
## Methods
### clear()
```ts
clear(): void;
```
Defined in: [packages/query-core/src/mutationCache.ts:236](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L236)
Removes all mutations from the cache.
#### Returns
`void`
#### Example
```ts
const mutationCache = queryClient.getMutationCache()
mutationCache.clear()
```
#### Inherited from
```ts
MC.clear
```
***
### find()
```ts
find<TData, TError, TVariables, TOnMutateResult>(filters):
| Mutation<TData, TError, TVariables, TOnMutateResult>
| undefined;
```
Defined in: [packages/vue-query/src/mutationCache.ts:16](https://github.com/TanStack/query/blob/main/packages/vue-query/src/mutationCache.ts#L16)
A slightly more advanced method that can be used to get an existing mutation instance from
the cache. If the mutation does not exist, `undefined` is returned.
This is not typically needed for most applications, but can come in handy when needing more
information about a mutation in rare scenarios.
#### Type Parameters
##### TData
`TData` = `unknown`
##### TError
`TError` = `Error`
##### TVariables
`TVariables` = `any`
##### TOnMutateResult
`TOnMutateResult` = `unknown`
#### Parameters
##### filters
`MaybeRefDeep`\<[`MutationFilters`](../interfaces/MutationFilters.md)\<`unknown`, `Error`, `unknown`, `unknown`\>\>
#### Returns
\| [`Mutation`](Mutation.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
\| `undefined`
#### See
[MutationCache#findAll](#findall)
#### Example
```ts
const mutationCache = queryClient.getMutationCache()
const mutation = mutationCache.find({ mutationKey: ['addPost'] })
```
#### Overrides
```ts
MC.find
```
***
### findAll()
```ts
findAll(filters): Mutation<unknown, Error, unknown, unknown>[];
```
Defined in: [packages/vue-query/src/mutationCache.ts:27](https://github.com/TanStack/query/blob/main/packages/vue-query/src/mutationCache.ts#L27)
An even more advanced method that can be used to get existing mutation instances from the
cache that match the given filters. If no mutations match, an empty array is returned.
This is not typically needed for most applications, but can come in handy when needing more
information about mutations in rare scenarios.
#### Parameters
##### filters
`MaybeRefDeep`\<[`MutationFilters`](../interfaces/MutationFilters.md)\<`unknown`, `Error`, `unknown`, `unknown`\>\> = `{}`
#### Returns
[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
#### See
[MutationCache#find](#find)
#### Example
```ts
const mutationCache = queryClient.getMutationCache()
const mutations = mutationCache.findAll({ mutationKey: ['addPost'] })
```
#### Overrides
```ts
MC.findAll
```
***
### getAll()
```ts
getAll(): Mutation<unknown, Error, unknown, unknown>[];
```
Defined in: [packages/query-core/src/mutationCache.ts:259](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationCache.ts#L259)
Returns all mutations within the cache.
This is not typically needed for most applications, but can come in handy when needing more
information about a mutation in rare scenarios.
#### Returns
[`Mutation`](Mutation.md)\<`unknown`, `Error`, `unknown`, `unknown`\>[]
#### Example
```ts
const mutationCache = queryClient.getMutationCache()
const mutations = mutationCache.getAll()
```
#### Inherited from
```ts
MC.getAll
```
***
### hasListeners()
```ts
hasListeners(): boolean;
```
Defined in: [packages/query-core/src/subscribable.ts:19](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L19)
#### Returns
`boolean`
#### Inherited from
```ts
MC.hasListeners
```
***
### onSubscribe()
```ts
protected onSubscribe(): void;
```
Defined in: [packages/query-core/src/subscribable.ts:23](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L23)
#### Returns
`void`
#### Inherited from
```ts
MC.onSubscribe
```
***
### onUnsubscribe()
```ts
protected onUnsubscribe(): void;
```
Defined in: [packages/query-core/src/subscribable.ts:27](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L27)
#### Returns
`void`
#### Inherited from
```ts
MC.onUnsubscribe
```
***
### subscribe()
```ts
subscribe(listener): () => void;
```
Defined in: [packages/query-core/src/subscribable.ts:8](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L8)
#### Parameters
##### listener
`MutationCacheListener`
#### Returns
```ts
(): void;
```
##### Returns
`void`
#### Inherited from
```ts
MC.subscribe
```

View File

@@ -0,0 +1,335 @@
---
id: MutationObserver
title: MutationObserver
---
Defined in: [packages/query-core/src/mutationObserver.ts:38](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L38)
Observes a single mutation and derives a `MutationObserverResult` from it.
A framework hook like `useMutation` creates one `MutationObserver` per hook
call, keeps it stable across re-renders, calls `setOptions` when the options
passed to the hook change, subscribes to it to re-render on updates, and
reads `getCurrentResult()` for the value to return. Calling `mutate()`
builds a new underlying `Mutation` in the `MutationCache` and executes it.
## Example
```ts
const observer = new MutationObserver(queryClient, {
mutationFn: (variables: { title: string }) => addPost(variables),
})
```
## Extends
- `Subscribable`\<`MutationObserverListener`\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>\>
## Type Parameters
### TData
`TData` = `unknown`
### TError
`TError` = [`DefaultError`](../type-aliases/DefaultError.md)
### TVariables
`TVariables` = `void`
### TOnMutateResult
`TOnMutateResult` = `unknown`
## Constructors
### Constructor
```ts
new MutationObserver<TData, TError, TVariables, TOnMutateResult>(client, options): MutationObserver<TData, TError, TVariables, TOnMutateResult>;
```
Defined in: [packages/query-core/src/mutationObserver.ts:58](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L58)
#### Parameters
##### client
`QueryClient`
##### options
[`MutationObserverOptions`](../interfaces/MutationObserverOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
#### Returns
`MutationObserver`\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
#### Overrides
```ts
Subscribable<
MutationObserverListener<TData, TError, TVariables, TOnMutateResult>
>.constructor
```
## Properties
### listeners
```ts
protected listeners: Set<MutationObserverListener<TData, TError, TVariables, TOnMutateResult>>;
```
Defined in: [packages/query-core/src/subscribable.ts:2](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L2)
#### Inherited from
```ts
Subscribable.listeners
```
***
### options
```ts
options: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>;
```
Defined in: [packages/query-core/src/mutationObserver.ts:46](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L46)
## Methods
### bindMethods()
```ts
protected bindMethods(): void;
```
Defined in: [packages/query-core/src/mutationObserver.ts:75](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L75)
#### Returns
`void`
***
### getCurrentResult()
```ts
getCurrentResult(): MutationObserverResult<TData, TError, TVariables, TOnMutateResult>;
```
Defined in: [packages/query-core/src/mutationObserver.ts:155](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L155)
Returns the observer's current result, derived from the observed
mutation's state (or the default, `idle` state if no mutation has been
built yet, e.g. before the first `mutate()` call or after `reset()`).
#### Returns
[`MutationObserverResult`](../type-aliases/MutationObserverResult.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
***
### hasListeners()
```ts
hasListeners(): boolean;
```
Defined in: [packages/query-core/src/subscribable.ts:19](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L19)
#### Returns
`boolean`
#### Inherited from
```ts
Subscribable.hasListeners
```
***
### mutate()
```ts
mutate(variables, options?): Promise<TData>;
```
Defined in: [packages/query-core/src/mutationObserver.ts:207](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L207)
Builds a new `Mutation` in the `MutationCache` using the observer's
current options, detaches this observer from any previously observed
mutation, attaches it to the new one, and executes it with the given
variables.
The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are
invoked once the mutation settles, in addition to any callbacks defined
on the observer's own options.
#### Parameters
##### variables
`TVariables`
##### options?
[`MutateOptions`](../interfaces/MutateOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
#### Returns
`Promise`\<`TData`\>
#### Example
```ts
await observer.mutate(
{ title: 'New post' },
{ onSuccess: (data) => console.log(data) },
)
```
***
### onSubscribe()
```ts
protected onSubscribe(): void;
```
Defined in: [packages/query-core/src/mutationObserver.ts:127](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L127)
#### Returns
`void`
#### Overrides
```ts
Subscribable.onSubscribe
```
***
### onUnsubscribe()
```ts
protected onUnsubscribe(): void;
```
Defined in: [packages/query-core/src/mutationObserver.ts:135](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L135)
#### Returns
`void`
#### Overrides
```ts
Subscribable.onUnsubscribe
```
***
### reset()
```ts
reset(): void;
```
Defined in: [packages/query-core/src/mutationObserver.ts:180](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L180)
Detaches the observer from the mutation it is currently observing (if
any) and resets the observed result back to its default, `idle` state.
This does not cancel an in-flight mutation; the mutation itself keeps
running to completion and its own callbacks still fire, but this
observer stops reflecting its state and a subsequent `mutate()` call
will build a brand new mutation.
#### Returns
`void`
#### Example
```ts
observer.reset()
```
#### See
[MutationObserver#mutate](#mutate)
***
### setOptions()
```ts
setOptions(options): void;
```
Defined in: [packages/query-core/src/mutationObserver.ts:96](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L96)
Updates the observer's options.
If the new `mutationKey` differs from the previous one (and both were
defined), the observer is reset, detaching it from the mutation it was
observing. Otherwise, if the currently observed mutation is still
`pending`, its options are updated in place as well.
#### Parameters
##### options
[`MutationObserverOptions`](../interfaces/MutationObserverOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
#### Returns
`void`
#### Example
```ts
observer.setOptions({
mutationFn: (variables: { title: string }) => addPost(variables),
onSuccess: (data) => console.log(data),
})
```
***
### subscribe()
```ts
subscribe(listener): () => void;
```
Defined in: [packages/query-core/src/subscribable.ts:8](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L8)
#### Parameters
##### listener
`MutationObserverListener`
#### Returns
```ts
(): void;
```
##### Returns
`void`
#### Inherited from
```ts
Subscribable.subscribe
```

View File

@@ -0,0 +1,325 @@
---
id: QueriesObserver
title: QueriesObserver
---
Defined in: [packages/query-core/src/queriesObserver.ts:56](https://github.com/TanStack/query/blob/main/packages/query-core/src/queriesObserver.ts#L56)
A `QueriesObserver` watches an array of queries at once, exposing them as
a single array of `QueryObserverResult`s (or, when a `combine` option is
given, as a combined value derived from that array). It manages one
internal `QueryObserver` per query, and is the primitive that framework
adapters (e.g. `useQueries`) build their hooks on top of.
## Example
```ts
const observer = new QueriesObserver(queryClient, [
{ queryKey: ['post', 1], queryFn: fetchPost },
{ queryKey: ['post', 2], queryFn: fetchPost },
])
const unsubscribe = observer.subscribe((result) => {
console.log(result)
})
```
## Extends
- `Subscribable`\<`QueriesObserverListener`\>
## Type Parameters
### TCombinedResult
`TCombinedResult` = [`QueryObserverResult`](../type-aliases/QueryObserverResult.md)[]
## Constructors
### Constructor
```ts
new QueriesObserver<TCombinedResult>(
client,
queries,
options?): QueriesObserver<TCombinedResult>;
```
Defined in: [packages/query-core/src/queriesObserver.ts:70](https://github.com/TanStack/query/blob/main/packages/query-core/src/queriesObserver.ts#L70)
#### Parameters
##### client
`QueryClient`
##### queries
[`QueryObserverOptions`](../interfaces/QueryObserverOptions.md)\<`any`, `any`, `any`, `any`, `any`, `never`\>[]
##### options?
[`QueriesObserverOptions`](../interfaces/QueriesObserverOptions.md)\<`TCombinedResult`\>
#### Returns
`QueriesObserver`\<`TCombinedResult`\>
#### Overrides
```ts
Subscribable<QueriesObserverListener>.constructor
```
## Properties
### listeners
```ts
protected listeners: Set<QueriesObserverListener>;
```
Defined in: [packages/query-core/src/subscribable.ts:2](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L2)
#### Inherited from
```ts
Subscribable.listeners
```
## Methods
### destroy()
```ts
destroy(): void;
```
Defined in: [packages/query-core/src/queriesObserver.ts:106](https://github.com/TanStack/query/blob/main/packages/query-core/src/queriesObserver.ts#L106)
Stops observing all queries: clears all listeners and destroys every
underlying `QueryObserver` this observer manages.
#### Returns
`void`
***
### getCurrentResult()
```ts
getCurrentResult(): QueryObserverResult[];
```
Defined in: [packages/query-core/src/queriesObserver.ts:210](https://github.com/TanStack/query/blob/main/packages/query-core/src/queriesObserver.ts#L210)
Returns the most recently computed array of `QueryObserverResult`s, one
per observed query, in the same order as the queries passed to the
constructor or `setQueries`.
#### Returns
[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)[]
#### Example
```ts
const results = observer.getCurrentResult()
const data = results.map((result) => result.data)
```
***
### getObservers()
```ts
getObservers(): QueryObserver<unknown, Error, unknown, unknown, readonly unknown[]>[];
```
Defined in: [packages/query-core/src/queriesObserver.ts:227](https://github.com/TanStack/query/blob/main/packages/query-core/src/queriesObserver.ts#L227)
Returns the underlying `QueryObserver` instances this observer manages,
in the same order as the queries passed to the constructor or
`setQueries`.
#### Returns
[`QueryObserver`](QueryObserver.md)\<`unknown`, `Error`, `unknown`, `unknown`, readonly `unknown`[]\>[]
***
### getOptimisticResult()
```ts
getOptimisticResult(queries, combine): [QueryObserverResult[], (r?) => TCombinedResult, () => QueryObserverResult[]];
```
Defined in: [packages/query-core/src/queriesObserver.ts:238](https://github.com/TanStack/query/blob/main/packages/query-core/src/queriesObserver.ts#L238)
The `QueriesObserver` counterpart of [QueryObserver#getOptimisticResult](QueryObserver.md#getoptimisticresult) — computes
the result for the given (already-defaulted) queries right now, synchronously. Called by
framework adapters (e.g. `useQueries`) ahead of subscribing, returning a tuple of the raw
per-query results, a function to compute the combined result from them, and a function to
wrap the results for property-access tracking.
#### Parameters
##### queries
[`QueryObserverOptions`](../interfaces/QueryObserverOptions.md)\<`unknown`, `Error`, `unknown`, `unknown`, readonly `unknown`[], `never`\>[]
##### combine
`CombineFn`\<`TCombinedResult`\> | `undefined`
#### Returns
\[[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)[], (`r?`) => `TCombinedResult`, () => [`QueryObserverResult`](../type-aliases/QueryObserverResult.md)[]\]
***
### getQueries()
```ts
getQueries(): Query<unknown, Error, unknown, readonly unknown[]>[];
```
Defined in: [packages/query-core/src/queriesObserver.ts:218](https://github.com/TanStack/query/blob/main/packages/query-core/src/queriesObserver.ts#L218)
Returns the underlying `Query` instances currently being observed, in
the same order as the queries passed to the constructor or `setQueries`.
#### Returns
[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
***
### hasListeners()
```ts
hasListeners(): boolean;
```
Defined in: [packages/query-core/src/subscribable.ts:19](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L19)
#### Returns
`boolean`
#### Inherited from
```ts
Subscribable.hasListeners
```
***
### onSubscribe()
```ts
protected onSubscribe(): void;
```
Defined in: [packages/query-core/src/queriesObserver.ts:86](https://github.com/TanStack/query/blob/main/packages/query-core/src/queriesObserver.ts#L86)
#### Returns
`void`
#### Overrides
```ts
Subscribable.onSubscribe
```
***
### onUnsubscribe()
```ts
protected onUnsubscribe(): void;
```
Defined in: [packages/query-core/src/queriesObserver.ts:96](https://github.com/TanStack/query/blob/main/packages/query-core/src/queriesObserver.ts#L96)
#### Returns
`void`
#### Overrides
```ts
Subscribable.onUnsubscribe
```
***
### setQueries()
```ts
setQueries(queries, options?): void;
```
Defined in: [packages/query-core/src/queriesObserver.ts:127](https://github.com/TanStack/query/blob/main/packages/query-core/src/queriesObserver.ts#L127)
Replaces the set of queries being observed. Existing `QueryObserver`s
are reused for queries that match an already-observed query hash;
observers for queries that are no longer present are destroyed, and new
observers are created and subscribed to for newly added queries.
#### Parameters
##### queries
[`QueryObserverOptions`](../interfaces/QueryObserverOptions.md)\<`unknown`, `Error`, `unknown`, `unknown`, readonly `unknown`[], `never`\>[]
##### options?
[`QueriesObserverOptions`](../interfaces/QueriesObserverOptions.md)\<`TCombinedResult`\>
#### Returns
`void`
#### Example
```ts
observer.setQueries([
{ queryKey: ['post', 1], queryFn: fetchPost },
{ queryKey: ['post', 3], queryFn: fetchPost },
])
```
***
### subscribe()
```ts
subscribe(listener): () => void;
```
Defined in: [packages/query-core/src/subscribable.ts:8](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L8)
#### Parameters
##### listener
`QueriesObserverListener`
#### Returns
```ts
(): void;
```
##### Returns
`void`
#### Inherited from
```ts
Subscribable.subscribe
```

View File

@@ -0,0 +1,585 @@
---
id: Query
title: Query
---
Defined in: [packages/query-core/src/query.ts:225](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L225)
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.
## Example
```ts
const queryCache = queryClient.getQueryCache()
const query = queryCache.find({ queryKey: ['posts'] })
if (query) {
console.log(query.state.dataUpdatedAt)
}
```
## Extends
- `Removable`
## Type Parameters
### TQueryFnData
`TQueryFnData` = `unknown`
### TError
`TError` = [`DefaultError`](../type-aliases/DefaultError.md)
### TData
`TData` = `TQueryFnData`
### TQueryKey
`TQueryKey` *extends* [`QueryKey`](../type-aliases/QueryKey.md) = [`QueryKey`](../type-aliases/QueryKey.md)
## Constructors
### Constructor
```ts
new Query<TQueryFnData, TError, TData, TQueryKey>(config): Query<TQueryFnData, TError, TData, TQueryKey>;
```
Defined in: [packages/query-core/src/query.ts:246](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L246)
#### Parameters
##### config
`QueryConfig`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
#### Returns
`Query`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
#### Overrides
```ts
Removable.constructor
```
## Properties
### gcTime
```ts
gcTime: number;
```
Defined in: [packages/query-core/src/removable.ts:7](https://github.com/TanStack/query/blob/main/packages/query-core/src/removable.ts#L7)
#### Inherited from
```ts
Removable.gcTime
```
***
### observers
```ts
observers: QueryObserver<any, any, any, any, any>[];
```
Defined in: [packages/query-core/src/query.ts:242](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L242)
***
### options
```ts
options: QueryOptions<TQueryFnData, TError, TData, TQueryKey>;
```
Defined in: [packages/query-core/src/query.ts:233](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L233)
***
### queryHash
```ts
queryHash: string;
```
Defined in: [packages/query-core/src/query.ts:232](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L232)
***
### queryKey
```ts
queryKey: TQueryKey;
```
Defined in: [packages/query-core/src/query.ts:231](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L231)
***
### state
```ts
state: QueryState<TData, TError>;
```
Defined in: [packages/query-core/src/query.ts:234](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L234)
## Accessors
### meta
#### Get Signature
```ts
get meta(): Record<string, unknown> | undefined;
```
Defined in: [packages/query-core/src/query.ts:264](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L264)
The `meta` object passed in the query's options, if any.
##### Returns
`Record`\<`string`, `unknown`\> \| `undefined`
***
### promise
#### Get Signature
```ts
get promise(): Promise<TData> | undefined;
```
Defined in: [packages/query-core/src/query.ts:277](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L277)
The promise for the currently in-flight fetch, if the query is fetching.
`undefined` when the query is not fetching.
##### Returns
`Promise`\<`TData`\> \| `undefined`
## Methods
### cancel()
```ts
cancel(options?): Promise<void>;
```
Defined in: [packages/query-core/src/query.ts:348](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L348)
Cancels the query's currently in-flight fetch, if any.
- Returns a promise that resolves once the cancellation has settled.
- If no fetch is in progress, resolves immediately.
#### Parameters
##### options?
[`CancelOptions`](../interfaces/CancelOptions.md)
#### Returns
`Promise`\<`void`\>
#### Example
```ts
await query.cancel()
```
***
### clearGcTimeout()
```ts
protected clearGcTimeout(): void;
```
Defined in: [packages/query-core/src/removable.ts:32](https://github.com/TanStack/query/blob/main/packages/query-core/src/removable.ts#L32)
#### Returns
`void`
#### Inherited from
```ts
Removable.clearGcTimeout
```
***
### destroy()
```ts
destroy(): void;
```
Defined in: [packages/query-core/src/query.ts:361](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L361)
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.
#### Returns
`void`
#### See
[Query#cancel](#cancel)
#### Overrides
```ts
Removable.destroy
```
***
### fetch()
```ts
fetch(options?, fetchOptions?): Promise<TData>;
```
Defined in: [packages/query-core/src/query.ts:590](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L590)
Fetches the query, i.e. runs its `queryFn` (through any configured
retryer/behavior) and updates the query's state with the result.
- If a fetch is already in flight, returns its promise instead of
starting a new one, unless `fetchOptions.cancelRefetch` is set and the
query already has data, in which case the current fetch is silently
cancelled first.
- If `options` is passed, it replaces the query's current options
before fetching.
#### Parameters
##### options?
`QueryOptions`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `never`\>
##### fetchOptions?
`FetchOptions`\<`TQueryFnData`\>
#### Returns
`Promise`\<`TData`\>
***
### getObserversCount()
```ts
getObserversCount(): number;
```
Defined in: [packages/query-core/src/query.ts:560](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L560)
Returns the number of observers currently subscribed to this query.
#### Returns
`number`
#### Example
```ts
if (query.getObserversCount() === 0) {
// no component is currently watching this query
}
```
***
### invalidate()
```ts
invalidate(): void;
```
Defined in: [packages/query-core/src/query.ts:574](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L574)
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.
#### Returns
`void`
#### Example
```ts
query.invalidate()
```
***
### isActive()
```ts
isActive(): boolean;
```
Defined in: [packages/query-core/src/query.ts:386](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L386)
Returns `true` if the query has at least one observer for which `enabled`
does not resolve to `false`.
#### Returns
`boolean`
***
### isDisabled()
```ts
isDisabled(): boolean;
```
Defined in: [packages/query-core/src/query.ts:400](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L400)
Returns `true` if the query is disabled, meaning it will not fetch
automatically.
- If the query has observers, it is disabled when none of them are active
(see `isActive`).
- If the query has no observers, it is disabled when its `queryFn` is
`skipToken` or it has never been fetched.
#### Returns
`boolean`
***
### isFetched()
```ts
isFetched(): boolean;
```
Defined in: [packages/query-core/src/query.ts:412](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L412)
Returns `true` if the query has been fetched, i.e. it has resolved with
either data or an error at least once.
#### Returns
`boolean`
***
### isStale()
```ts
isStale(): boolean;
```
Defined in: [packages/query-core/src/query.ts:447](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L447)
Returns `true` if the query is stale.
- If the query has observers, defers to whether any observer's current
result reports `isStale` (which accounts for each observer's own
`staleTime` and `enabled` state).
- If the query has no observers, it is considered stale when it has no
data or has been invalidated.
#### Returns
`boolean`
#### See
[Query#isStaleByTime](#isstalebytime)
#### Example
```ts
if (query.isStale()) {
// refetch or otherwise treat the cached data as outdated
}
```
***
### isStaleByTime()
```ts
isStaleByTime(staleTime): boolean;
```
Defined in: [packages/query-core/src/query.ts:473](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L473)
Returns `true` if the query's data is stale relative to the given
`staleTime` (defaults to `0`).
- A query with no data is always stale.
- `staleTime: 'static'` is never stale.
- An invalidated query is always stale.
- Otherwise, staleness is based on elapsed time since `dataUpdatedAt`.
#### Parameters
##### staleTime
[`StaleTime`](../type-aliases/StaleTime.md) = `0`
#### Returns
`boolean`
#### See
[Query#isStale](#isstale)
#### Example
```ts
const isStale = query.isStaleByTime(1000 * 60)
```
***
### isStatic()
```ts
isStatic(): boolean;
```
Defined in: [packages/query-core/src/query.ts:420](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L420)
Returns `true` if the query has at least one observer configured with
`staleTime: 'static'`, meaning it is treated as never stale.
#### Returns
`boolean`
***
### optionalRemove()
```ts
protected optionalRemove(): void;
```
Defined in: [packages/query-core/src/query.ts:305](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L305)
#### Returns
`void`
#### Overrides
```ts
Removable.optionalRemove
```
***
### reset()
```ts
reset(): void;
```
Defined in: [packages/query-core/src/query.ts:377](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L377)
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.
#### Returns
`void`
***
### scheduleGc()
```ts
protected scheduleGc(): void;
```
Defined in: [packages/query-core/src/removable.ts:14](https://github.com/TanStack/query/blob/main/packages/query-core/src/removable.ts#L14)
#### Returns
`void`
#### Inherited from
```ts
Removable.scheduleGc
```
***
### setState()
```ts
setState(state): void;
```
Defined in: [packages/query-core/src/query.ts:334](https://github.com/TanStack/query/blob/main/packages/query-core/src/query.ts#L334)
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.
#### Parameters
##### state
`Partial`\<[`QueryState`](../interfaces/QueryState.md)\<`TData`, `TError`\>\>
#### Returns
`void`
***
### updateGcTime()
```ts
protected updateGcTime(newGcTime): void;
```
Defined in: [packages/query-core/src/removable.ts:24](https://github.com/TanStack/query/blob/main/packages/query-core/src/removable.ts#L24)
#### Parameters
##### newGcTime
`number` | `undefined`
#### Returns
`void`
#### Inherited from
```ts
Removable.updateGcTime
```

View File

@@ -0,0 +1,506 @@
---
id: QueryCache
title: QueryCache
---
Defined in: [packages/vue-query/src/queryCache.ts:16](https://github.com/TanStack/query/blob/main/packages/vue-query/src/queryCache.ts#L16)
Vue-aware subclass of `@tanstack/query-core`'s `QueryCache`. `find`/`findAll` also accept a
MaybeRefDeep filters object, so `ref`s can be passed directly without unwrapping. Access it via
`queryClient.getQueryCache()` — `QueryClient` constructs one of these by default.
## Extends
- `QueryCache`
## Constructors
### Constructor
```ts
new QueryCache(config): QueryCache;
```
Defined in: [packages/query-core/src/queryCache.ts:126](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L126)
#### Parameters
##### config
[`QueryCacheConfig`](../interfaces/QueryCacheConfig.md) = `{}`
#### Returns
`QueryCache`
#### Inherited from
```ts
QC.constructor
```
## Properties
### config
```ts
config: QueryCacheConfig = {};
```
Defined in: [packages/query-core/src/queryCache.ts:126](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L126)
#### Inherited from
```ts
QC.config
```
***
### listeners
```ts
protected listeners: Set<QueryCacheListener>;
```
Defined in: [packages/query-core/src/subscribable.ts:2](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L2)
#### Inherited from
```ts
QC.listeners
```
## Methods
### build()
```ts
build<TQueryFnData, TError, TData, TQueryKey>(
client,
options,
state?): Query<TQueryFnData, TError, TData, TQueryKey>;
```
Defined in: [packages/query-core/src/queryCache.ts:147](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L147)
Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or
builds and adds a new one to the cache if none exists yet. Used by framework adapters and
plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing
the reactive `QueryObserver` machinery.
#### Type Parameters
##### TQueryFnData
`TQueryFnData` = `unknown`
##### TError
`TError` = `Error`
##### TData
`TData` = `TQueryFnData`
##### TQueryKey
`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
#### Parameters
##### client
`QueryClient`
##### options
[`WithRequired`](../type-aliases/WithRequired.md)\<`QueryOptions`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `never`\>, `"queryKey"`\>
##### state?
[`QueryState`](../interfaces/QueryState.md)\<`TData`, `TError`\>
#### Returns
[`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
#### Example
```ts
const queryCache = queryClient.getQueryCache()
const query = queryCache.build(queryClient, {
queryKey: ['posts'],
queryFn: fetchPosts,
})
```
#### Inherited from
```ts
QC.build
```
***
### clear()
```ts
clear(): void;
```
Defined in: [packages/query-core/src/queryCache.ts:232](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L232)
Removes all queries from the cache.
#### Returns
`void`
#### Example
```ts
const queryCache = queryClient.getQueryCache()
queryCache.clear()
```
#### Inherited from
```ts
QC.clear
```
***
### find()
```ts
find<TQueryFnData, TError, TData>(filters):
| Query<TQueryFnData, TError, TData, readonly unknown[]>
| undefined;
```
Defined in: [packages/vue-query/src/queryCache.ts:17](https://github.com/TanStack/query/blob/main/packages/vue-query/src/queryCache.ts#L17)
A slightly more advanced method that can be used to get an existing query instance from the
cache. This instance not only contains all the state for the query, but all of the instances,
and underlying guts of the query as well. If the query does not exist, `undefined` is
returned.
This is not typically needed for most applications, but can come in handy when needing more
information about a query in rare scenarios (e.g. looking at `query.state.dataUpdatedAt` to
decide whether a query is fresh enough to be used as an initial value).
#### Type Parameters
##### TQueryFnData
`TQueryFnData` = `unknown`
##### TError
`TError` = `Error`
##### TData
`TData` = `TQueryFnData`
#### Parameters
##### filters
`MaybeRefDeep`\<[`WithRequired`](../type-aliases/WithRequired.md)\<[`QueryFilters`](../interfaces/QueryFilters.md)\<readonly `unknown`[]\>, `"queryKey"`\>\>
#### Returns
\| [`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, readonly `unknown`[]\>
\| `undefined`
#### See
[QueryCache#findAll](#findall)
#### Example
```ts
const queryCache = queryClient.getQueryCache()
const query = queryCache.find({ queryKey: ['posts'] })
```
#### Overrides
```ts
QC.find
```
***
### findAll()
```ts
findAll(filters): Query<unknown, Error, unknown, readonly unknown[]>[];
```
Defined in: [packages/vue-query/src/queryCache.ts:23](https://github.com/TanStack/query/blob/main/packages/vue-query/src/queryCache.ts#L23)
An even more advanced method that can be used to get existing query instances from the cache
that partially match a query key. If no queries match, an empty array is returned.
This is not typically needed for most applications, but can come in handy when needing more
information about queries in rare scenarios.
#### Parameters
##### filters
`MaybeRefDeep`\<[`QueryFilters`](../interfaces/QueryFilters.md)\<readonly `unknown`[]\>\> = `{}`
#### Returns
[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
#### See
[QueryCache#find](#find)
#### Example
```ts
const queryCache = queryClient.getQueryCache()
const queries = queryCache.findAll({ queryKey: ['posts'] })
```
#### Overrides
```ts
QC.findAll
```
***
### get()
```ts
get<TQueryFnData, TError, TData, TQueryKey>(queryHash):
| Query<TQueryFnData, TError, TData, TQueryKey>
| undefined;
```
Defined in: [packages/query-core/src/queryCache.ts:254](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L254)
Returns the `Query` instance stored under the given `queryHash`, or `undefined` if none
exists. Unlike [QueryCache#find](#find), this looks up by the already-computed hash rather
than by `QueryFilters`. Used by plugins (e.g. broadcast/hydration) that already have a hash
to look up directly.
#### Type Parameters
##### TQueryFnData
`TQueryFnData` = `unknown`
##### TError
`TError` = `Error`
##### TData
`TData` = `TQueryFnData`
##### TQueryKey
`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
#### Parameters
##### queryHash
`string`
#### Returns
\| [`Query`](Query.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
\| `undefined`
#### Example
```ts
const queryCache = queryClient.getQueryCache()
const queryHash = hashKey(['posts'])
const query = queryCache.get(queryHash)
```
#### Inherited from
```ts
QC.get
```
***
### getAll()
```ts
getAll(): Query<unknown, Error, unknown, readonly unknown[]>[];
```
Defined in: [packages/query-core/src/queryCache.ts:277](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L277)
Returns all queries within the cache.
#### Returns
[`Query`](Query.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[]\>[]
#### Example
```ts
const queryCache = queryClient.getQueryCache()
const queries = queryCache.getAll()
```
#### Inherited from
```ts
QC.getAll
```
***
### hasListeners()
```ts
hasListeners(): boolean;
```
Defined in: [packages/query-core/src/subscribable.ts:19](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L19)
#### Returns
`boolean`
#### Inherited from
```ts
QC.hasListeners
```
***
### onSubscribe()
```ts
protected onSubscribe(): void;
```
Defined in: [packages/query-core/src/subscribable.ts:23](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L23)
#### Returns
`void`
#### Inherited from
```ts
QC.onSubscribe
```
***
### onUnsubscribe()
```ts
protected onUnsubscribe(): void;
```
Defined in: [packages/query-core/src/subscribable.ts:27](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L27)
#### Returns
`void`
#### Inherited from
```ts
QC.onUnsubscribe
```
***
### remove()
```ts
remove(query): void;
```
Defined in: [packages/query-core/src/queryCache.ts:208](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryCache.ts#L208)
Destroys the given `Query` and removes it from the cache, notifying subscribers with a
`'removed'` event. A no-op if the query is no longer the one currently stored under its hash
(e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror
removals across `QueryCache` instances.
#### Parameters
##### query
[`Query`](Query.md)\<`any`, `any`, `any`, `any`\>
#### Returns
`void`
#### Example
```ts
const queryCache = queryClient.getQueryCache()
const query = queryCache.find({ queryKey: ['posts'] })
if (query) {
queryCache.remove(query)
}
```
#### Inherited from
```ts
QC.remove
```
***
### subscribe()
```ts
subscribe(listener): () => void;
```
Defined in: [packages/query-core/src/subscribable.ts:8](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L8)
#### Parameters
##### listener
`QueryCacheListener`
#### Returns
```ts
(): void;
```
##### Returns
`void`
#### Inherited from
```ts
QC.subscribe
```

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,555 @@
---
id: QueryObserver
title: QueryObserver
---
Defined in: [packages/query-core/src/queryObserver.ts:57](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L57)
A `QueryObserver` watches a single query in the `QueryCache` and computes a
`QueryObserverResult` from its state, recomputing and notifying subscribers
whenever the underlying query (or the observer's options) changes. It is
the primitive that framework adapters (e.g. `useQuery`) build their hooks
on top of, but it can also be used directly to observe and switch between
queries outside of any framework.
## Example
```ts
const observer = new QueryObserver(queryClient, {
queryKey: ['posts'],
queryFn: fetchPosts,
})
const unsubscribe = observer.subscribe((result) => {
console.log(result.data)
})
```
## Extends
- `Subscribable`\<`QueryObserverListener`\<`TData`, `TError`\>\>
## Extended by
- [`InfiniteQueryObserver`](InfiniteQueryObserver.md)
## Type Parameters
### TQueryFnData
`TQueryFnData` = `unknown`
### TError
`TError` = [`DefaultError`](../type-aliases/DefaultError.md)
### TData
`TData` = `TQueryFnData`
### TQueryData
`TQueryData` = `TQueryFnData`
### TQueryKey
`TQueryKey` *extends* [`QueryKey`](../type-aliases/QueryKey.md) = [`QueryKey`](../type-aliases/QueryKey.md)
## Constructors
### Constructor
```ts
new QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>(client, options): QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>;
```
Defined in: [packages/query-core/src/queryObserver.ts:87](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L87)
#### Parameters
##### client
`QueryClient`
##### options
[`QueryObserverOptions`](../interfaces/QueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryData`, `TQueryKey`\>
#### Returns
`QueryObserver`\<`TQueryFnData`, `TError`, `TData`, `TQueryData`, `TQueryKey`\>
#### Overrides
```ts
Subscribable<QueryObserverListener<TData, TError>>.constructor
```
## Properties
### listeners
```ts
protected listeners: Set<QueryObserverListener<TData, TError>>;
```
Defined in: [packages/query-core/src/subscribable.ts:2](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L2)
#### Inherited from
```ts
Subscribable.listeners
```
***
### options
```ts
options: QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>;
```
Defined in: [packages/query-core/src/queryObserver.ts:89](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L89)
## Methods
### bindMethods()
```ts
protected bindMethods(): void;
```
Defined in: [packages/query-core/src/queryObserver.ts:106](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L106)
#### Returns
`void`
***
### createResult()
```ts
protected createResult(query, options): QueryObserverResult<TData, TError>;
```
Defined in: [packages/query-core/src/queryObserver.ts:559](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L559)
#### Parameters
##### query
[`Query`](Query.md)\<`TQueryFnData`, `TError`, `TQueryData`, `TQueryKey`\>
##### options
[`QueryObserverOptions`](../interfaces/QueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryData`, `TQueryKey`\>
#### Returns
[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>
***
### destroy()
```ts
destroy(): void;
```
Defined in: [packages/query-core/src/queryObserver.ts:161](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L161)
Stops observing the current query: clears all listeners, cancels the
stale and refetch-interval timers, and removes this observer from the
query it was observing.
#### Returns
`void`
***
### fetch()
```ts
protected fetch(fetchOptions): Promise<QueryObserverResult<TData, TError>>;
```
Defined in: [packages/query-core/src/queryObserver.ts:448](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L448)
#### Parameters
##### fetchOptions
`ObserverFetchOptions`
#### Returns
`Promise`\<[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>\>
***
### fetchOptimistic()
```ts
fetchOptimistic(options): Promise<QueryObserverResult<TData, TError>>;
```
Defined in: [packages/query-core/src/queryObserver.ts:395](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L395)
Fetches a query defined by the given options without affecting this
observer's own tracked query or result, and returns a promise that
resolves with the `QueryObserverResult` for that fetch. This is useful
for prefetching data that another observer (e.g. a query about to be
navigated to) will need, ahead of time.
#### Parameters
##### options
[`QueryObserverOptions`](../interfaces/QueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryData`, `TQueryKey`\>
#### Returns
`Promise`\<[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>\>
#### Example
```ts
const result = await observer.fetchOptimistic({
queryKey: ['posts', 2],
queryFn: () => fetchPost(2),
})
console.log(result.data)
```
***
### getCurrentQuery()
```ts
getCurrentQuery(): Query<TQueryFnData, TError, TQueryData, TQueryKey>;
```
Defined in: [packages/query-core/src/queryObserver.ts:357](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L357)
Returns the `Query` instance this observer is currently observing.
#### Returns
[`Query`](Query.md)\<`TQueryFnData`, `TError`, `TQueryData`, `TQueryKey`\>
***
### getCurrentResult()
```ts
getCurrentResult(): QueryObserverResult<TData, TError>;
```
Defined in: [packages/query-core/src/queryObserver.ts:321](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L321)
Returns the most recently computed `QueryObserverResult` for the
observed query. This is a point-in-time read; to be notified of updates
as they happen, subscribe to the observer instead (its inherited
`subscribe` method).
#### Returns
[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>
#### Example
```ts
const result = observer.getCurrentResult()
console.log(result.status, result.data)
```
***
### getOptimisticResult()
```ts
getOptimisticResult(options): QueryObserverResult<TData, TError>;
```
Defined in: [packages/query-core/src/queryObserver.ts:272](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L272)
Computes the result the observer would produce for the given (already-defaulted) options
right now, building the underlying `Query` if it doesn't exist yet, without waiting for a
subscription callback. Called by framework adapters on every render (e.g. `useQuery`) so the
returned value is available synchronously, ahead of `setOptions` triggering an actual fetch.
#### Parameters
##### options
[`DefaultedQueryObserverOptions`](../type-aliases/DefaultedQueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryData`, `TQueryKey`\>
#### Returns
[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>
***
### hasListeners()
```ts
hasListeners(): boolean;
```
Defined in: [packages/query-core/src/subscribable.ts:19](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L19)
#### Returns
`boolean`
#### Inherited from
```ts
Subscribable.hasListeners
```
***
### onSubscribe()
```ts
protected onSubscribe(): void;
```
Defined in: [packages/query-core/src/queryObserver.ts:110](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L110)
#### Returns
`void`
#### Overrides
```ts
Subscribable.onSubscribe
```
***
### onUnsubscribe()
```ts
protected onUnsubscribe(): void;
```
Defined in: [packages/query-core/src/queryObserver.ts:124](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L124)
#### Returns
`void`
#### Overrides
```ts
Subscribable.onUnsubscribe
```
***
### refetch()
```ts
refetch(__namedParameters): Promise<QueryObserverResult<TData, TError>>;
```
Defined in: [packages/query-core/src/queryObserver.ts:371](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L371)
Refetches the observed query and returns a promise that resolves with
the resulting `QueryObserverResult`.
#### Parameters
##### \_\_namedParameters
[`RefetchOptions`](../interfaces/RefetchOptions.md) = `{}`
#### Returns
`Promise`\<[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>\>
#### Example
```ts
const result = await observer.refetch({ cancelRefetch: false })
console.log(result.data)
```
***
### setOptions()
```ts
setOptions(options): void;
```
Defined in: [packages/query-core/src/queryObserver.ts:182](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L182)
Updates the observer's options. This will re-resolve the query being
observed (switching to a different query if the `queryKey` changed),
trigger a fetch if the new options require one and the observer has
subscribers, recompute the current result, and reschedule the stale and
refetch-interval timers as needed.
#### Parameters
##### options
[`QueryObserverOptions`](../interfaces/QueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryData`, `TQueryKey`\>
#### Returns
`void`
#### Example
```ts
observer.setOptions({ queryKey: ['posts', 1], queryFn: () => fetchPost(1) })
// later: switch to a different query, reusing the same observer
observer.setOptions({ queryKey: ['posts', 2], queryFn: () => fetchPost(2) })
```
***
### shouldFetchOnReconnect()
```ts
shouldFetchOnReconnect(): boolean;
```
Defined in: [packages/query-core/src/queryObserver.ts:135](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L135)
Returns whether the observed query is currently stale and configured
(via the `refetchOnReconnect` option) to refetch when the network
reconnects.
#### Returns
`boolean`
***
### shouldFetchOnWindowFocus()
```ts
shouldFetchOnWindowFocus(): boolean;
```
Defined in: [packages/query-core/src/queryObserver.ts:148](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L148)
Returns whether the observed query is currently stale and configured
(via the `refetchOnWindowFocus` option) to refetch when the window
regains focus.
#### Returns
`boolean`
***
### subscribe()
```ts
subscribe(listener): () => void;
```
Defined in: [packages/query-core/src/subscribable.ts:8](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L8)
#### Parameters
##### listener
`QueryObserverListener`
#### Returns
```ts
(): void;
```
##### Returns
`void`
#### Inherited from
```ts
Subscribable.subscribe
```
***
### trackProp()
```ts
trackProp(key): void;
```
Defined in: [packages/query-core/src/queryObserver.ts:350](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L350)
Records that the given `QueryObserverResult` property was read, so a subsequent update only
notifies this observer if a tracked property actually changed. Normally called indirectly via
[QueryObserver#trackResult](#trackresult)'s proxy; exposed directly for adapters that track property
access themselves (e.g. through their own reactivity system) instead of via the proxy.
#### Parameters
##### key
`"error"` | `"data"` | `"isError"` | `"isPending"` | `"isLoading"` | `"isLoadingError"` | `"isRefetchError"` | `"isSuccess"` | `"isPlaceholderData"` | `"status"` | `"dataUpdatedAt"` | `"errorUpdatedAt"` | `"failureCount"` | `"failureReason"` | `"errorUpdateCount"` | `"isFetched"` | `"isFetchedAfterMount"` | `"isFetching"` | `"isInitialLoading"` | `"isPaused"` | `"isRefetching"` | `"isStale"` | `"isEnabled"` | `"refetch"` | `"fetchStatus"`
#### Returns
`void`
***
### trackResult()
```ts
trackResult(result, onPropTracked?): QueryObserverResult<TData, TError>;
```
Defined in: [packages/query-core/src/queryObserver.ts:331](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L331)
Wraps a `QueryObserverResult` in a `Proxy` that records which properties are read, via
[QueryObserver#trackProp](#trackprop) (and an optional `onPropTracked` callback). Used by framework
adapters when `notifyOnChangeProps` is not set, to implement its default "only re-render on
properties you actually read" behavior.
#### Parameters
##### result
[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>
##### onPropTracked?
(`key`) => `void`
#### Returns
[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>
***
### updateResult()
```ts
updateResult(): void;
```
Defined in: [packages/query-core/src/queryObserver.ts:735](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L735)
Recomputes and stores the current result from the current query/options, notifying listeners
if it changed. Framework adapters call this right after subscribing to make sure no query
update was missed in the gap between creating the observer and subscribing to it.
#### Returns
`void`