Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

unnoq
Copy link

@unnoq unnoq commented Mar 3, 2025

Old Behavior:
.mutate, .mutateAsync, ... allowed omitting the variables parameter only when its type was exactly void.

However, in TypeScript, when using union types like unknown | void, the void portion is ignored and treated as unknown, which still requires that variables be provided.

New Behavior:
.mutate, .mutateAsync, ... now allow omitting the variables parameter when its type can be undefined.

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.

@lonelyplanetdev
Copy link

bump

number | undefined
>()

mutate() // can be called with no arguments
Copy link
Collaborator

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

Copy link
Author

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.

Copy link
Collaborator

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:

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 🙏

Copy link
Author

@unnoq unnoq Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

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.

Copy link

nx-cloud bot commented Mar 5, 2025

View your CI Pipeline Execution ↗ for commit 01b77b4.

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ❌ Failed 2m 55s View ↗
nx run-many --target=build --exclude=examples/*... ❌ Failed 1m 15s View ↗

☁️ Nx Cloud last updated this comment at 2025-03-05 14:47:53 UTC

@TkDodo
Copy link
Collaborator

TkDodo commented Mar 5, 2025

there are type errors now in other adapters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants