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

Mutation.md

330 lines6.0 KB

1---
2id: Mutation
3title: Mutation
4---
5​
6Defined in: packages/query-core/dist-ts/src/mutation.d.ts:101
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/dist-ts/src/mutation.d.ts:106
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/dist-ts/src/removable.d.ts:3
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/dist-ts/src/mutation.d.ts:105
98​
99***
100​
101### options
102​
103```ts
104options: MutationOptions<TData, TError, TVariables, TOnMutateResult>;
105```
106​
107Defined in: packages/query-core/dist-ts/src/mutation.d.ts:104
108​
109***
110​
111### state
112​
113```ts
114state: MutationState<TData, TError, TVariables, TOnMutateResult>;
115```
116​
117Defined in: packages/query-core/dist-ts/src/mutation.d.ts:103
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/dist-ts/src/mutation.d.ts:112
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### clearGcTimeout()
140​
141```ts
142protected clearGcTimeout(): void;
143```
144​
145Defined in: packages/query-core/dist-ts/src/removable.d.ts:7
146​
147#### Returns
148​
149`void`
150​
151#### Inherited from
152​
153```ts
154Removable.clearGcTimeout
155```
156​
157***
158​
159### continue()
160​
161```ts
162continue(): Promise<unknown>;
163```
164​
165Defined in: packages/query-core/dist-ts/src/mutation.d.ts:139
166​
167Resumes a mutation that is currently paused or was restored from a
168dehydrated, still-`pending` state.
169​
170- If this mutation has an active retryer (it paused mid-attempt, e.g. due
171 to the network mode or scope-based queuing), its retryer is resumed.
172- Otherwise, if the mutation's status is still `pending` (e.g. it was
173 dehydrated while an attempt was in flight and never got a retryer in
174 this instance), `execute` is called again with the last known variables.
175- Otherwise the mutation has already settled and this resolves immediately
176 without running anything again.
177​
178#### Returns
179​
180`Promise`\<`unknown`\>
181​
182#### Example
183​
184```ts
185// typically driven by reconnect handling, e.g. queryClient.resumePausedMutations()
186const mutation = mutationCache.find({ mutationKey: ['addPost'] })
187await mutation?.continue()
188```
189​
190#### See
191​
192[Mutation#execute](#execute)
193​
194***
195​
196### destroy()
197​
198```ts
199destroy(): void;
200```
201​
202Defined in: packages/query-core/dist-ts/src/removable.d.ts:4
203​
204#### Returns
205​
206`void`
207​
208#### Inherited from
209​
210```ts
211Removable.destroy
212```
213​
214***
215​
216### execute()
217​
218```ts
219execute(variables: TVariables): Promise<TData>;
220```
221​
222Defined in: packages/query-core/dist-ts/src/mutation.d.ts:170
223​
224Runs the mutation function for the given variables through a retryer, and
225drives the mutation's state and lifecycle callbacks through to settlement.
226​
227If this mutation's state is already `pending` when `execute` is called
228(i.e. it was restored, still in-flight, from a dehydrated state), the
229`onMutate` step is skipped and a `continue` action is dispatched to
230unpause it; otherwise a `pending` action is dispatched first, then the
231mutation cache's `onMutate` and the mutation's own `onMutate` option are
232awaited in that order, and the resulting context is stored.
233​
234The mutation function is then run (subject to `retry`/`retryDelay`/
235`networkMode`, and to the mutation cache's scope-based serialization).
236On success, the cache's `onSuccess`/`onSettled` callbacks run before the
237mutation's own `onSuccess`/`onSettled` options, a `success` action is
238dispatched, and the resolved data is returned. On failure, the same
239cache-then-option ordering is used for `onError`/`onSettled`, but each of
240those four callbacks is individually caught so that a throwing callback
241cannot mask the original error; an `error` action is then dispatched and
242the original error is re-thrown.
243​
244#### Parameters
245​
246##### variables
247​
248`TVariables`
249​
250#### Returns
251​
252`Promise`\<`TData`\>
253​
254#### Example
255​
256```ts
257// Called internally by `MutationObserver.mutate` and `Mutation.continue` β€”
258// applications normally trigger mutations through those, not this method.
259const data = await mutation.execute(variables)
260```
261​
262#### See
263​
264[Mutation#continue](#continue)
265​
266***
267​
268### optionalRemove()
269​
270```ts
271protected optionalRemove(): void;
272```
273​
274Defined in: packages/query-core/dist-ts/src/mutation.d.ts:117
275​
276#### Returns
277​
278`void`
279​
280#### Overrides
281​
282```ts
283Removable.optionalRemove
284```
285​
286***
287​
288### scheduleGc()
289​
290```ts
291protected scheduleGc(): void;
292```
293​
294Defined in: packages/query-core/dist-ts/src/removable.d.ts:5
295​
296#### Returns
297​
298`void`
299​
300#### Inherited from
301​
302```ts
303Removable.scheduleGc
304```
305​
306***
307​
308### updateGcTime()
309​
310```ts
311protected updateGcTime(newGcTime: number | undefined): void;
312```
313​
314Defined in: packages/query-core/dist-ts/src/removable.d.ts:6
315​
316#### Parameters
317​
318##### newGcTime
319​
320`number` | `undefined`
321​
322#### Returns
323​
324`void`
325​
326#### Inherited from
327​
328```ts
329Removable.updateGcTime
330```