openapi-react-query-codegen icon indicating copy to clipboard operation
openapi-react-query-codegen copied to clipboard

Prefetch hook keys aren't properly set

Open nopitown opened this issue 1 year ago • 6 comments
trafficstars

Describe the bug I started using the prefetch hooks introduced a few days ago, but the dynamic key is hardcoded, making it challenging to use the prefetched data because the keys do not match.

To Reproduce Just generate the hooks and check the prefetch code.

Here is an example of the code generated:

export const prefetchUseFooServiceGetBar = (queryClient: QueryClient) =>
  queryClient.prefetchQuery({
    queryKey: [Common.useUseFooServiceGetBarKey, []],
    queryFn: () => FooService.getBar(),
  });

OpenAPI spec file N/A

Expected behavior Similar to a query hook, the keys must be dynamic so we can tell React Query what key should be used for caching:

export const prefetchUseFooServiceGetBar = (queryClient: QueryClient, queryKey?: TQueryKey) =>
  queryClient.prefetchQuery({
    queryKey: Common.useUseFooServiceGetBarKeyFn(queryKey),
    queryFn: () => FooService.getBar(),
  });

Screenshots

  • OS: MacOS
  • Version: 14.4.1

Additional context N/A

nopitown avatar May 02 '24 20:05 nopitown

Please check out #97

https://github.com/7nohe/openapi-react-query-codegen/pull/97

I believe that should help you

seriouslag avatar May 02 '24 21:05 seriouslag

@seriouslag Got it!

But for prefetching to work correctly, it should use the same keys created in the query. Right now, the implementation is different, so the result is that it doesn't use the cache. In my case, I have a query: useQueryAuthServiceGetToken(), and this creates the key AuthServiceGetToken, but the prefetch hooks create the key AuthServiceGetToken, [] so since both are different React Query doesn't use the cached values; it assumes it is a different query.

I hope that makes more sense. Thanks for your quick response 🙏🏼

nopitown avatar May 02 '24 21:05 nopitown

@seriouslag Got it!

But for prefetching to work correctly, it should use the same keys created in the query. Right now, the implementation is different, so the result is that it doesn't use the cache. In my case, I have a query: useQueryAuthServiceGetToken(),, and this creates the key AuthServiceGetToken, but the prefetch hooks create the key AuthServiceGetToken, [] so since both are different React Query doesn't use the cached values; it assumes it is a different query.

I hope that makes more sense. Thanks for your quick response 🙏🏼

I see, so prefetch isn't using the new queryKey functions. This is a bug.

seriouslag avatar May 02 '24 21:05 seriouslag

@seriouslag Yes, I think that's the issue here 👍🏼

nopitown avatar May 02 '24 21:05 nopitown

If you wanna give a PR a go that would be awesome. I won't be able to look at this for at least a day.

seriouslag avatar May 02 '24 21:05 seriouslag

@seriouslag I just sent a draft PR but it will take until tomorrow to make sure it works as intended as I'm getting familiar with the codebase. Feel free to let me know if I'm heading in the wrong direction tho. Thanks!

nopitown avatar May 03 '24 00:05 nopitown