Skip to content

Commit 9e82374

Browse files
committed
fix(react-query): add query params to query key
1 parent a1f5c79 commit 9e82374

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

packages/client/src/codegen/generated-api-client.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe("Generated API Client", () => {
122122
it("can pass query params", async () => {
123123
const { queryFn, queryKey } = client.cats.useList({ color: "black" });
124124

125-
expect(queryKey).toEqual(["/api/cats"]);
125+
expect(queryKey).toEqual(["/api/cats?color=black"]);
126126
expect(queryFn).toBeTypeOf("function");
127127

128128
const cats = await queryFn();

packages/client/src/core/api-client.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ describe("API Client", () => {
174174
it("can pass query params", async () => {
175175
const { queryFn, queryKey } = client.cats.useList({ color: "black" });
176176

177-
expect(queryKey).toEqual(["/api/cats"]);
177+
expect(queryKey).toEqual(["/api/cats?color=black"]);
178178
expect(queryFn).toBeTypeOf("function");
179179

180180
const cats = await queryFn();

packages/client/src/core/api-client.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,23 @@ function createClientProxy(
173173

174174
if (config.extensions) {
175175
const [action, extensionMethod] = callPath.slice(-2);
176+
const method = inferHTTPMethod(action);
176177
const path = callPath.slice(0, -2);
177178
const bodyOrQuery = isQueryOrBody(pendingArgs[0])
178179
? pendingArgs[0]
179180
: undefined;
181+
const query = method === "GET" ? bodyOrQuery : undefined;
180182
const queryFn = (callTimeBody?: any) => {
181-
const method = inferHTTPMethod(action);
182183
const body = method === "GET" ? undefined : callTimeBody;
183-
const query = method === "GET" ? bodyOrQuery : undefined;
184184

185185
return makeRequest(config, action, path, body, query);
186186
};
187187
const queryKey = [
188-
makeUrl(path, { outputCase: config.urlCase, method: "GET" }),
188+
makeUrl(path, {
189+
outputCase: config.urlCase,
190+
method: "GET",
191+
query,
192+
}),
189193
];
190194
const handler = getExtensionHandler(
191195
config.extensions,
@@ -201,13 +205,18 @@ function createClientProxy(
201205

202206
if (isCallingHook(lastCall)) {
203207
const action = callPath.slice(-1)[0];
208+
const method = inferHTTPMethod(action);
204209
const path = callPath.slice(0, -1);
205210
const body = argumentsList[0];
206211

207212
return {
208213
queryFn: () => makeRequest(config, action, path, body),
209214
queryKey: [
210-
makeUrl(path, { outputCase: config.urlCase, method: "GET" }),
215+
makeUrl(path, {
216+
outputCase: config.urlCase,
217+
method: "GET",
218+
query: method === "GET" ? body : undefined,
219+
}),
211220
],
212221
};
213222
}

packages/client/src/extensions/extensions.generated.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe("react-query extension runtime", () => {
6161
client.cats.list({ color: "black" }).useQuery();
6262
expect(mockUseQuery).toBeCalledWith({
6363
queryFn: expect.any(Function),
64-
queryKey: ["/api/cats"],
64+
queryKey: ["/api/cats?color=black"],
6565
});
6666
expect(mockFetch).toBeCalledWith("/api/cats?color=black", {
6767
method: "GET",

0 commit comments

Comments
 (0)