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

useInfiniteQuery clashes with useQuery for the same query results in error #2184

Open
1 task
longzheng opened this issue Feb 28, 2025 · 0 comments
Open
1 task
Labels
bug Something isn't working openapi-react-query Relevant to openapi-react-query

Comments

@longzheng
Copy link

openapi-react-query version

0.3.0

Description

Using useInfiniteQuery concurrently with useQuery (on the same page) will lead to an TypeError: Cannot read properties of undefined (reading 'length') exception at

function getNextPageParam(options, { pages, pageParams }) {
  const lastIndex = pages.length - 1;

Upon debugging the reason for this is very simple actually, the queryKey generated for both queries will be the same because the method path init are shared between the two.

const { queryKey } = queryOptions(method, path, init);

However the data type between the two is different, for useInfiniteQuery the data is

interface InfiniteData<TData, TPageParam = unknown> {
    pages: Array<TData>;
    pageParams: Array<TPageParam>;
}

whereas for useQuery it's just TData.

So when the useInfiniteQuery runs, it'll reuse the same cache as the useQuery which results in getNextPageParams unable to access the data.pages.

I believe the solution is for the queryKey to be unique for the useInfiniteQuery function.

Reproduction

One query used in both useQuery and useInfiniteQuery will trigger exception.

    const {} = $api.useQuery(`get`, `/api/test`, {}, {});
    const {} = $api.useInfiniteQuery(`get`, `/api/test`, {}, {});

Expected result

I can use the same query in both useQuery and useInfiniteQuery successfully.

Extra

@longzheng longzheng added bug Something isn't working openapi-react-query Relevant to openapi-react-query labels Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-react-query Relevant to openapi-react-query
Projects
None yet
Development

No branches or pull requests

1 participant