-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
feat(query-core): make MutateFunction
optional undefinable-variables
#8737
base: main
Are you sure you want to change the base?
feat(query-core): make MutateFunction
optional undefinable-variables
#8737
Conversation
bump |
number | undefined | ||
>() | ||
|
||
mutate() // can be called with no arguments |
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.
that’s a good improvement 👍 . I’m missing a test for the use-case where we have a mutation function that doesn’t take anything in, so the variables are void
.
Also, I’d prefer if we create the mutation functions not via type assertions, but by whatever a MutationObserver
returns. Something like:
const { mutate } = new MutationObserver(new QueryClient(), {
mutationFn: async (_vars: number | undefined) => {
return null
},
})
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.
I've added the void
case as requested. However, I couldn’t create a test using MutationObserver
because the mutate
function is implemented as a standalone function rather than using MutateFunction
. As a result, the changes don’t impact its behavior. Let me know if you have any suggestions on how to approach testing this scenario.
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.
oh interesting. question is if it would also work with the mutate
function returned from useMutation
, as that is what most users will use. The type is defined here:
query/packages/react-query/src/types.ts
Lines 197 to 204 in 3e3fba9
export type UseMutateFunction< | |
TData = unknown, | |
TError = DefaultError, | |
TVariables = void, | |
TContext = unknown, | |
> = ( | |
...args: Parameters<MutateFunction<TData, TError, TVariables, TContext>> | |
) => void |
would be great to have a test in useMutation.test-d.tsx
in the react-query adapter for this then 🙏
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.
I've already covered this case. Please check my tests.
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.
I've already covered this case
yeah that’s okay for the query-core
, but it doesn’t actually test what useMutation
in react-query does. It’s great that we cover the functionality that we have in useMutation
now, but if we refactor that, it won’t be covered by the test in the core.
That’s why each adapter should have its own tests for the types they are doing. But we can do this in a follow-up.
View your CI Pipeline Execution ↗ for commit 01b77b4.
☁️ Nx Cloud last updated this comment at |
there are type errors now in other adapters |
Old Behavior:
.mutate
,.mutateAsync
, ... allowed omitting thevariables
parameter only when its type was exactlyvoid
.However, in TypeScript, when using union types like
unknown | void
, thevoid
portion is ignored and treated asunknown
, which still requires thatvariables
be provided.New Behavior:
.mutate
,.mutateAsync
, ... now allow omitting thevariables
parameter when its type can beundefined
.This change significantly broadens the optional types, supporting cases such as
unknown
,any
,undefined | ...
,void | ...
, ...Another benefit of the new approach is that, for popular schema libraries like Zod and Valibot, it is easier to define an undefinable schema than a void-able one.