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

mutationOptions.md

338 lines12.0 KB

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