useLazyQuery #1549
Replies: 6 comments 21 replies
-
You can use the |
Beta Was this translation helpful? Give feedback.
-
How can I pass params to refetch function when calling that |
Beta Was this translation helpful? Give feedback.
-
@AjaxSolutions now we are back one full circle to the enabled prop. If you really want a
usage would be:
|
Beta Was this translation helpful? Give feedback.
-
Hi ! If it can help someone, I managed to make an usable useLazyQuery (like Apollo), here is the code export type LazyQueryFunction<
TParams = unknown,
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey
> = (
params?: TParams,
options?: FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>
) => Promise<TData>;
export type UseLazyQueryResult<
TParams = unknown,
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey
> = [
LazyQueryFunction<TParams, TQueryFnData, TError, TData, TQueryKey>,
UseQueryResult<TData, TError>
];
export const useLazyQuery = <
TParams = unknown,
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey
>(
queryKey: TQueryKey,
queryFn: (params?: TParams) => Promise<TQueryFnData> | TQueryFnData,
options?: FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>
): UseLazyQueryResult<TParams, TQueryFnData, TError, TData, TQueryKey> => {
const query = useQuery<TQueryFnData, TError, TData, TQueryKey>(queryKey, {
enabled: false,
cacheTime: 0,
});
const clientQuery = useQueryClient();
const fetchQuery = useCallback<
LazyQueryFunction<TParams, TQueryFnData, TError, TData, TQueryKey>
>(
(params, fetchQueryOptions) =>
clientQuery.fetchQuery<TQueryFnData, TError, TData, TQueryKey>(
queryKey,
() => queryFn(params),
{
cacheTime: 0,
...options,
...fetchQueryOptions,
}
),
[clientQuery, options, queryFn, queryKey]
);
return [fetchQuery, query];
}; Here is an example of use: const [fetch, data] = useLazyQuery<
{ property: string },
{ response: string },
{ errorMessage: string }
>('key', (params) => api.get('things', { params }), { retry: 4 });
// example data
data.data?.response; // api response
data.error?.errorMessage; // error message
data.isLoading; // check if data is loading
// Example fetch
fetch({ property: 'a parameter' }).then((result) => result.response); If you find some things that aren't good, or something that can improve this hook, please let me know |
Beta Was this translation helpful? Give feedback.
-
Late to the party here, but for those still looking for a reasonable solution, I ended up doing this: export const useLazyQuery = <Vars, Data>(
args: {
getKey: (vars: Vars) => QueryKey;
fetcher: (vars: Vars) => () => Promise<Data>;
},
options?: Omit<
UseQueryOptions<Data, unknown, Data, QueryKey>,
"queryKey" | "queryFn"
>
) => {
const [variables, setVariables] = useState<Vars>();
const queryClient = useQueryClient();
const queryInfo = useQuery(
args.getKey(variables as any),
args.fetcher(variables as any),
{ enabled: Boolean(variables), ...options }
);
const fetch = async (
vars: Vars,
fetchOptions?: FetchQueryOptions<Data, unknown, Data, QueryKey>
) => {
setVariables(vars);
try {
const data = await queryClient.fetchQuery(
args.getKey(vars),
args.fetcher(vars),
fetchOptions ?? options
);
return { data, error: undefined };
} catch (error) {
return { data: undefined, error };
}
};
return [fetch, queryInfo] as const;
}; Not rigorously tested as of yet but seems to be doing the trick. Was designed to work with |
Beta Was this translation helpful? Give feedback.
-
I would love to have a Because of the generated code, I'd rather not create a userland implementation such as in #1549 (comment), though perhaps I could make a dynamic utility based on this. In my opinion, this should be implemented on the library side, however maybe it's best implemented in the GraphQL Code Generator adapter rather than RQ itself. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
How could I use React Query lazily?
I'm looking for a functionality similar to Apollo useLazyQuery
See also this useSWR feature request.
Beta Was this translation helpful? Give feedback.
All reactions