-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
feat(query-core): add MutationFunctionContext argument to mutateFn #9193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
add new argument to mutateFn enabling access to queryClient as well as meta object
we already have a PR for this: can we consolidate the work in one PR? I think we’d want 3 properties passed:
|
This PR adds new mutationKey property to MutationFunctionContext that's passed to mutateFn.
@TkDodo updated as requested |
View your CI Pipeline Execution ↗ for commit 2c1a713.
☁️ Nx Cloud last updated this comment at |
- **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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
query/packages/query-core/src/mutation.ts
Line 29 in 02c3d51
context: TContext | undefined |
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 ?
This PR adds new argument to
mutateFn
allowing for access toqueryClient
as well asmeta
object.This feature should make it easier to attach authentication headers from within
mutateFn
, that would typically live inQueryCache
. Currently that's made very difficult and requires either having singleton context in the codebase or requiring consumers of mutations to pass auth token each time.This PR also makes it more similar to how
queryFn
behaves, in that it acceptsQueryFunctionContext
.Note: I wasn't entirely sure about the name of
MutateFunctionContext
, but ultimately I decided to go ahead with that name to mirror the wayqueryFn
behaves. I realise this might be suboptimal, due to there already existing aTContext
for mutations. Happy to discuss and take direction here.