Skip to content

Commit ea2cc38

Browse files
committed
feat(query-core): add mutation context to all mutation callbacks
1 parent 2ae2fbe commit ea2cc38

File tree

4 files changed

+69
-8
lines changed

4 files changed

+69
-8
lines changed

packages/query-core/src/mutation.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ export class Mutation<
175175
this.#dispatch({ type: 'continue' })
176176
}
177177

178-
const mutationFnContext: MutationFunctionContext = {
178+
const mutationFnContext = {
179179
client: this.#client,
180180
meta: this.options.meta,
181181
mutationKey: this.options.mutationKey,
182-
}
182+
} satisfies MutationFunctionContext
183183

184184
this.#retryer = createRetryer({
185185
fn: () => {
@@ -239,7 +239,12 @@ export class Mutation<
239239
this as Mutation<unknown, unknown, unknown, unknown>,
240240
)
241241

242-
await this.options.onSuccess?.(data, variables, this.state.scope!)
242+
await this.options.onSuccess?.(
243+
data,
244+
variables,
245+
this.state.scope,
246+
mutationFnContext,
247+
)
243248

244249
// Notify cache callback
245250
await this.#mutationCache.config.onSettled?.(
@@ -250,7 +255,13 @@ export class Mutation<
250255
this as Mutation<unknown, unknown, unknown, unknown>,
251256
)
252257

253-
await this.options.onSettled?.(data, null, variables, this.state.scope)
258+
await this.options.onSettled?.(
259+
data,
260+
null,
261+
variables,
262+
this.state.scope,
263+
mutationFnContext,
264+
)
254265

255266
this.#dispatch({ type: 'success', data })
256267
return data
@@ -268,6 +279,7 @@ export class Mutation<
268279
error as TError,
269280
variables,
270281
this.state.scope,
282+
mutationFnContext,
271283
)
272284

273285
// Notify cache callback
@@ -284,6 +296,7 @@ export class Mutation<
284296
error as TError,
285297
variables,
286298
this.state.scope,
299+
mutationFnContext,
287300
)
288301
throw error
289302
} finally {

packages/query-core/src/mutationObserver.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { QueryClient } from './queryClient'
66
import type {
77
DefaultError,
88
MutateOptions,
9+
MutationFunctionContext,
910
MutationObserverOptions,
1011
MutationObserverResult,
1112
} from './types'
@@ -148,16 +149,34 @@ export class MutationObserver<
148149
const variables = this.#currentResult.variables!
149150
const scope = this.#currentResult.scope
150151

152+
const context = {
153+
client: this.#client,
154+
meta: this.options.meta,
155+
mutationKey: this.options.mutationKey,
156+
} satisfies MutationFunctionContext
157+
151158
if (action?.type === 'success') {
152-
this.#mutateOptions.onSuccess?.(action.data, variables, scope!)
153-
this.#mutateOptions.onSettled?.(action.data, null, variables, scope)
159+
this.#mutateOptions.onSuccess?.(
160+
action.data,
161+
variables,
162+
scope,
163+
context,
164+
)
165+
this.#mutateOptions.onSettled?.(
166+
action.data,
167+
null,
168+
variables,
169+
scope,
170+
context,
171+
)
154172
} else if (action?.type === 'error') {
155-
this.#mutateOptions.onError?.(action.error, variables, scope)
173+
this.#mutateOptions.onError?.(action.error, variables, scope, context)
156174
this.#mutateOptions.onSettled?.(
157175
undefined,
158176
action.error,
159177
variables,
160178
scope,
179+
context,
161180
)
162181
}
163182
}

packages/query-core/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,17 +1118,20 @@ export interface MutationOptions<
11181118
data: TData,
11191119
variables: TVariables,
11201120
scope: TScope | undefined,
1121+
context: MutationFunctionContext,
11211122
) => Promise<unknown> | unknown
11221123
onError?: (
11231124
error: TError,
11241125
variables: TVariables,
11251126
scope: TScope | undefined,
1127+
context: MutationFunctionContext,
11261128
) => Promise<unknown> | unknown
11271129
onSettled?: (
11281130
data: TData | undefined,
11291131
error: TError | null,
11301132
variables: TVariables,
11311133
scope: TScope | undefined,
1134+
context: MutationFunctionContext,
11321135
) => Promise<unknown> | unknown
11331136
retry?: RetryValue<TError>
11341137
retryDelay?: RetryDelayValue<TError>
@@ -1158,17 +1161,20 @@ export interface MutateOptions<
11581161
data: TData,
11591162
variables: TVariables,
11601163
scope: TScope | undefined,
1164+
context: MutationFunctionContext,
11611165
) => void
11621166
onError?: (
11631167
error: TError,
11641168
variables: TVariables,
11651169
scope: TScope | undefined,
1170+
context: MutationFunctionContext,
11661171
) => void
11671172
onSettled?: (
11681173
data: TData | undefined,
11691174
error: TError | null,
11701175
variables: TVariables,
11711176
scope: TScope | undefined,
1177+
context: MutationFunctionContext,
11721178
) => void
11731179
}
11741180

packages/react-query/src/__tests__/mutationOptions.test-d.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useIsMutating, useMutation, useMutationState } from '..'
44
import { mutationOptions } from '../mutationOptions'
55
import type {
66
DefaultError,
7+
MutationFunctionContext,
78
MutationState,
89
WithRequired,
910
} from '@tanstack/query-core'
@@ -62,7 +63,29 @@ describe('mutationOptions', () => {
6263
return { name: 'scope' }
6364
},
6465
onSuccess: (_data, _variables, scope) => {
65-
expectTypeOf(scope).toEqualTypeOf<{ name: string }>()
66+
expectTypeOf(scope).toEqualTypeOf<{ name: string } | undefined>()
67+
},
68+
})
69+
})
70+
71+
it('should infer context type correctly', () => {
72+
mutationOptions<number>({
73+
mutationFn: (_variables, context) => {
74+
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
75+
return Promise.resolve(5)
76+
},
77+
mutationKey: ['key'],
78+
onMutate: (_variables, context) => {
79+
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
80+
},
81+
onSuccess: (_data, _variables, _scope, context) => {
82+
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
83+
},
84+
onError: (_error, _variables, _scope, context) => {
85+
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
86+
},
87+
onSettled: (_data, _error, _variables, _scope, context) => {
88+
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
6689
},
6790
})
6891
})

0 commit comments

Comments
 (0)