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

Add support for generating useInfiniteQuery custom hooks #117

Open
7nohe opened this issue May 18, 2024 · 2 comments
Open

Add support for generating useInfiniteQuery custom hooks #117

7nohe opened this issue May 18, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@7nohe
Copy link
Owner

7nohe commented May 18, 2024

Is your feature request related to a problem? Please describe.
Currently, this library does not support generating custom hooks for useInfiniteQuery. This limitation makes it difficult to handle APIs that require pagination or infinite scrolling, leading to additional manual coding and reduced efficiency.

Describe the solution you'd like
I would like the library to support the generation of useInfiniteQuery custom hooks directly from OpenAPI schemas. This feature should automatically create hooks that handle pagination parameters and fetch data incrementally as needed, making it easier for developers to integrate infinite scrolling functionality into their applications. Specifically, the solution would involve adding CLI options --next-page-param and --page-param to specify the relevant parameters. If the API has query parameters and responses that match these options, the library should automatically generate useInfiniteQuery custom hooks.

Here is an example of a generated custom hook:

export const useInfiniteDefaultServiceFindPets = <
  TData = Common.DefaultServiceFindPetsDefaultResponse,
  TError = unknown,
  TQueryKey extends Array<unknown> = unknown[]
>(
  {
    limit,
    tags,
  }: {
    limit?: number;
    tags?: string[];
  } = {},
  queryKey?: TQueryKey,
  options?: Omit<UseInfiniteQueryOptions<TData, TError>, "queryKey" | "queryFn">
) =>
  useInfiniteQuery({
    queryKey: Common.UseDefaultServiceFindPetsKeyFn({ limit, tags }, queryKey),
    queryFn: (({ pageParam }) => DefaultService.findPets({ limit, tags, page: pageParam as number })) as QueryFunction<TData, QueryKey, unknown>,
    initialPageParam: 1,
    getNextPageParam: (response) => (response as { nextPage: number }).nextPage,
    ...options,
  });

Additional context
Here is the documentation for useInfiniteQuery for reference.

@7nohe 7nohe added the enhancement New feature or request label May 18, 2024
@7nohe 7nohe self-assigned this May 26, 2024
@MalguyMQ
Copy link

Great idea! I can help if you need to support this feature!

@7nohe
Copy link
Owner Author

7nohe commented Jun 9, 2024

@MalguyMQ This feature has been implemented in #122 and pre-released. You can try it with the following command.

npx @7nohe/openapi-react-query-codegen@0.0.0-1712acfb083e0b1eec43cfc6f917f780ae5b5815 -i ./your-openapi-file.yaml

If you have any feedback or ideas for improvement, please leave a comment!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants