|
I am wondering if there is a way to have the global query client that gets used automatically for things like useQuery to be passed as context to a mutationOptions object. This would be useful for updating the query cache from the mutation responses. |
Answered by
lMysticl
Aug 2, 2026
Replies: 1 comment 1 reply
|
Yes. In current TanStack Query v5, mutation callbacks receive a const options = mutationOptions({
mutationFn: saveTodo,
onSuccess: (data, _variables, _onMutateResult, { client }) => {
client.setQueryData(['todo', data.id], data)
},
})You can use the same client for invalidation: onSuccess: async (_data, _variables, _onMutateResult, { client }) => {
await client.invalidateQueries({ queryKey: ['todos'] })
}So you do not need to pass the client into the options factory manually. If your installed version does not expose the fourth argument, it predates the mutation context added in #9615 and needs to be upgraded. |
1 reply
Answer selected by
eprig
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes. In current TanStack Query v5, mutation callbacks receive a
MutationFunctionContextas their final argument, and itsclientfield is theQueryClientthat owns the mutation:You can use the same client for invalidation:
So you do not need to pass the client into the options factory manually. If your installed version does not expose the fourth argument, it predates the mutation c…