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

mutationOptions.ts

198 lines6.5 KB

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