뮤테이션 옵션
여러 위치에서 뮤테이션 옵션을 공유하는 가장 좋은 방법 중 하나입니다,
mutationOptions 헬퍼를 사용하는 것입니다. 런타임에서 이 헬퍼는 전달된 값을 그대로 반환할 뿐이며,
하지만 TypeScript와 함께 사용하면 많은 이점이 있습니다.
뮤테이션에 사용할 수 있는 모든 옵션을 한곳에서 정의할 수 있으며,
또한 모든 쿼리에 대해 타입 추론과 타입 안전성을 확보할 수 있습니다.
export class QueriesService {
private 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)
},
})
}
}