Skip to content

Conversation

@matthewhausman
Copy link
Contributor

@matthewhausman matthewhausman commented Oct 18, 2022

Definitely need someone to help verify this implementation as I am not intimately familiar with how tanstack query is used professionally, only generally familiar.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 18, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 830ecfc:

Sandbox Source
@tanstack/query-example-react-basic-typescript Configuration
@tanstack/query-example-solid-basic-typescript Configuration
@tanstack/query-example-vue-basic Configuration

@matthewhausman matthewhausman changed the title Make use mutate reactive to argument changes feat(vue-query): Make use mutate reactive to argument changes Oct 18, 2022
@matthewhausman matthewhausman changed the title feat(vue-query): Make use mutate reactive to argument changes fix(vue-query): Make use mutate reactive to argument changes Oct 18, 2022
@DamianOsipiuk
Copy link
Contributor

So, from my point of view, there are a lot of unnecessary changes.
How i would see this implemented is to add something like this in types.ts:

// A Vue version of MutationObserverOptions from "@tanstack/query-core"
// Accept refs as options
export type VueMutationObserverOptions<
  TData = unknown,
  TError = unknown,
  TVariables = void,
  TContext = unknown,
> = {
  [Property in keyof MutationObserverOptions<
    TData,
    TError,
    TVariables,
    TContext
  >]: MaybeRefDeep<
    MutationObserverOptions<TData, TError, TVariables, TContext>[Property]
  >
}

and then use it in https://github.com/TanStack/query/blob/main/packages/vue-query/src/useMutation.ts#L23

This will also require change in
https://github.com/TanStack/query/blob/main/packages/vue-query/src/useMutation.ts#L168
and
https://github.com/TanStack/query/blob/main/packages/vue-query/src/useMutation.ts#L183

to use non reffed version of options which is currently at https://github.com/TanStack/query/blob/main/packages/vue-query/src/useMutation.ts#L23

@matthewhausman
Copy link
Contributor Author

@DamianOsipiuk thanks for the feedback gonna start on that now

@matthewhausman
Copy link
Contributor Author

matthewhausman commented Oct 20, 2022

@DamianOsipiuk let me know what ya think! I'd like if functions could also be accepted as refs by tanstack query if possible but what do you think? (hence the change to the deep ref function)

test('should return error when request fails', async () => {
const mutation = useMutation(errorMutator)

mutation.mutate()
Copy link
Contributor

Choose a reason for hiding this comment

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

I do not think changes in this file are relevant

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok will take another look - for some reason or another passing no args was failing the tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Screen Shot 2022-10-22 at 4 33 44 PM

Any idea why these changes would cause something as such?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually no idea.
Does it work on main branch?
What TS version do you have installed?

Copy link
Contributor Author

@matthewhausman matthewhausman Oct 22, 2022

Choose a reason for hiding this comment

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

that test is running on your cloud test suite - not my local so not sure if my ts version is relevant

Copy link
Contributor Author

Choose a reason for hiding this comment

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

export type MutateFunction<
TData = unknown,
TError = unknown,
TVariables = void,
TContext = unknown,

= (
variables: TVariables,
options?: MutateOptions<TData, TError, TVariables, TContext>,
) => Promise

it seems completely reasonable to get an error for not passing args - variables is not optional. Maybe I somehow removed a Partial somewhere... pretty strange

@matthewhausman
Copy link
Contributor Author

I have a separate concern with the current typing of the arguments - I see the most popular way to create reactive arguments to be to make a single options object, so const options = ref({ mutationKey: ..., mutationFn: ... })

currently, we do not allow for such an argument to be passed. I generally think that we should adjust the type signature to something like what I have just committed

export type UseMutationOptions<TData, TError, TVariables, TContext> =
WithQueryClientKey<
MutationObserverOptions<TData, TError, TVariables, TContext>

The mutation options above are all non-reactive

export type VueMutationObserverOptions<
TData = unknown,
TError = unknown,
TVariables = void,
TContext = unknown,

= {
[Property in keyof UseMutationOptions<
TData,
TError,
TVariables,
TContext
]: MaybeRefDeep<
UseMutationOptions<TData, TError, TVariables, TContext>[Property]

}

the Vue options use maybe deep ref on each key....

options: MaybeRef<VueMutationObserverOptions<TData, TError, TVariables, TContext>>,

the object itself is allowed to be reactive

@matthewhausman
Copy link
Contributor Author

Lastly I have tried all of the prettier formatting commands defined in package.json but still getting errors in the workflow tests somehow, any idea how to fix that?

@DamianOsipiuk
Copy link
Contributor

Lastly I have tried all of the prettier formatting commands defined in package.json but still getting errors in the workflow tests somehow, any idea how to fix that?

yeah, i think the write script was not updated after the switch to pnpm.

Change pnpm run prettier -- --write to pnpm run prettier --write and then run it and it should work

@matthewhausman
Copy link
Contributor Author

worked like a charm thanks!!!!!

@codecov-commenter
Copy link

codecov-commenter commented Oct 22, 2022

Codecov Report

Base: 96.36% // Head: 92.28% // Decreases project coverage by -4.07% ⚠️

Coverage data is based on head (830ecfc) compared to base (eab6e2c).
Patch has no changes to coverable lines.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4339      +/-   ##
==========================================
- Coverage   96.36%   92.28%   -4.08%     
==========================================
  Files          45       87      +42     
  Lines        2281     3563    +1282     
  Branches      640      904     +264     
==========================================
+ Hits         2198     3288    +1090     
- Misses         80      259     +179     
- Partials        3       16      +13     
Impacted Files Coverage Δ
src/react/Hydrate.tsx
src/core/onlineManager.ts
src/core/retryer.ts
src/core/queriesObserver.ts
src/react/reactBatchedUpdates.ts
src/react/useMutation.ts
src/core/mutation.ts
src/devtools/useLocalStorage.ts
src/core/infiniteQueryObserver.ts
src/devtools/Explorer.tsx
... and 122 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@matthewhausman
Copy link
Contributor Author

matthewhausman commented Oct 22, 2022

Woooo 🎉🎉🎉🎉🎉🎉🎉 test passed! lmk if you want to zoom or something and go over before we move forward. Thank you so much @DamianOsipiuk for the help it means so much to help with my first open source contribution :)

@DamianOsipiuk DamianOsipiuk merged commit 915d5e9 into TanStack:main Oct 22, 2022
@DamianOsipiuk
Copy link
Contributor

Thanks for the contribution @matthewhausman 🚀

@TkDodo
Copy link
Collaborator

TkDodo commented Oct 23, 2022

@allcontributors add @matthewhausman for code

@allcontributors
Copy link
Contributor

@TkDodo

I've put up a pull request to add @matthewhausman! 🎉

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.

4 participants