feat: generate useMutation hooks for TanStack React Query plugin#3432
feat: generate useMutation hooks for TanStack React Query plugin#3432nmokkenstorm wants to merge 1 commit intohey-api:mainfrom
Conversation
|
|
|
Leaping into action... |
|
|
@nmokkenstorm is attempting to deploy a commit to the Hey API Team on Vercel. A member of the Team first needs to authorize it. |
It probably should actually? It's a non-breaking feature addition, happy to take some pointers re: changesets setup. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #3432 +/- ##
==========================================
- Coverage 39.37% 39.31% -0.07%
==========================================
Files 475 476 +1
Lines 17454 17491 +37
Branches 5276 5286 +10
==========================================
+ Hits 6873 6876 +3
- Misses 8498 8525 +27
- Partials 2083 2090 +7
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
mrlubos
left a comment
There was a problem hiding this comment.
Not much I can say, this pull request looks very clean!
| case: plugin.config.case ?? 'camelCase', | ||
| enabled: false, | ||
| name: 'use{{name}}', | ||
| requestOptions: false, |
There was a problem hiding this comment.
This line is redundant I think?
| requestOptions: false, |
There was a problem hiding this comment.
hmmmm I think I should just remove that option I think, I originally had some configurability but I think it just needs to go.
I'll take it out, it'll be consistent with the useQuery option.
| * | ||
| * @default false | ||
| */ | ||
| useMutation?: |
There was a problem hiding this comment.
I wonder if we should have useQuery and useMutation for any other frameworks?
There was a problem hiding this comment.
To clarify, this is NOT a request for this pull request, just an observation
There was a problem hiding this comment.
I think the better thing is to take them both out for the other frameworks tbh, but adding the option in in the type is the consistent thing for now.
I wouldn't have added it in if the type check didn't fail on it. Agreed that changing the structure to accommodate did seem out of scope for this PR, definitely open to workshopping something later.
| defaultValue: { | ||
| case: plugin.config.case ?? 'camelCase', | ||
| enabled: false, | ||
| name: 'use{{name}}', |
There was a problem hiding this comment.
Thoughts on this to stay in sync with the use{{name}}Query pattern below?
| name: 'use{{name}}', | |
| name: 'use{{name}}Mutation', |
There was a problem hiding this comment.
Ah yes agreed, I'll change it. I think the default of looking at the operation name was enough for my codebase to be clear but I don't think that generalises well and a suffix is objectively more consistent 👍
There was a problem hiding this comment.
Did you see projects like https://github.com/7nohe/openapi-react-query-codegen or other generators? I think the ideal state is this plugin would be to eventually cover most of the extra functionality they offer. You mentioned you have multiple pull requests, what are the other ones about?
There was a problem hiding this comment.
the fork I run locally has three features that are not supported in the latest release, one is what we talk about here. the others:
- expose the rest of the query options through usequery, see here
- allow skiptoken through that, this allows for typesafe conditional query execution without hard casts, but definitely requires opt-in. it's not a breaking change (I think?) but it does have a potential performance/bundle size impact, see here
the public github is not as clean as I'd like because I did some ad-hoc extraction, cleaning it up and making it work within the ecosystem of the public heyapi github repository is non-trivial effort so I wanted to test the waters first with the current PR we're disccusing.
if you want and timezones/agenda allows I'm also more than open to a more direct chat than github comments. I saw you're on bsky, I'm there as mokkenstorm.dev
Thank you 🫶🏻 it's a relatively simple change that composes well and saves my personally quite a bit of boilerplate in application code so I thought I'd share. I do need a bit of time to figure out the testing strategy/merge checks, lmk if you have any tips there? |
|
@nmokkenstorm It looks good to me. You can run tests locally if you'd like to confirm. That heavy typecheck step is flaky in CI, I'm meant to split those tests because it's gotten too large, but other things are more important |
|
I'll fix what we discussed, run some local tests, report what I did and then we can continue 👍 |

I wanted to expose react/tanstack query mutations in the same way the useQuery works, so now you get
useFooMutation(), no need to wire that through together. It greatly simplifies the consumption of mutation related code in a (private/corporate) codebase that I contribute to. We've been running a local fork with this feature.The generated
useMutationhooks reduce boilerplate in application/consumer code and save lot on small files that do manual wiring:Before:
After:
Before:
After:
One choice/tradeoff I made was to put the mutation options as a first positional argument, as the resulting mutation function seemed the neater way to pass in actual function arguments:
Without this ordering, every consumer that wants to pass
onSuccesshas to write:which was the more naive implementation I had at first, but the undefined everywhere for the positional arguments was bothering me. Now you set up your request stuff in the hook call, and request data in the execution of the resulting function, which seemed the more ergonomic approach.
This PR is first of three, as mentioned I am running a fork with some additional things for my project. I want use this as a starting point to get some discussion going and figure out how to collaborate / if a PR like this is appreciated or not. The actual code diff isn't that big, it's mostly wiring through a parameter that works the same way for
useQueryand the snapshot tests.