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

Feature request: useQueryOptionsHook for react-query #1679

Open
bartlangelaan opened this issue Oct 30, 2024 · 0 comments
Open

Feature request: useQueryOptionsHook for react-query #1679

bartlangelaan opened this issue Oct 30, 2024 · 0 comments
Labels
tanstack-query TanStack Query related issue

Comments

@bartlangelaan
Copy link

bartlangelaan commented Oct 30, 2024

First of all, thanks for this amazing library!

I have a feature request for the react-query generator. I would like to use the generated query options with useQueries, while also using a hook mutator.

What happens?

A currently generated function is, for example:

export const useListPetsQueryOptions = <
TData = Awaited<ReturnType<ReturnType<typeof useListPetsHook>>>,
TError = Error,
>(
params?: ListPetsParams,
options?: {
query?: UseQueryOptions<
Awaited<ReturnType<ReturnType<typeof useListPetsHook>>>,
TError,
TData
>;
},
) => {
const { query: queryOptions } = options ?? {};
const queryKey = queryOptions?.queryKey ?? getListPetsQueryKey(params);
const listPets = useListPetsHook();
const queryFn: QueryFunction<
Awaited<ReturnType<ReturnType<typeof useListPetsHook>>>
> = ({ signal }) => listPets(params, signal);
return { queryKey, queryFn, ...queryOptions } as UseQueryOptions<
Awaited<ReturnType<ReturnType<typeof useListPetsHook>>>,
TError,
TData
> & { queryKey: QueryKey };
};

The useListPetsQueryOptions function accepts parameters, and returns the query options. But you cannot run hooks in a loop.

What were you expecting to happen?

I would expect that there is a way to get an option generator.

For example, this already exists for the query function itself. The useListPetsHook returns a function.

In the same style, I would also expect a useListPetsQueryOptionsHook or useListPetsQueryOptionsGenerator or useGetListPetsQueryOptions, that could be used like this:

const getListPetsQueryOptions = useListPetsQueryOptionsHook();

const queries = useQueries({
  queries: Array(5).keys().map(length => getListPetsQueryOptions({ length }))
});

For me, it would also be okay if the current use*QueryOptions is changed instead of introducing a new hook - but that would of course be a breaking change.

@melloware melloware added the tanstack-query TanStack Query related issue label Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tanstack-query TanStack Query related issue
Projects
None yet
Development

No branches or pull requests

2 participants