You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Typically we use react-query in the following way, by writing a small reusable hook, ex:
// Example of request varstypeEventRequestVariables{
count: number;
filter: Record<string,string>,skip: number;}// in queries.tsexportconstEventsQueries=createQueryKeys('events',{// bunch of other events related queriesevents: (variables: EventRequestVariables)=>({queryKey: [variables],queryFn: ()=>api.events(variables),}),})// in useEvents.tsexportconstuseEvents=(variables?: QueryVariables<typeofEventsQueries.events>,options?: CommonQueryOptions<typeofEventsQueries.events>,)=>{const{ locale }=useLocale();const{ data, status, isLoading, isError, refetch }=useQuery({
...EventsQueries.events({ locale, ...variables}),staleTime: StaleTime.FOREVER,
...options,});return{data: data?.allPageEvents||[],
status,
isLoading,
isError,
refetch,};};
This allows us to easily expose what we need from useQuery while allowing to reuse useEvents across our application. I've used 2 helper types to extract the variables and query options from the QueryKey factory instance.
They could use some love but I think it might be a great addition to this library, also potentially solving issue #40 without making the typings less strict.
// We need to use any here for generics// eslint-disable-next-line @typescript-eslint/no-explicit-anytypeGenericFunction<T=any>=(...args: any[])=>T;// Extracts the query functiontypeExtractQueryFn<T>=TextendsGenericFunction<{queryFn: infer QueryFn;}>
? QueryFnextendsGenericFunction
? QueryFn
: never
: never;// Extracts the queryData aka the returned DatatypeExtractQueryData<T>=TextendsGenericFunction<{queryFn: infer QueryFn;}>
? QueryFnextendsGenericFunction
? Awaited<ReturnType<QueryFn>>
: never
: never;// Extract the query keytypeExtractQueryKey<T>=TextendsGenericFunction<{queryKey: infer TQueryKey;}>
? TQueryKeyextendsQueryKey
? TQueryKey
: never
: never;// You can use this to allow passing in custom variablesexporttypeQueryVariables<TextendsGenericFunction,>=Parameters<T>[0];// You can use this to pass correct Query options, allowing to pass a custom errorexporttypeCommonQueryOptions<TextendsGenericFunction,TError=Error,TQueryData=ExtractQueryData<T>,TQueryKeyextendsQueryKey=ExtractQueryKey<T>,>=UseQueryOptions<TQueryData,TError,TQueryData,TQueryKey>;
Right now it always assumes the Keys are a function, I couldn't really manage to wrap my head around the different options like passing null or returning a string
WDYT?
The text was updated successfully, but these errors were encountered:
@JanStevens did you end up using this in any of your work? I'm currently having issues with query function return values if they return a promise. Looks like TS doesn't like something about GenericFunction<any>.
Hi,
Typically we use react-query in the following way, by writing a small reusable hook, ex:
This allows us to easily expose what we need from useQuery while allowing to reuse
useEvents
across our application. I've used 2 helper types to extract thevariables
and queryoptions
from the QueryKey factory instance.They could use some love but I think it might be a great addition to this library, also potentially solving issue #40 without making the typings less strict.
Right now it always assumes the Keys are a function, I couldn't really manage to wrap my head around the different options like passing
null
or returning a stringWDYT?
The text was updated successfully, but these errors were encountered: