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

mutation-options.ts

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