Skip to content

Commit 9754d4f

Browse files
committed
feat(core): add context as second parameter of mutation function
1 parent 2aca521 commit 9754d4f

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
@@ -729,8 +729,14 @@ export type MutationMeta = Register extends {
729729
: Record<string, unknown>
730730
: Record<string, unknown>
731731

732+
export type MutationFunctionContext = {
733+
mutationKey?: MutationKey
734+
meta: MutationMeta | undefined
735+
}
736+
732737
export type MutationFunction<TData = unknown, TVariables = unknown> = (
733738
variables: TVariables,
739+
context: MutationFunctionContext,
734740
) => Promise<TData>
735741

736742
export interface MutationOptions<

0 commit comments

Comments
 (0)