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

mutationOptions.md

336 lines11.7 KB

1---
2id: mutationOptions
3title: mutationOptions
4redirect_from:
5 - framework/vue/reference/mutationOptions
6---
7​
8## Overview
9​
10```ts
11function mutationOptions<TData, TError, TVariables, TOnMutateResult>(options: WithRequired<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">): WithRequired<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">;
12function mutationOptions<TData, TError, TVariables, TOnMutateResult>(options: () => WithRequired<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">): () => WithRequired<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">;
13function mutationOptions<TData, TError, TVariables, TOnMutateResult>(options: Omit<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">): Omit<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">;
14function mutationOptions<TData, TError, TVariables, TOnMutateResult>(options: () => Omit<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">): () => Omit<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">;
15```
16​
17- [`WithRequired<MutationOptions>` β†’ `WithRequired<MutationOptions>`](#call-signature-1): You can generally pass everything to `mutationOptions` that you can also pass to `useMutation`. A `mutationKey` is required on this overload so the mutation can be looked up later, e.g. with `useMutationState`.
18- [`() => WithRequired<MutationOptions>` β†’ `() => WithRequired<MutationOptions>`](#call-signature-2): Same as the plain-object overload with a required `mutationKey`, but for options that close over reactive state (`ref`s read inside the function body). Wrap them in a getter so `useMutation` and the other consumers always read the current values instead of the ones captured when the options were created.
19- [`Omit<MutationOptions>` β†’ `Omit<MutationOptions>`](#call-signature-3): You can generally pass everything to `mutationOptions` that you can also pass to `useMutation`. No `mutationKey` is required on this overload β€” use this when you don't need to target the mutation via a `mutationKey` filter later (e.g. with `useMutationState`); it can still be observed through other filters, such as `status`.
20- [`() => Omit<MutationOptions>` β†’ `() => Omit<MutationOptions>`](#call-signature-4): Same as the plain-object overload without a `mutationKey`, but for options that close over reactive state (`ref`s read inside the function body). Wrap them in a getter so `useMutation` and the other consumers always read the current values instead of the ones captured when the options were created.
21​
22See also: [Parameters](#parameters-summary) Β· [Returns](#returns-summary)
23​
24<a id="call-signature-1"></a>
25​
26## Call Signature
27​
28```ts
29function mutationOptions<TData, TError, TVariables, TOnMutateResult>(options: WithRequired<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">): WithRequired<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">;
30```
31​
32Defined in: [packages/vue-query/src/mutationOptions.ts:36](https://github.com/TanStack/query/blob/main/packages/vue-query/src/mutationOptions.ts#L36)
33​
34You can generally pass everything to `mutationOptions` that you can also pass to `useMutation`. A
35`mutationKey` is required on this overload so the mutation can be looked up later, e.g. with
36`useMutationState`.
37​
38Unlike a `queryKey` property on a query's options (e.g. `queryOptions({ queryKey: [...] })`), `mutationKey`
39entries that are reactive getters (`() => id.value`) are never unwrapped β€” the getter function itself is
40stored as the entry, rather than its current value. `ref` entries are still unwrapped normally.
41​
42### Type Parameters
43​
44#### TData
45​
46`TData` = `unknown`
47​
48#### TError
49​
50`TError` = `Error`
51​
52#### TVariables
53​
54`TVariables` = `void`
55​
56#### TOnMutateResult
57​
58`TOnMutateResult` = `unknown`
59​
60### Parameters
61​
62#### options
63​
64[`WithRequired`](../type-aliases/WithRequired.md)\<[`MutationOptions`](../type-aliases/MutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
65​
66The mutation options to use, identical to what you'd pass to `useMutation`, with a
67required `mutationKey`.
68​
69### Returns
70​
71[`WithRequired`](../type-aliases/WithRequired.md)\<[`MutationOptions`](../type-aliases/MutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
72​
73The same options object, unchanged.
74​
75### See
76​
77[useMutation](useMutation.md) to run the mutation these options describe.
78​
79### Example
80​
81Looking the mutation up elsewhere via its `mutationKey`, e.g. for a global "saving…" indicator:
82```vue
83<script setup lang="ts">
84import { mutationOptions, useMutationState } from '@tanstack/vue-query'
85import { computed } from 'vue'
86​
87const createPostOptions = mutationOptions({
88 mutationKey: ['posts', 'create'],
89 mutationFn: createPost,
90})
91​
92// Call `useMutationState` once β€” it manages its own subscription β€” then derive from its result.
93const creatingPosts = useMutationState({
94 filters: { mutationKey: createPostOptions.mutationKey, status: 'pending' },
95})
96const isCreatingPost = computed(() => creatingPosts.value.length > 0)
97</script>
98```
99​
100<a id="call-signature-2"></a>
101​
102## Call Signature
103​
104```ts
105function mutationOptions<TData, TError, TVariables, TOnMutateResult>(options: () => WithRequired<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">): () => WithRequired<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">;
106```
107​
108Defined in: [packages/vue-query/src/mutationOptions.ts:80](https://github.com/TanStack/query/blob/main/packages/vue-query/src/mutationOptions.ts#L80)
109​
110Same as the plain-object overload with a required `mutationKey`, but for options that close over reactive
111state (`ref`s read inside the function body). Wrap them in a getter so `useMutation` and the other consumers
112always read the current values instead of the ones captured when the options were created.
113​
114### Type Parameters
115​
116#### TData
117​
118`TData` = `unknown`
119​
120#### TError
121​
122`TError` = `Error`
123​
124#### TVariables
125​
126`TVariables` = `void`
127​
128#### TOnMutateResult
129​
130`TOnMutateResult` = `unknown`
131​
132### Parameters
133​
134#### options
135​
136() => [`WithRequired`](../type-aliases/WithRequired.md)\<[`MutationOptions`](../type-aliases/MutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
137​
138A function returning the mutation options to use, with a required `mutationKey`,
139re-evaluated on demand.
140​
141### Returns
142​
143A function that returns the same options object, unchanged.
144​
145() => [`WithRequired`](../type-aliases/WithRequired.md)\<[`MutationOptions`](../type-aliases/MutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
146​
147### See
148​
149[useMutation](useMutation.md) to run the mutation these options describe.
150​
151### Example
152​
153```vue
154<script setup lang="ts">
155import { mutationOptions, useMutation } from '@tanstack/vue-query'
156​
157const props = defineProps<{ userId: number }>()
158​
159const createPostOptions = mutationOptions(() => ({
160 mutationKey: ['posts', 'create'],
161 // Reacts to changes on `props.userId`, unlike the plain-object overload.
162 mutationFn: (title: string) => createPost({ title, userId: props.userId }),
163}))
164​
165const mutation = useMutation(createPostOptions)
166</script>
167​
168<template>
169 <button @click="mutation.mutate('Hello')">Create</button>
170</template>
171```
172​
173<a id="call-signature-3"></a>
174​
175## Call Signature
176​
177```ts
178function mutationOptions<TData, TError, TVariables, TOnMutateResult>(options: Omit<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">): Omit<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">;
179```
180​
181Defined in: [packages/vue-query/src/mutationOptions.ts:122](https://github.com/TanStack/query/blob/main/packages/vue-query/src/mutationOptions.ts#L122)
182​
183You can generally pass everything to `mutationOptions` that you can also pass to `useMutation`. No
184`mutationKey` is required on this overload β€” use this when you don't need to target the mutation via a
185`mutationKey` filter later (e.g. with `useMutationState`); it can still be observed through other filters,
186such as `status`.
187​
188### Type Parameters
189​
190#### TData
191​
192`TData` = `unknown`
193​
194#### TError
195​
196`TError` = `Error`
197​
198#### TVariables
199​
200`TVariables` = `void`
201​
202#### TOnMutateResult
203​
204`TOnMutateResult` = `unknown`
205​
206### Parameters
207​
208#### options
209​
210`Omit`\<[`MutationOptions`](../type-aliases/MutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
211​
212The mutation options to use, identical to what you'd pass to `useMutation`, without a
213`mutationKey`.
214​
215### Returns
216​
217`Omit`\<[`MutationOptions`](../type-aliases/MutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
218​
219The same options object, unchanged.
220​
221### Remarks
222​
223See the other overload's example for looking a mutation up via `useMutationState`.
224​
225### See
226​
227[useMutation](useMutation.md) to run the mutation these options describe.
228​
229### Example
230​
231```vue
232<script setup lang="ts">
233import { mutationOptions, useMutation } from '@tanstack/vue-query'
234​
235const createPostOptions = mutationOptions({
236 mutationFn: createPost,
237})
238​
239const mutation = useMutation(createPostOptions)
240</script>
241​
242<template>
243 <button @click="mutation.mutate({ title: 'Hello' })">Create</button>
244</template>
245```
246​
247<a id="call-signature-4"></a>
248​
249## Call Signature
250​
251```ts
252function mutationOptions<TData, TError, TVariables, TOnMutateResult>(options: () => Omit<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">): () => Omit<MutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">;
253```
254​
255Defined in: [packages/vue-query/src/mutationOptions.ts:165](https://github.com/TanStack/query/blob/main/packages/vue-query/src/mutationOptions.ts#L165)
256​
257Same as the plain-object overload without a `mutationKey`, but for options that close over reactive state
258(`ref`s read inside the function body). Wrap them in a getter so `useMutation` and the other consumers
259always read the current values instead of the ones captured when the options were created.
260​
261### Type Parameters
262​
263#### TData
264​
265`TData` = `unknown`
266​
267#### TError
268​
269`TError` = `Error`
270​
271#### TVariables
272​
273`TVariables` = `void`
274​
275#### TOnMutateResult
276​
277`TOnMutateResult` = `unknown`
278​
279### Parameters
280​
281#### options
282​
283() => `Omit`\<[`MutationOptions`](../type-aliases/MutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
284​
285A function returning the mutation options to use, without a `mutationKey`, re-evaluated on
286demand.
287​
288### Returns
289​
290A function that returns the same options object, unchanged.
291​
292() => `Omit`\<[`MutationOptions`](../type-aliases/MutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
293​
294### See
295​
296[useMutation](useMutation.md) to run the mutation these options describe.
297​
298### Example
299​
300```vue
301<script setup lang="ts">
302import { mutationOptions, useMutation } from '@tanstack/vue-query'
303​
304const props = defineProps<{ userId: number }>()
305​
306const createPostOptions = mutationOptions(() => ({
307 // Reacts to changes on `props.userId`, unlike the plain-object overload.
308 mutationFn: (title: string) => createPost({ title, userId: props.userId }),
309}))
310​
311const mutation = useMutation(createPostOptions)
312</script>
313​
314<template>
315 <button @click="mutation.mutate('Hello')">Create</button>
316</template>
317```
318​
319<a id="parameters-summary"></a>
320​
321## Parameters
322​
323### options
324​
325() => `Omit`\<[`MutationOptions`](../type-aliases/MutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
326​
327A function returning the mutation options to use, without a `mutationKey`, re-evaluated on
328demand.
329​
330<a id="returns-summary"></a>
331​
332## Returns
333​
334A function that returns the same options object, unchanged.
335​
336() => `Omit`\<[`MutationOptions`](../type-aliases/MutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>