Closed denisborovikov closed 6 months ago
Using option onMutate and mutationFn’s context.
Sorry Now I'm on vacation in Thailand without my Mac. So I can not show u the code quickly.
Sure thing, thanks! Enjoy the vacation!
For the later discussion - I refreshed my memory and checked the docs. The mutationFn
doesn't receive any other arguments than variables. The context returned from the onMutate
is supposed to be used for the optimistic updates and passed down to other callbacks (onSuccess, etc.) but not to the mutation function.
I forgot it. Or You can pass a new mutationFn
that warped the original mutationFn
. For example
const mutationMiddleware: Middleware<MutationHook> = (useMutationNext) => {
return (options) => {
const mutationFn = (variables) => {
const mergedVariables = { foo: 'bar', ...variables}
return options.mutationFn(mergedVariables)
}
return useMutationNext({ ...options, mutationFn })
}
}
Aw right, thank you for the insight! I'll give it a try.
I'm trying to pass data from my middleware to the
mutationFn
. The only argumentmutationFn
has isvariables
but I don't seevariables
exist inmutationMiddleware
options.Anyway even if I suppress the ts error, the variables from the middleware are overriden with variables from the mutate: