query-key-factory icon indicating copy to clipboard operation
query-key-factory copied to clipboard

Extracting `queryFn` from query key store

Open linardsblk opened this issue 1 year ago • 0 comments

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;

linardsblk avatar Aug 21 '23 13:08 linardsblk