lukemorales / query-key-factory

A library for creating typesafe standardized query keys, useful for cache management in @tanstack/query
https://www.npmjs.com/package/@lukemorales/query-key-factory
MIT License
1.16k stars 32 forks source link

Extracting `queryFn` from query key store #77

Open linardsblk opened 1 year ago

linardsblk commented 1 year ago

Hi. I was trying to implement queryKeyStore that I could use in Nextjs in both SSR and client-side. In order to prefetch, I need to call the defined function but it requires a parameter of type QueryFunctionContext.

Expected 1 arguments, but got 0.ts(2554)
types.d.ts(9, 108): An argument for 'context' was not provided.
const queryFn: (context: QueryFunctionContext<readonly ["administrators", "list", string], any>) => AdministratorDto[] | Promise<AdministratorDto[]>

Is there a good way to extract queryFn? I want to define by API once in the queryKeyStore then use it in all of my application. Here is the example.

const AdministratorsPage = () => {
  const { data: administrators } = useQuery(
    queryKeyStore.administrators.list(),
  );

  return (
    //...
  );
};

export const getServerSideProps = async () => {
  const queryClient = new QueryClient()

  const { queryKey, queryFn } = queryKeyStore.administrators.list();
  await queryClient.prefetchQuery(
    queryKey,
    queryFn,
  );

  return {
    props: {
      dehydratedState: dehydrate(queryClient),
    },
  }

export default AdministratorsPage;