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

mutationOptions.ts

181 lines6.3 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 * 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 * @param options - The mutation options to use, identical to what you'd pass to `useMutation`, with a
13 * required `mutationKey`.
14 * @returns The same options object, unchanged.
15 * @see {@link useMutation} to run the mutation these options describe.
16 * @example
17 * Looking the mutation up elsewhere via its `mutationKey`, e.g. for a global "saving…" indicator:
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 */
36export function mutationOptions<
37 TData = unknown,
38 TError = DefaultError,
39 TVariables = void,
40 TOnMutateResult = unknown,
41>(
42 options: WithRequired<
43 MutationOptions<TData, TError, TVariables, TOnMutateResult>,
44 'mutationKey'
45 >,
46): WithRequired<
47 MutationOptions<TData, TError, TVariables, TOnMutateResult>,
48 'mutationKey'
50​
51/**
52 * Same as the plain-object overload with a required `mutationKey`, but for options that close over reactive
53 * state (`ref`s read inside the function body). Wrap them in a getter so `useMutation` and the other consumers
54 * always read the current values instead of the ones captured when the options were created.
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 * @see {@link useMutation} to run the mutation these options describe.
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 * @param options - The mutation options to use, identical to what you'd pass to `useMutation`, without a
101 * `mutationKey`.
102 * @returns The same options object, unchanged.
103 * @remarks See the other overload's example for looking a mutation up via `useMutationState`.
104 * @see {@link useMutation} to run the mutation these options describe.
105 * @example
106 * ```vue
107 * <script setup lang="ts">
108 * import { mutationOptions, useMutation } from '@tanstack/vue-query'
109 *
110 * const createPostOptions = mutationOptions({
111 * mutationFn: createPost,
112 * })
113 *
114 * const mutation = useMutation(createPostOptions)
115 * </script>
116 *
117 * <template>
118 * <button @click="mutation.mutate({ title: 'Hello' })">Create</button>
119 * </template>
120 * ```
121 */
122export function mutationOptions<
123 TData = unknown,
124 TError = DefaultError,
125 TVariables = void,
126 TOnMutateResult = unknown,
127>(
128 options: Omit<
129 MutationOptions<TData, TError, TVariables, TOnMutateResult>,
130 'mutationKey'
131 >,
132): Omit<
133 MutationOptions<TData, TError, TVariables, TOnMutateResult>,
134 'mutationKey'
136​
137/**
138 * Same as the plain-object overload without a `mutationKey`, but for options that close over reactive state
139 * (`ref`s read inside the function body). Wrap them in a getter so `useMutation` and the other consumers
140 * always read the current values instead of the ones captured when the options were created.
141 * @param options - A function returning the mutation options to use, without a `mutationKey`, re-evaluated on
142 * demand.
143 * @returns A function that returns the same options object, unchanged.
144 * @see {@link useMutation} to run the mutation these options describe.
145 * @example
146 * ```vue
147 * <script setup lang="ts">
148 * import { mutationOptions, useMutation } from '@tanstack/vue-query'
149 *
150 * const props = defineProps<{ userId: number }>()
151 *
152 * const createPostOptions = mutationOptions(() => ({
153 * // Reacts to changes on `props.userId`, unlike the plain-object overload.
154 * mutationFn: (title: string) => createPost({ title, userId: props.userId }),
155 * }))
156 *
157 * const mutation = useMutation(createPostOptions)
158 * </script>
159 *
160 * <template>
161 * <button @click="mutation.mutate('Hello')">Create</button>
162 * </template>
163 * ```
164 */
165export function mutationOptions<
166 TData = unknown,
167 TError = DefaultError,
168 TVariables = void,
169 TOnMutateResult = unknown,
170>(
171 options: () => Omit<
172 MutationOptions<TData, TError, TVariables, TOnMutateResult>,
173 'mutationKey'
174 >,
175): () => Omit<
176 MutationOptions<TData, TError, TVariables, TOnMutateResult>,
177 'mutationKey'
179export function mutationOptions(options: unknown) {
180 return options