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

feat: Add useInfiniteQuery support #122

Merged
merged 7 commits into from
Aug 6, 2024
Merged

feat: Add useInfiniteQuery support #122

merged 7 commits into from
Aug 6, 2024

Conversation

7nohe
Copy link
Owner

@7nohe 7nohe commented May 26, 2024

#117
This new feature will generate a function in infiniteQueries.ts when the name specified by the pageParam option exists in the query parameters and the name specified by the nextPageParam option exists in the response.

Input:

  /paginated-pets:
    get:
      description: |
        Returns paginated pets from the system that the user has access to
      operationId: findPaginatedPets
      parameters:
        - name: page
          in: query
          description: page number
          required: false
          schema:
            type: integer
            format: int32
        - name: tags
          in: query
          description: tags to filter by
          required: false
          style: form
          schema:
            type: array
            items:
              type: string
        - name: limit
          in: query
          description: maximum number of results to return
          required: false
          schema:
            type: integer
            format: int32
      responses:
        '200':
          description: pet response
          content:
            application/json:
              schema:
                type: object
                properties:
                  pets:
                    type: array
                    items:
                      $ref: '#/components/schemas/Pet'
                  nextPage: 
                    type: integer
                    format: int32
                    minimum: 1

Output:

export const useDefaultServiceFindPaginatedPetsInfinite = <
  TData = InfiniteData<Common.DefaultServiceFindPaginatedPetsDefaultResponse>,
  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.UseDefaultServiceFindPaginatedPetsKeyFn(
      { limit, tags },
      queryKey
    ),
    queryFn: ({ pageParam }) =>
      DefaultService.findPaginatedPets({
        limit,
        page: pageParam as number,
        tags,
      }) as TData,
    initialPageParam: 1,
    getNextPageParam: (response) => (response as { nextPage: number }).nextPage,
    ...options,
  });

This can be used as follows:

export default function PaginatedPets() {
  const { data, fetchNextPage } = useDefaultServiceFindPaginatedPetsInfinite({
    limit: 10,
    tags: [],
  });

  return ...
}

@7nohe 7nohe self-assigned this May 26, 2024
Copy link

github-actions bot commented May 26, 2024

Coverage Report

Status Category Percentage Covered / Total
🟢 Lines 99.01% (🎯 95%) 2014 / 2034
🟢 Statements 99.01% (🎯 95%) 2014 / 2034
🟢 Functions 100% (🎯 95%) 41 / 41
🟢 Branches 94.84% (🎯 85%) 184 / 194
File Coverage
File Stmts % Branch % Funcs % Lines Uncovered Lines
Changed Files
src/constants.mts 100% 100% 100% 100%
src/createExports.mts 100% 100% 100% 100%
src/createSource.mts 100% 100% 100% 100%
src/createUseQuery.mts 99.41% 97.05% 100% 99.41% 267-270
src/generate.mts 100% 100% 100% 100%
Generated in workflow #308

@7nohe 7nohe requested a review from seriouslag June 9, 2024 14:08
@7nohe 7nohe marked this pull request as ready for review June 9, 2024 14:08
@7nohe
Copy link
Owner Author

7nohe commented Jun 9, 2024

npm publish

}) {
const methodName = getNameFromMethod(method);
const customHookName = hookNameFromMethod({ method, className });
const queryKey = createQueryKeyFromMethod({ method, className });

const isInfiniteQuery =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we throw an exception if 'queryString' is not set to 'useInfiniteQuery' and either pageParam or nextPageParam is undefined?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, throwing an exception would be safer, so I will add it.

Copy link
Collaborator

@seriouslag seriouslag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR looks great! I would love to see some updates to the readme.

An infinite scroll example using an Intersection observer would be awesome.
It doesn't have to be in the PR :)

@HJK181
Copy link

HJK181 commented Jul 1, 2024

What's the state of the PR?

@7nohe 7nohe merged commit e7626d9 into main Aug 6, 2024
4 checks passed
@7nohe 7nohe deleted the feat/use-infinite-query branch August 6, 2024 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants