Skip to content

Commit aee68ae

Browse files
committed
feat(core): add context as second parameter of mutation function
1 parent 51d3e8f commit aee68ae

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

packages/query-core/src/mutation.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Removable } from './removable'
33
import { createRetryer } from './retryer'
44
import type {
55
DefaultError,
6+
MutationFunctionContext,
67
MutationMeta,
78
MutationOptions,
89
MutationStatus,
@@ -167,7 +168,11 @@ export class Mutation<
167168
if (!this.options.mutationFn) {
168169
return Promise.reject(new Error('No mutationFn found'))
169170
}
170-
return this.options.mutationFn(variables)
171+
const mutationFnContext: MutationFunctionContext = {
172+
mutationKey: this.options.mutationKey,
173+
meta: this.meta,
174+
}
175+
return this.options.mutationFn(variables, mutationFnContext)
171176
},
172177
onFail: (failureCount, error) => {
173178
this.#dispatch({ type: 'failed', failureCount, error })

packages/query-core/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,14 @@ export type MutationMeta = Register extends {
897897
: Record<string, unknown>
898898
: Record<string, unknown>
899899

900+
export type MutationFunctionContext = {
901+
mutationKey?: MutationKey
902+
meta: MutationMeta | undefined
903+
}
904+
900905
export type MutationFunction<TData = unknown, TVariables = unknown> = (
901906
variables: TVariables,
907+
context: MutationFunctionContext,
902908
) => Promise<TData>
903909

904910
export interface MutationOptions<

0 commit comments

Comments
 (0)