Open History to see the last commit
1---
2id: mutation-options
3title: Mutation Options
4---
5β
6One of the best ways to share mutation options between multiple places,
7is to use the `mutationOptions` helper. At runtime, this helper just returns whatever you pass into it,
8but it has a lot of advantages when using it [with TypeScript](../typescript.md#typing-query-options).
9You can define all possible options for a mutation in one place,
10and you'll also get type inference and type safety for all of them.
11β
12```ts
13export class QueriesService {
14 private readonly queryClient = inject(QueryClient)
15β
16 updatePost(id: number) {
17 return mutationOptions({
18 mutationFn: (post: Post) => Promise.resolve(post),
19 mutationKey: ['updatePost', id],
20 onSuccess: (newPost) => {
21 // ^? newPost: Post
22 this.queryClient.setQueryData(['posts', id], newPost)
23 },
24 })
25 }
26}
27```