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

mutation-options.ts

117 lines4.0 KB

1import type { DefaultError, WithRequired } from '@tanstack/query-core'
2import type { CreateMutationOptions } from './types'
3​
4/**
5 * You can generally pass everything to `mutationOptions` that you can also pass to `injectMutation`. A
6 * `mutationKey` is required on this overload so the mutation can be looked up later, e.g. with
7 * `injectMutationState`.
8 * @param options - The mutation options to use, identical to what you'd pass to `injectMutation`, with a
9 * required `mutationKey`.
10 * @returns The same options object, unchanged.
11 * @see {@link injectMutation} to run the mutation these options describe.
12 * @example
13 * Looking the mutation up elsewhere via its `mutationKey`, e.g. for a global "saving…" indicator:
14 * ```angular-ts
15 * import { mutationOptions, injectMutationState } from '@tanstack/angular-query'
16 *
17 * const createPostOptions = mutationOptions({
18 * mutationKey: ['posts', 'create'],
19 * mutationFn: createPost,
20 * })
21 *
22 * @Component({
23 * selector: 'saving-indicator',
24 * template: `
25 * @if (isCreatingPost()) {
26 * <span>Saving…</span>
27 * }
28 * `,
29 * })
30 * export class SavingIndicator {
31 * readonly #pendingCreates = injectMutationState(() => ({
32 * filters: { mutationKey: createPostOptions.mutationKey, status: 'pending' },
33 * }))
34 * readonly isCreatingPost = computed(() => this.#pendingCreates().length > 0)
35 * }
36 * ```
37 */
38export function mutationOptions<
39 TData = unknown,
40 TError = DefaultError,
41 TVariables = void,
42 TOnMutateResult = unknown,
43>(
44 options: WithRequired<
45 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
46 'mutationKey'
47 >,
48): WithRequired<
49 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
50 'mutationKey'
52/**
53 * You can generally pass everything to `mutationOptions` that you can also pass to `injectMutation`. No
54 * `mutationKey` is required on this overload — use this when you don't need to target the mutation via a
55 * `mutationKey` filter later (e.g. with `injectMutationState`); it can still be observed through other
56 * filters, such as `status`.
57 * @param options - The mutation options to use, identical to what you'd pass to `injectMutation`, without a
58 * `mutationKey`.
59 * @returns The same options object, unchanged.
60 * @remarks See the other overload's example for looking a mutation up via `injectMutationState`.
61 * @see {@link injectMutation} to run the mutation these options describe.
62 * @example
63 * Sharing options across services, so `QueriesService` stays the single place a mutation is defined:
64 * ```angular-ts
65 * import { mutationOptions, injectMutation } from '@tanstack/angular-query'
66 *
67 * @Injectable({ providedIn: 'root' })
68 * export class QueriesService {
69 * readonly #queryClient = inject(QueryClient)
70 *
71 * updatePost(id: number) {
72 * return mutationOptions({
73 * mutationFn: (post: Partial<Post>) => putPost(id, post),
74 * onSuccess: (newPost) => this.#queryClient.setQueryData(['posts', id], newPost),
75 * })
76 * }
77 * }
78 *
79 * @Component({
80 * selector: 'post',
81 * template: `<button (click)="save()">Save</button>`,
82 * })
83 * export class Post {
84 * readonly queries = inject(QueriesService)
85 * readonly id = signal(0)
86 * readonly updatePostMutation = injectMutation(() => this.queries.updatePost(this.id()))
87 *
88 * save() {
89 * this.updatePostMutation.mutate({ title: 'New Title' })
90 * }
91 * }
92 * ```
93 */
94export function mutationOptions<
95 TData = unknown,
96 TError = DefaultError,
97 TVariables = void,
98 TOnMutateResult = unknown,
99>(
100 options: Omit<
101 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
102 'mutationKey'
103 >,
104): Omit<
105 CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
106 'mutationKey'
108export function mutationOptions<
109 TData = unknown,
110 TError = DefaultError,
111 TVariables = void,
112 TOnMutateResult = unknown,
113>(
114 options: CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>,
115): CreateMutationOptions<TData, TError, TVariables, TOnMutateResult> {
116 return options