Skip to content

Commit 0c58762

Browse files
committed
feat(core): add context as second parameter of mutation function
1 parent 59eef10 commit 0c58762

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
@@ -888,8 +888,14 @@ export type MutationMeta = Register extends {
888888
: Record<string, unknown>
889889
: Record<string, unknown>
890890

891+
export type MutationFunctionContext = {
892+
mutationKey?: MutationKey
893+
meta: MutationMeta | undefined
894+
}
895+
891896
export type MutationFunction<TData = unknown, TVariables = unknown> = (
892897
variables: TVariables,
898+
context: MutationFunctionContext,
893899
) => Promise<TData>
894900

895901
export interface MutationOptions<

0 commit comments

Comments
 (0)