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

code-gen(react-query): add .fetch, .invalidate, .prefetch etc on generated hooks #1879

Closed
dirkdev98 opened this issue Jul 1, 2022 · 0 comments · Fixed by #1901
Closed

code-gen(react-query): add .fetch, .invalidate, .prefetch etc on generated hooks #1879

dirkdev98 opened this issue Jul 1, 2022 · 0 comments · Fixed by #1901
Labels
prio 2 Issues with a lower prio, mostly features

Comments

@dirkdev98
Copy link
Member

dirkdev98 commented Jul 1, 2022

Working with the generated react-query hooks and api functions outside of the render tree is currently relatively verbose;

queryClient.fetchQuery(useUserSettings.queryKey(), () => apiUserSettings(axiosInstance, ...params))

this can be shortened most of the time in something like;

usUserSettings.fetch(queryClient, axiosInstance, ...params);

This improves the following cases;

  • Without Typescript, the end user can call / import the wrong apiXxYY function
  • Readability when many api calls are fetched, prefetched etc. Currently you have to scan to the use of useUserSettings.queryKey() to find the api call that is prefetched.
  • A few less imports
  • An easier abstraction to work with, without sacrificing customisability when needed.

We can generate a few helpers to do this for the user;

useUserSettings.fetch = (queryClient, axiosInstance, { params, query, body, }) =>  queryClient.fetchQuery(...);
useUserSettings.prefetch = (queryClient, axiosInstance, { params, query, body }) => queryClient.prefetchQuery(...);
useUserSettings.invalidate = (queryClient, { params, query, body }) => queryClient.invalidateQueries(...);
useUserSettings.setQueryData = (queryClient, { params, query, body }, data) => queryClient.setQueryData(...);

Usage in Next.js SSR would look something like;

export function getServerSideProps(context) {
  const queryClient = new QueryClient();
  const axiosInstance = createSSRAxiosInstanceSomehow(context);

  await Promise.all([
    useAuthMe.prefetch(queryClient, axiosInstance),
    useEntityList.prefetch(queryClient, axiosInstance, {
      body: { ... },
    }),
  ]);

  return {
    props: {
      dehydratedState: dehydrate(queryClient),
    },
  };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
prio 2 Issues with a lower prio, mostly features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant