Skip to content
Source
Sign inSign up
TanStack/query

feat(query-core): add MutationFunctionContext argument to mutateFn #9193

Closedtravikk(GitHub profile, opens in a new tab)wants to merge 4 commits intomain fromrefs/pull/9193/headopened View on GitHub

Showing 6 changed files with 33 additions and 2 deletions (loading 3).

x
0 of 6 viewed, 6 left
2 additions and 1 deletiondocs/framework/react/reference/useMutation.mdModified
View file
@@ -48,10 +48,11 @@
​
**Parameter1 (Options)**
​
-- `mutationFn: (variables: TVariables) => Promise<TData>`
+- `mutationFn: (variables: TVariables, context: MutationFunctionContext) => Promise<TData>`
- **Required, but only if no default mutation function has been defined**
- A function that performs an asynchronous task and returns a promise.
- `variables` is an object that `mutate` will pass to your `mutationFn`
+ - `context` is an object that `mutate` will pass to your `mutationFn`. Contains reference to `QueryClient`, `mutationKey` and optional `meta` object.
Thread on line 55
TkDodocommented

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:

https://tanstack.com/query/v5/docs/framework/react/guides/optimistic-updates#updating-a-list-of-todos-when-adding-a-new-todo

and in code here:

https://github.com/TanStack/query/blob/02c3d5130e063c5f623adbfaf9bd986273b615cc/packages/query-core/src/mutation.ts#L29

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:

  • QueryFunctionParams and MutationFunctionParams
  • QueryFunctionInfo and MutationFunctionInfo
  • QueryFunctionExecutionContext and MutationFunctionExecutionContext
  • QueryFunctionDetails and MutationFunctionDetails
  • QueryFunctionMetaContext and MutationFunctionMetaContext

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?

TkDodocommented

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)
}
TkDodocommented

@Rendez let’s do it 👍

- `gcTime: number | Infinity`
- The time in milliseconds that unused/inactive cache data remains in memory. When a mutation's cache becomes unused or inactive, that cache data will be garbage collected after this duration. When different cache times are specified, the longest one will be used.
- If set to `Infinity`, will disable garbage collection
30 additions and 1 deletionpackages/query-core/src/__tests__/mutations.test.tsxModified
View file