feat(query-core): add MutationFunctionContext argument to mutateFn #9193
main fromrefs/pull/9193/head·opened View on GitHubShowing 6 changed files with 33 additions and 2 deletions (loading 3).
there’s one more thing that’s a bit troubling, and that’s the fact that we already refer to something as “context” for mutations: The value that gets returned from onMutate, which can be seen in this example here:
and in code here:
since it’s part of the public MutationState, which is available via useMutationState, we can’t just rename this, so it’s a bit unfortunate.
Any ideas ?
Given that the equivalent object in queryFn is already called context, it seems the trade off is consistency with QueryFunctionContext context vs a conflict with MutationState context.
One idea is to rename the QueryFunctionContext to something else and name this new mutation function context accordingly. Examples:
QueryFunctionParamsandMutationFunctionParamsQueryFunctionInfoandMutationFunctionInfoQueryFunctionExecutionContextandMutationFunctionExecutionContextQueryFunctionDetailsandMutationFunctionDetailsQueryFunctionMetaContextandMutationFunctionMetaContext
This way we get consistency between queryFn and mutationFn, but eliminate the conflict with MutationState.
Honestly I don't love any of these, but I think the root of that issue is that these objects and the context in MutationState are both most accurately described as a "context".
@travikk light bump on this!
Hey apologies for missing the updates.
Yeah, I totally agree with @solastley here. There doesn't seem to be a preferred way forward here, but there's clearly a benefit in having consistency in API between queryFn and mutateFn in that we can access QueryCache or QueryClient from both.
Can we perhaps bet on a slightly tuned/better documentation to distinguish between mutateFn context proposed here and return value of onMutate?
@TkDodo do you mind weighing in?
would it be a breaking change to rename the return value of onMutate to something else? After all, it’s just as positional argument. I never liked context for this...
How about calling it scope?
mutationFn(vars, context) {
if (context.meta.something) {
// ...
}
return fetch('...');
},
onMutate(_vars, context) {
if (context.client) {
// ...
}
const scope = {foo: 'bar'}
return scope;
},
onSuccess(_data, _vars, scope) {
console.log(scope)
}@Rendez let’s do it 👍