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

mutationOptions.ts

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