Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/crisp-baths-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-react-query": minor
---

Fix useInfiniteQuery query key collision with useQuery
6 changes: 3 additions & 3 deletions packages/openapi-react-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export type UseInfiniteQueryMethod<Paths extends Record<string, Record<HttpMetho
Response["data"],
Response["error"],
InferSelectReturnType<InfiniteData<Response["data"]>, Options["select"]>,
QueryKey<Paths, Method, Path>,
readonly [...QueryKey<Paths, Method, Path>, "infinite"],
unknown
>,
"queryKey" | "queryFn"
Expand Down Expand Up @@ -231,8 +231,8 @@ export default function createClient<Paths extends {}, Media extends MediaType =
const { queryKey } = queryOptions(method, path, init);
return useInfiniteQuery(
{
queryKey,
queryFn: async ({ queryKey: [method, path, init], pageParam = 0, signal }) => {
queryKey: [...queryKey, "infinite"],
queryFn: async ({ pageParam = 0, signal }) => {
const mth = method.toUpperCase() as Uppercase<typeof method>;
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
const mergedInit = {
Expand Down
52 changes: 52 additions & 0 deletions packages/openapi-react-query/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1268,5 +1268,57 @@ describe("client", () => {

expect(result.current.data).toEqual([1, 2, 3, 4, 5, 6]);
});
it("should not clash with useQuery when using the same method, path, and params", async () => {
const fetchClient = createFetchClient<paths>({ baseUrl });
const client = createClient(fetchClient);

useMockRequestHandler({
baseUrl,
method: "get",
path: "/paginated-data",
status: 200,
body: { items: [1, 2, 3], nextPage: 1 },
});

const { result } = renderHook(
() => ({
query: client.useQuery("get", "/paginated-data", {
params: {
query: {
limit: 3,
},
},
}),
infiniteQuery: client.useInfiniteQuery(
"get",
"/paginated-data",
{
params: {
query: {
limit: 3,
},
},
},
{
getNextPageParam: (lastPage) => lastPage.nextPage,
initialPageParam: 0,
},
),
}),
{ wrapper },
);

await waitFor(() => {
expect(result.current.query.isSuccess).toBe(true);
expect(result.current.infiniteQuery.isSuccess).toBe(true);
});

// useQuery should return flat data
expect(result.current.query.data).toEqual({ items: [1, 2, 3], nextPage: 1 });

// useInfiniteQuery should return paginated data with pages array
expect(result.current.infiniteQuery.data?.pages).toHaveLength(1);
expect(result.current.infiniteQuery.data?.pages[0]).toEqual({ items: [1, 2, 3], nextPage: 1 });
});
});
});
Loading