snapshot of https://github.com/TanStack/query.git (main, 2026-09-16; blobs > 131072B omitted)
This commit is contained in:
27
docs/framework/angular/guides/mutation-options.md
Normal file
27
docs/framework/angular/guides/mutation-options.md
Normal file
@@ -0,0 +1,27 @@
|
||||
---
|
||||
id: query-options
|
||||
title: Mutation Options
|
||||
---
|
||||
|
||||
One of the best ways to share mutation options between multiple places,
|
||||
is to use the `mutationOptions` helper. At runtime, this helper just returns whatever you pass into it,
|
||||
but it has a lot of advantages when using it [with TypeScript](../typescript.md#typing-query-options).
|
||||
You can define all possible options for a mutation in one place,
|
||||
and you'll also get type inference and type safety for all of them.
|
||||
|
||||
```ts
|
||||
export class QueriesService {
|
||||
private readonly http = inject(HttpClient)
|
||||
|
||||
updatePost(id: number) {
|
||||
return mutationOptions({
|
||||
mutationFn: (post: Post) => Promise.resolve(post),
|
||||
mutationKey: ['updatePost', id],
|
||||
onSuccess: (newPost) => {
|
||||
// ^? newPost: Post
|
||||
this.queryClient.setQueryData(['posts', id], newPost)
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user