Skip to content

Issues with generating custom pagination infinite queries #139

Closed

Description

@hanahem

Describe the bug
The codegen tool works wonder. I've been just having a hard time generating working infinite queries.

Am I missing something on how I'm naming my pageParam and nextPageParam?

Expected behavior
From the OpenAPI spec I'd expect to have such an infinite query:

    useInfiniteQuery({
      queryKey: [useGetStuffKey],
      queryFn: ({ pageParam }) =>
        getStuff({ page: pageParam as number, size: PAGE_SIZE }),
      initialPageParam: 1,
      getNextPageParam: (lastPage) =>
        lastPage.items.length === 0 ? undefined : lastPage.pagination.nextPage,
    });

But I get this from the codegen:

export const useGetStuffInfinite = <
  TData = InfiniteData<Common.GetStuffDefaultResponse>,
  TError = unknown,
  TQueryKey extends Array<unknown> = unknown[],
>({ size }: { size: number; },
  queryKey?: TQueryKey,
  options?: Omit<UseInfiniteQueryOptions<TData, TError>, "queryKey" | "queryFn">
) =>
  useInfiniteQuery({
    queryKey: Common.UseGetStuffKeyFn({ size }, queryKey),
    queryFn: ({ pageParam }) =>
      getStuff({ page: pageParam as number, size }) as TData,
    initialPageParam: 1,
    getNextPageParam: (response) => (response as { nextPage: number }).nextPage,
    ...options,
  });

OpenAPI spec file
Here are bits of my openapi spec file, I'm trying to use the /stuff endpoint, I only kept what's necessary here.

/stuff:
    get:
      tags:
        - My Stuff
      operationId: get_stuff
      parameters:
        - name: page
          in: query
          description: The page number
          required: true
          schema:
            type: integer
            format: int32
            minimum: 0
        - name: size
          in: query
          description: The page size
          required: true
          schema:
            type: integer
            format: int32
            minimum: 0
      responses:
        '200':
          description: The Stuff I Need
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PaginatedStuff'
        '500':
          description: Internal server error
          content:
            text/plain:
              schema:
                type: string
                
                
# ....
 
 PaginatedStuff:
      type: object
      required:
        - items
        - pagination
      properties:
        items:
          type: array
          items:
            $ref: '#/components/schemas/Stuff'
        pagination:
          $ref: '#/components/schemas/PaginationInfo'
          
#....

PaginationInfo:
      type: object
      required:
        - size
        - nextPage
      properties:
        nextPage:
          type: integer
          format: int32
          minimum: 0
        size:
          type: integer
          format: int32
          minimum: 0
                

Additional info
If the issue is related to how the API and schema are constructed and isn't fixable through the codegen tool, is there a way to customize the generation of infinite queries to my custom-cut situation?

Activity

  1. 7nohe commented on Sep 16, 2024

    @7nohe
    Owner

    @hanahem

    It seems that the error occurs when the parameter specified in pageParam is set as required. As a temporary workaround, you can avoid the issue by setting required: false. I will also look into a fix.

        parameters:
            - name: page
              in: query
              description: The page number
              required: false # <- change here
              schema:
                type: integer
                format: int32
                minimum: 0
  2. self-assigned this
    on Sep 16, 2024
  3. Martijn-Schoenmaker-Webbio commented on May 21, 2025

    @Martijn-Schoenmaker-Webbio

    @7nohe Is this something you are fixing still? Making page optional is not within our reach.

  4. 7nohe commented on Aug 11, 2026

    @7nohe
    Owner

    Update after verifying this against the published v3.0.1.

    The reported bug is fixed. --nextPageParam now supports dot notation, so a nested cursor like yours (pagination.nextPage) generates the correct accessor:

    openapi-rq -i spec.yaml --pageParam page --nextPageParam pagination.nextPage
    

    emits

    getNextPageParam: (response: unknown) => (response as { pagination: { nextPage: number } }).pagination.nextPage,

    instead of reading a top-level nextPage. Documented under Usage.

    The second half of your request is not fully there yet. You also wanted custom logic (lastPage.items.length === 0 ? undefined : ...). v3 does expose an override slot for getNextPageParam / initialPageParam on the infinite hooks, and it works at runtime — but the callback parameter is mistyped, so your snippet does not compile without a cast. I filed #203 with the root cause.

    Keeping this open until #203 ships. Thanks for the detailed report — the nested-cursor case came directly from it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions