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

mutationOptions.md

167 lines4.8 KB

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)
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 }
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 }
167```