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

mutationoptions.md

191 lines4.0 KB

1---
2id: mutationOptions
3title: mutationOptions
4---
5​
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7​
8# Function: mutationOptions()
9​
10Allows to share and re-use mutation options in a type-safe way.
11​
12**Example**
13​
14```ts
15export class QueriesService {
16 private http = inject(HttpClient)
17​
18 updatePost(id: number) {
19 return mutationOptions({
20 mutationFn: (post: Post) => Promise.resolve(post),
21 mutationKey: ['updatePost', id],
22 onSuccess: (newPost) => {
23 // ^? newPost: Post
24 this.queryClient.setQueryData(['posts', id], newPost)
25 },
26 })
27 }
29​
30class ComponentOrService {
31 queries = inject(QueriesService)
32 id = signal(0)
33 mutation = injectMutation(() => this.queries.updatePost(this.id()))
34​
35 save() {
36 this.mutation.mutate({ title: 'New Title' })
37 }
39```
40​
41## Param
42​
43The mutation options.
44​
45## Call Signature
46​
47```ts
48function mutationOptions<TData, TError, TVariables, TOnMutateResult>(
49 options,
50): WithRequired<
51 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
52 'mutationKey'
54```
55​
56Defined in: [mutation-options.ts:38](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/mutation-options.ts#L38)
57​
58Allows to share and re-use mutation options in a type-safe way.
59​
60**Example**
61​
62```ts
63export class QueriesService {
64 private http = inject(HttpClient)
65​
66 updatePost(id: number) {
67 return mutationOptions({
68 mutationFn: (post: Post) => Promise.resolve(post),
69 mutationKey: ['updatePost', id],
70 onSuccess: (newPost) => {
71 // ^? newPost: Post
72 this.queryClient.setQueryData(['posts', id], newPost)
73 },
74 })
75 }
77​
78class ComponentOrService {
79 queries = inject(QueriesService)
80 id = signal(0)
81 mutation = injectMutation(() => this.queries.updatePost(this.id()))
82​
83 save() {
84 this.mutation.mutate({ title: 'New Title' })
85 }
87```
88​
89### Type Parameters
90​
91β€’ **TData** = `unknown`
92​
93β€’ **TError** = `Error`
94​
95β€’ **TVariables** = `void`
96​
97β€’ **TOnMutateResult** = `unknown`
98​
99### Parameters
100​
101#### options
102​
103`WithRequired`\<[`CreateMutationOptions`](../../interfaces/createmutationoptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
104​
105The mutation options.
106​
107### Returns
108​
109`WithRequired`\<[`CreateMutationOptions`](../../interfaces/createmutationoptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
110​
111Mutation options.
112​
113Mutation options.
114​
115### Param
116​
117The mutation options.
118​
119## Call Signature
120​
121```ts
122function mutationOptions<TData, TError, TVariables, TOnMutateResult>(
123 options,
124): Omit<
125 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
126 'mutationKey'
128```
129​
130Defined in: [mutation-options.ts:52](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/mutation-options.ts#L52)
131​
132Allows to share and re-use mutation options in a type-safe way.
133​
134**Example**
135​
136```ts
137export class QueriesService {
138 private http = inject(HttpClient)
139​
140 updatePost(id: number) {
141 return mutationOptions({
142 mutationFn: (post: Post) => Promise.resolve(post),
143 mutationKey: ['updatePost', id],
144 onSuccess: (newPost) => {
145 // ^? newPost: Post
146 this.queryClient.setQueryData(['posts', id], newPost)
147 },
148 })
149 }
151​
152class ComponentOrService {
153 queries = inject(QueriesService)
154 id = signal(0)
155 mutation = injectMutation(() => this.queries.updatePost(this.id()))
156​
157 save() {
158 this.mutation.mutate({ title: 'New Title' })
159 }
161```
162​
163### Type Parameters
164​
165β€’ **TData** = `unknown`
166​
167β€’ **TError** = `Error`
168​
169β€’ **TVariables** = `void`
170​
171β€’ **TOnMutateResult** = `unknown`
172​
173### Parameters
174​
175#### options
176​
177`Omit`\<[`CreateMutationOptions`](../../interfaces/createmutationoptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
178​
179The mutation options.
180​
181### Returns
182​
183`Omit`\<[`CreateMutationOptions`](../../interfaces/createmutationoptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>, `"mutationKey"`\>
184​
185Mutation options.
186​
187Mutation options.
188​
189### Param
190​
191The mutation options.