Open History to see the last commit
1import type { DefaultError, WithRequired } from '@tanstack/query-core'
2import type { MutationOptions } from './types'
3
4/**
5 * You can generally pass everything to `mutationOptions` that you can also pass to `useMutation`. A
6 * `mutationKey` is required on this overload so the mutation can be looked up later, e.g. with
7 * `useMutationState`.
8 *
9 * Unlike a `queryKey` property on a query's options (e.g. `queryOptions({ queryKey: [...] })`), `mutationKey`
10 * entries that are reactive getters (`() => id.value`) are never unwrapped — the getter function itself is
11 * stored as the entry, rather than its current value. `ref` entries are still unwrapped normally.
12 *
13 * @see {@link useMutation} to run the mutation these options describe.
14 * @param options - The mutation options to use, identical to what you'd pass to `useMutation`, with a
15 * required `mutationKey`.
16 * @returns The same options object, unchanged.
17 *
18 * @example
19 * Looking the mutation up elsewhere via its `mutationKey`, e.g. for a global "saving…" indicator:
20 * ```vue
21 * <script setup lang="ts">
22 * import { mutationOptions, useMutationState } from '@tanstack/vue-query'
23 * import { computed } from 'vue'
24 *
25 * const createPostOptions = mutationOptions({
26 * mutationKey: ['posts', 'create'],
27 * mutationFn: createPost,
28 * })
29 *
30 * // Call `useMutationState` once — it manages its own subscription — then derive from its result.
31 * const creatingPosts = useMutationState({
32 * filters: { mutationKey: createPostOptions.mutationKey, status: 'pending' },
33 * })
34 * const isCreatingPost = computed(() => creatingPosts.value.length > 0)
35 * </script>
36 * ```
37 */
38export function mutationOptions<
39 TData = unknown,
40 TError = DefaultError,
41 TVariables = void,
42 TOnMutateResult = unknown,
43>(
44 options: WithRequired<
45 MutationOptions<TData, TError, TVariables, TOnMutateResult>,
46 'mutationKey'
47 >,
48): WithRequired<
49 MutationOptions<TData, TError, TVariables, TOnMutateResult>,
50 'mutationKey'
51>
52
53/**
54 * Same as the plain-object overload with a required `mutationKey`, but for options that close over reactive
55 * state (`ref`s read inside the function body). Wrap them in a getter so `useMutation` and the other consumers
56 * always read the current values instead of the ones captured when the options were created.
57 *
58 * @see {@link useMutation} to run the mutation these options describe.
59 * @param options - A function returning the mutation options to use, with a required `mutationKey`,
60 * re-evaluated on demand.
61 * @returns A function that returns the same options object, unchanged.
62 *
63 * @example
64 * ```vue
65 * <script setup lang="ts">
66 * import { mutationOptions, useMutation } from '@tanstack/vue-query'
67 *
68 * const props = defineProps<{ userId: number }>()
69 *
70 * const createPostOptions = mutationOptions(() => ({
71 * mutationKey: ['posts', 'create'],
72 * // Reacts to changes on `props.userId`, unlike the plain-object overload.
73 * mutationFn: (title: string) => createPost({ title, userId: props.userId }),
74 * }))
75 *
76 * const mutation = useMutation(createPostOptions)
77 * </script>
78 *
79 * <template>
80 * <button @click="mutation.mutate('Hello')">Create</button>
81 * </template>
82 * ```
83 */
84export function mutationOptions<
85 TData = unknown,
86 TError = DefaultError,
87 TVariables = void,
88 TOnMutateResult = unknown,
89>(
90 options: () => WithRequired<
91 MutationOptions<TData, TError, TVariables, TOnMutateResult>,
92 'mutationKey'
93 >,
94): () => WithRequired<
95 MutationOptions<TData, TError, TVariables, TOnMutateResult>,
96 'mutationKey'
97>
98
99/**
100 * You can generally pass everything to `mutationOptions` that you can also pass to `useMutation`. No
101 * `mutationKey` is required on this overload — use this when you don't need to target the mutation via a
102 * `mutationKey` filter later (e.g. with `useMutationState`); it can still be observed through other filters,
103 * such as `status`.
104 *
105 * @see {@link useMutation} to run the mutation these options describe.
106 * @param options - The mutation options to use, identical to what you'd pass to `useMutation`, without a
107 * `mutationKey`.
108 * @returns The same options object, unchanged.
109 * @remarks See the other overload's example for looking a mutation up via `useMutationState`.
110 *
111 * @example
112 * ```vue
113 * <script setup lang="ts">
114 * import { mutationOptions, useMutation } from '@tanstack/vue-query'
115 *
116 * const createPostOptions = mutationOptions({
117 * mutationFn: createPost,
118 * })
119 *
120 * const mutation = useMutation(createPostOptions)
121 * </script>
122 *
123 * <template>
124 * <button @click="mutation.mutate({ title: 'Hello' })">Create</button>
125 * </template>
126 * ```
127 */
128export function mutationOptions<
129 TData = unknown,
130 TError = DefaultError,
131 TVariables = void,
132 TOnMutateResult = unknown,
133>(
134 options: Omit<
135 MutationOptions<TData, TError, TVariables, TOnMutateResult>,
136 'mutationKey'
137 >,
138): Omit<
139 MutationOptions<TData, TError, TVariables, TOnMutateResult>,
140 'mutationKey'
141>
142
143/**
144 * Same as the plain-object overload without a `mutationKey`, but for options that close over reactive state
145 * (`ref`s read inside the function body). Wrap them in a getter so `useMutation` and the other consumers
146 * always read the current values instead of the ones captured when the options were created.
147 *
148 * @see {@link useMutation} to run the mutation these options describe.
149 * @param options - A function returning the mutation options to use, without a `mutationKey`, re-evaluated on
150 * demand.
151 * @returns A function that returns the same options object, unchanged.
152 *
153 * @example
154 * ```vue
155 * <script setup lang="ts">
156 * import { mutationOptions, useMutation } from '@tanstack/vue-query'
157 *
158 * const props = defineProps<{ userId: number }>()
159 *
160 * const createPostOptions = mutationOptions(() => ({
161 * // Reacts to changes on `props.userId`, unlike the plain-object overload.
162 * mutationFn: (title: string) => createPost({ title, userId: props.userId }),
163 * }))
164 *
165 * const mutation = useMutation(createPostOptions)
166 * </script>
167 *
168 * <template>
169 * <button @click="mutation.mutate('Hello')">Create</button>
170 * </template>
171 * ```
172 */
173export function mutationOptions<
174 TData = unknown,
175 TError = DefaultError,
176 TVariables = void,
177 TOnMutateResult = unknown,
178>(
179 options: () => Omit<
180 MutationOptions<TData, TError, TVariables, TOnMutateResult>,
181 'mutationKey'
182 >,
183): () => Omit<
184 MutationOptions<TData, TError, TVariables, TOnMutateResult>,
185 'mutationKey'
186>
187export function mutationOptions(options: unknown) {
188 return options
189}