Skip to content

Commit

Permalink
fix(query-core): fix context type error in onSuccess (#6355)
Browse files Browse the repository at this point in the history
* fix(query-core): fix context type error in `onSuccess`

* docs(query-core): update `useMutation` documentations

* fix(query-core): the `context` should not be inferred as `undefined` when `TContext` is defined

---------

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
  • Loading branch information
Mini-ghost and TkDodo committed Dec 17, 2023
1 parent c38c1cb commit 320cf9b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/react/reference/useMutation.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ mutate(variables, {
- This function will fire before the mutation function is fired and is passed the same variables the mutation function would receive
- Useful to perform optimistic updates to a resource in hopes that the mutation succeeds
- The value returned from this function will be passed to both the `onError` and `onSettled` functions in the event of a mutation failure and can be useful for rolling back optimistic updates.
- `onSuccess: (data: TData, variables: TVariables, context?: TContext) => Promise<unknown> | unknown`
- `onSuccess: (data: TData, variables: TVariables, context: TContext) => Promise<unknown> | unknown`
- Optional
- This function will fire when the mutation is successful and will be passed the mutation's result.
- If a promise is returned, it will be awaited and resolved before proceeding
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export class Mutation<
this as Mutation<unknown, unknown, unknown, unknown>,
)

await this.options.onSuccess?.(data, variables, this.state.context)
await this.options.onSuccess?.(data, variables, this.state.context!)

// Notify cache callback
await this.#mutationCache.config.onSettled?.(
Expand Down
26 changes: 8 additions & 18 deletions packages/query-core/src/mutationObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,29 +144,19 @@ export class MutationObserver<
notifyManager.batch(() => {
// First trigger the mutate callbacks
if (this.#mutateOptions && this.hasListeners()) {
const variables = this.#currentResult.variables!
const context = this.#currentResult.context

if (action?.type === 'success') {
this.#mutateOptions.onSuccess?.(
action.data,
this.#currentResult.variables!,
this.#currentResult.context!,
)
this.#mutateOptions.onSettled?.(
action.data,
null,
this.#currentResult.variables!,
this.#currentResult.context,
)
this.#mutateOptions.onSuccess?.(action.data, variables, context!)
this.#mutateOptions.onSettled?.(action.data, null, variables, context)
} else if (action?.type === 'error') {
this.#mutateOptions.onError?.(
action.error,
this.#currentResult.variables!,
this.#currentResult.context,
)
this.#mutateOptions.onError?.(action.error, variables, context)
this.#mutateOptions.onSettled?.(
undefined,
action.error,
this.#currentResult.variables!,
this.#currentResult.context,
variables,
context,
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ export interface MutationOptions<
onSuccess?: (
data: TData,
variables: TVariables,
context: TContext | undefined,
context: TContext,
) => Promise<unknown> | unknown
onError?: (
error: TError,
Expand Down

0 comments on commit 320cf9b

Please sign in to comment.