Skip to content

useInfiniteQuery sends page param with value 0 on first page when initialPageParam is undefined #2715

@sbalay

Description

@sbalay

openapi-react-query version

0.5.4

Description

Bug

When calling useInfiniteQuery with initialPageParam: undefined, the first page request still includes the page parameter in the query string with value 0.

Root cause

In the queryFn implementation, pageParam defaults to 0:

queryFn: async ({ pageParam = 0, signal }) => {
  query: {
    [pageParamName]: pageParam, // sends e.g. after=0 on first page
  }
}

When initialPageParam is undefined, pageParam is undefined, which the destructuring default then replaces with 0. The page param is then always included in the query — even for the first page.

Expected behavior

When pageParam is undefined (i.e. the initial page), the page param should be omitted from the query entirely.

Suggested fix

queryFn: async ({ pageParam, signal }) => {
  query: {
    ...(pageParam !== undefined ? { [pageParamName]: pageParam } : {}),
  }
}

Impact

APIs that use cursor-based pagination receive an unexpected cursor=0 (or equivalent) on the first request, which they may not handle gracefully — since 0 is not a valid cursor value.

Reproduction

Craft an infinite query and set the initialPageParam as undefined. See that the request includes the page param as 0

Expected result

When pageParam is undefined (i.e. the initial page), the page param should be omitted from the query entirely.

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-react-queryRelevant to openapi-react-query

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions