Open History to see the last commit
1---
2id: mutationOptions
3title: mutationOptions
4---
5β
6## Call Signature
7β
8```ts
9function mutationOptions<TData, TError, TVariables, TOnMutateResult>(options: WithRequired<CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">): WithRequired<CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">;
10```
11β
12Defined in: [packages/angular-query-experimental/src/mutation-options.ts:40](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/mutation-options.ts#L40)
13β
14You can generally pass everything to `mutationOptions` that you can also pass to `injectMutation`. A
15`mutationKey` is required on this overload so the mutation can be looked up later, e.g. with
16`injectMutationState`.
17β
18### Type Parameters
19β
20#### TData
21β
22`TData` = `unknown`
23β
24#### TError
25β
26`TError` = `Error`
27β
28#### TVariables
29β
30`TVariables` = `void`
31β
32#### TOnMutateResult
33β
34`TOnMutateResult` = `unknown`
35β
36### Parameters
37β
38#### options
39β
40[`WithRequired`](../type-aliases/WithRequired.md)\<[`CreateMutationOptions`](../interfaces/CreateMutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
41β
42The mutation options to use, identical to what you'd pass to `injectMutation`, with a
43required `mutationKey`.
44β
45### Returns
46β
47[`WithRequired`](../type-aliases/WithRequired.md)\<[`CreateMutationOptions`](../interfaces/CreateMutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
48β
49The same options object, unchanged.
50β
51### See
52β
53[injectMutation](injectMutation.md) to run the mutation these options describe.
54β
55### Example
56β
57Looking the mutation up elsewhere via its `mutationKey`, e.g. for a global "savingβ¦" indicator:
58```angular-ts
59import { mutationOptions, injectMutationState } from '@tanstack/angular-query-experimental'
60β
61const createPostOptions = mutationOptions({
62 mutationKey: ['posts', 'create'],
63 mutationFn: createPost,
64})
65β
66@Component({
67 selector: 'saving-indicator',
68 template: `
69 @if (isCreatingPost()) {
70 <span>Savingβ¦</span>
71 }
72 `,
73})
74export class SavingIndicator {
75 readonly #pendingCreates = injectMutationState(() => ({
76 filters: { mutationKey: createPostOptions.mutationKey, status: 'pending' },
77 }))
78 readonly isCreatingPost = computed(() => this.#pendingCreates().length > 0)
79}
80```
81β
82## Call Signature
83β
84```ts
85function mutationOptions<TData, TError, TVariables, TOnMutateResult>(options: Omit<CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">): Omit<CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>, "mutationKey">;
86```
87β
88Defined in: [packages/angular-query-experimental/src/mutation-options.ts:98](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/mutation-options.ts#L98)
89β
90You can generally pass everything to `mutationOptions` that you can also pass to `injectMutation`. No
91`mutationKey` is required on this overload β use this when you don't need to target the mutation via a
92`mutationKey` filter later (e.g. with `injectMutationState`); it can still be observed through other
93filters, such as `status`.
94β
95### Type Parameters
96β
97#### TData
98β
99`TData` = `unknown`
100β
101#### TError
102β
103`TError` = `Error`
104β
105#### TVariables
106β
107`TVariables` = `void`
108β
109#### TOnMutateResult
110β
111`TOnMutateResult` = `unknown`
112β
113### Parameters
114β
115#### options
116β
117`Omit`\<[`CreateMutationOptions`](../interfaces/CreateMutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
118β
119The mutation options to use, identical to what you'd pass to `injectMutation`, without a
120`mutationKey`.
121β
122### Returns
123β
124`Omit`\<[`CreateMutationOptions`](../interfaces/CreateMutationOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
125β
126The same options object, unchanged.
127β
128### See
129β
130[injectMutation](injectMutation.md) to run the mutation these options describe.
131β
132### Remarks
133β
134See the other overload's example for looking a mutation up via `injectMutationState`.
135β
136### Example
137β
138Sharing options across services, so `QueriesService` stays the single place a mutation is defined:
139```angular-ts
140import { mutationOptions, injectMutation } from '@tanstack/angular-query-experimental'
141β
142@Injectable({ providedIn: 'root' })
143export class QueriesService {
144 readonly #queryClient = inject(QueryClient)
145β
146 updatePost(id: number) {
147 return mutationOptions({
148 mutationFn: (post: Partial<Post>) => putPost(id, post),
149 onSuccess: (newPost) => this.#queryClient.setQueryData(['posts', id], newPost),
150 })
151 }
152}
153β
154@Component({
155 selector: 'post',
156 template: `<button (click)="save()">Save</button>`,
157})
158export class Post {
159 readonly queries = inject(QueriesService)
160 readonly id = signal(0)
161 readonly updatePostMutation = injectMutation(() => this.queries.updatePost(this.id()))
162β
163 save() {
164 this.updatePostMutation.mutate({ title: 'New Title' })
165 }
166}
167```