Open History to see the last commit
1import type { DefaultError, WithRequired } from '@tanstack/query-core'
2import type { CreateMutationOptions } from './types'
3​
4/**
5 * You can generally pass everything to `mutationOptions` that you can also pass
6 * to `injectMutation`. A `mutationKey` is required on this overload so the
7 * mutation can be looked up later, e.g. with `injectMutationState`.
8 *
9 * @example
10 * Looking the mutation up elsewhere via its `mutationKey`, e.g. for a global "saving…" indicator:
11 * ```angular-ts
12 * import { mutationOptions, injectMutationState } from '@tanstack/angular-query-experimental'
13 *
14 * const createPostOptions = mutationOptions({
15 * mutationKey: ['posts', 'create'],
16 * mutationFn: createPost,
17 * })
18 *
19 * @Component({
20 * selector: 'saving-indicator',
21 * template: `
22 * @if (isCreatingPost()) {
23 * <span>Saving…</span>
24 * }
25 * `,
26 * })
27 * export class SavingIndicator {
28 * readonly #pendingCreates = injectMutationState(() => ({
29 * filters: { mutationKey: createPostOptions.mutationKey, status: 'pending' },
30 * }))
31 * readonly isCreatingPost = computed(() => this.#pendingCreates().length > 0)
32 * }
33 * ```
34 *
35 * @param options - The mutation options to use, identical to what you'd pass to
36 * `injectMutation`, with a required `mutationKey`.
37 * @returns The same options object, unchanged.
38 * @see {@link injectMutation} to run the mutation these options describe.
39 */
40export function mutationOptions<
41 TData = unknown,
42 TError = DefaultError,
43 TVariables = void,
44 TOnMutateResult = unknown,
45>(
46 options: WithRequired<
47 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
48 'mutationKey'
49 >,
50): WithRequired<
51 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
52 'mutationKey'
53>
54/**
55 * You can generally pass everything to `mutationOptions` that you can also pass
56 * to `injectMutation`. No `mutationKey` is required on this overload — use this
57 * when you don't need to target the mutation via a `mutationKey` filter later
58 * (e.g. with `injectMutationState`); it can still be observed through other
59 * filters, such as `status`.
60 *
61 * @remarks
62 * See the other overload's example for looking a mutation up via
63 * `injectMutationState`.
64 * @example
65 * Sharing options across services, so `QueriesService` stays the single place a mutation is defined:
66 * ```angular-ts
67 * import { mutationOptions, injectMutation } from '@tanstack/angular-query-experimental'
68 *
69 * @Injectable({ providedIn: 'root' })
70 * export class QueriesService {
71 * readonly #queryClient = inject(QueryClient)
72 *
73 * updatePost(id: number) {
74 * return mutationOptions({
75 * mutationFn: (post: Partial<Post>) => putPost(id, post),
76 * onSuccess: (newPost) => this.#queryClient.setQueryData(['posts', id], newPost),
77 * })
78 * }
79 * }
80 *
81 * @Component({
82 * selector: 'post',
83 * template: `<button (click)="save()">Save</button>`,
84 * })
85 * export class Post {
86 * readonly queries = inject(QueriesService)
87 * readonly id = signal(0)
88 * readonly updatePostMutation = injectMutation(() => this.queries.updatePost(this.id()))
89 *
90 * save() {
91 * this.updatePostMutation.mutate({ title: 'New Title' })
92 * }
93 * }
94 * ```
95 *
96 * @param options - The mutation options to use, identical to what you'd pass to
97 * `injectMutation`, without a `mutationKey`.
98 * @returns The same options object, unchanged.
99 * @see {@link injectMutation} to run the mutation these options describe.
100 */
101export function mutationOptions<
102 TData = unknown,
103 TError = DefaultError,
104 TVariables = void,
105 TOnMutateResult = unknown,
106>(
107 options: Omit<
108 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
109 'mutationKey'
110 >,
111): Omit<
112 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
113 'mutationKey'
114>
115export function mutationOptions<
116 TData = unknown,
117 TError = DefaultError,
118 TVariables = void,
119 TOnMutateResult = unknown,
120>(
121 options: CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
122): CreateMutationOptions<TData, TError, TVariables, TOnMutateResult> {
123 return options
124}