Open History to see the last commit
1---
2id: Mutation
3title: Mutation
4---
5β
6Defined in: [packages/query-core/src/mutation.ts:135](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L135)
7β
8Represents a single mutation attempt. A `Mutation` holds the mutation's
9options, state (data/error/status), and the `MutationObserver`s currently
10subscribed to it.
11β
12Instances are created and managed internally by `MutationCache`; application
13code typically interacts with mutations indirectly through `QueryClient` or
14a framework hook like `useMutation`. Direct access to a `Mutation` instance
15is possible via `mutationCache.find()`/`getAll()` for inspecting cache state.
16β
17## Example
18β
19```ts
20const mutationCache = queryClient.getMutationCache()
21β
22const mutation = mutationCache.find({ mutationKey: ['addPost'] })
23```
24β
25## Extends
26β
27- `Removable`
28β
29## Type Parameters
30β
31### TData
32β
33`TData` = `unknown`
34β
35### TError
36β
37`TError` = [`DefaultError`](../type-aliases/DefaultError.md)
38β
39### TVariables
40β
41`TVariables` = `unknown`
42β
43### TOnMutateResult
44β
45`TOnMutateResult` = `unknown`
46β
47## Constructors
48β
49### Constructor
50β
51```ts
52new Mutation<TData, TError, TVariables, TOnMutateResult>(config: MutationConfig<TData, TError, TVariables, TOnMutateResult>): Mutation<TData, TError, TVariables, TOnMutateResult>;
53```
54β
55Defined in: [packages/query-core/src/mutation.ts:152](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L152)
56β
57#### Parameters
58β
59##### config
60β
61`MutationConfig`\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
62β
63#### Returns
64β
65`Mutation`\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
66β
67#### Overrides
68β
69```ts
70Removable.constructor
71```
72β
73## Properties
74β
75### gcTime
76β
77```ts
78gcTime: number;
79```
80β
81Defined in: [packages/query-core/src/removable.ts:11](https://github.com/TanStack/query/blob/main/packages/query-core/src/removable.ts#L11)
82β
83#### Inherited from
84β
85```ts
86Removable.gcTime
87```
88β
89***
90β
91### mutationId
92β
93```ts
94readonly mutationId: number;
95```
96β
97Defined in: [packages/query-core/src/mutation.ts:143](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L143)
98β
99***
100β
101### options
102β
103```ts
104options: MutationOptions<TData, TError, TVariables, TOnMutateResult>;
105```
106β
107Defined in: [packages/query-core/src/mutation.ts:142](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L142)
108β
109***
110β
111### state
112β
113```ts
114state: MutationState<TData, TError, TVariables, TOnMutateResult>;
115```
116β
117Defined in: [packages/query-core/src/mutation.ts:141](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L141)
118β
119## Accessors
120β
121### meta
122β
123#### Get Signature
124β
125```ts
126get meta(): Record<string, unknown> | undefined;
127```
128β
129Defined in: [packages/query-core/src/mutation.ts:179](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L179)
130β
131The `meta` object passed in the mutation's options, if any.
132β
133##### Returns
134β
135`Record`\<`string`, `unknown`\> \| `undefined`
136β
137## Methods
138β
139### continue()
140β
141```ts
142continue(): Promise<unknown>;
143```
144β
145Defined in: [packages/query-core/src/mutation.ts:243](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L243)
146β
147Resumes a mutation that is currently paused or was restored from a
148dehydrated, still-`pending` state.
149β
150- If this mutation has an active retryer (it paused mid-attempt, e.g. due
151 to the network mode or scope-based queuing), its retryer is resumed.
152- Otherwise, if the mutation's status is still `pending` (e.g. it was
153 dehydrated while an attempt was in flight and never got a retryer in
154 this instance), `execute` is called again with the last known variables.
155- Otherwise the mutation has already settled and this resolves immediately
156 without running anything again.
157β
158#### Returns
159β
160`Promise`\<`unknown`\>
161β
162#### Example
163β
164```ts
165// typically driven by reconnect handling, e.g. queryClient.resumePausedMutations()
166const mutation = mutationCache.find({ mutationKey: ['addPost'] })
167await mutation?.continue()
168```
169β
170#### See
171β
172[Mutation#execute](#execute)
173β
174***
175β
176### destroy()
177β
178```ts
179destroy(): void;
180```
181β
182Defined in: [packages/query-core/src/removable.ts:19](https://github.com/TanStack/query/blob/main/packages/query-core/src/removable.ts#L19)
183β
184Clears the pending garbage collection timeout, so the entry is no longer scheduled for removal.
185A subclass may override this to release what it holds on to as well β `Query` also cancels any
186in-flight fetch.
187β
188#### Returns
189β
190`void`
191β
192#### Inherited from
193β
194```ts
195Removable.destroy
196```
197β
198***
199β
200### execute()
201β
202```ts
203execute(variables: TVariables): Promise<TData>;
204```
205β
206Defined in: [packages/query-core/src/mutation.ts:284](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutation.ts#L284)
207β
208Runs the mutation function for the given variables through a retryer, and
209drives the mutation's state and lifecycle callbacks through to settlement.
210β
211If this mutation's state is already `pending` when `execute` is called
212(i.e. it was restored, still in-flight, from a dehydrated state), the
213`onMutate` step is skipped and a `continue` action is dispatched to
214unpause it; otherwise a `pending` action is dispatched first, then the
215mutation cache's `onMutate` and the mutation's own `onMutate` option are
216awaited in that order, and the resulting context is stored.
217β
218The mutation function is then run (subject to `retry`/`retryDelay`/
219`networkMode`, and to the mutation cache's scope-based serialization).
220On success, the cache's `onSuccess`/`onSettled` callbacks run before the
221mutation's own `onSuccess`/`onSettled` options, a `success` action is
222dispatched, and the resolved data is returned. On failure, the same
223cache-then-option ordering is used for `onError`/`onSettled`, but each of
224those four callbacks is individually caught so that a throwing callback
225cannot mask the original error; an `error` action is then dispatched and
226the original error is re-thrown.
227β
228#### Parameters
229β
230##### variables
231β
232`TVariables`
233β
234#### Returns
235β
236`Promise`\<`TData`\>
237β
238#### Example
239β
240```ts
241// Called internally by `MutationObserver.mutate` and `Mutation.continue` β
242// applications normally trigger mutations through those, not this method.
243const data = await mutation.execute(variables)
244```
245β
246#### See
247β
248[Mutation#continue](#continue)