Skip to content

Commit 5928a3c

Browse files
committed
fix: passing signal to tanstack queryFn
chore: rm mutationKey/mutationOptions on query fns
1 parent fd82fe8 commit 5928a3c

File tree

4 files changed

+21
-87
lines changed

4 files changed

+21
-87
lines changed

packages/typed-openapi/src/tanstack-query.generator.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
7676
.map(
7777
(method) => `
7878
// <ApiClient.${method}>
79-
${method}<Path extends keyof ${capitalize(method)}Endpoints, TEndpoint extends ${capitalize(method)}Endpoints[Path]>(
79+
${method}<
80+
Path extends keyof ${capitalize(method)}Endpoints,
81+
TEndpoint extends ${capitalize(method)}Endpoints[Path]
82+
>(
8083
path: Path,
8184
...params: MaybeOptionalArg<TEndpoint["parameters"]>
8285
) {
@@ -91,28 +94,14 @@ export const generateTanstackQueryFile = async (ctx: GeneratorContext & { relati
9194
const requestParams = {
9295
...(params[0] || {}),
9396
...(queryKey[0] || {}),
94-
signal,
97+
overrides: { signal },
9598
withResponse: false as const
9699
};
97100
const res = await this.client.${method}(path, requestParams as never);
98101
return res as Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"];
99102
},
100103
queryKey: queryKey
101104
}),
102-
mutationFn: {} as "You need to pass .mutationOptions to the useMutation hook",
103-
mutationOptions: {
104-
mutationKey: queryKey,
105-
mutationFn: async (localOptions: TEndpoint extends { parameters: infer Parameters} ? Parameters: never) => {
106-
const requestParams = {
107-
...(params[0] || {}),
108-
...(queryKey[0] || {}),
109-
...(localOptions || {}),
110-
withResponse: false as const
111-
};
112-
const res = await this.client.${method}(path, requestParams as never);
113-
return res as Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"];
114-
}
115-
}
116105
};
117106
118107
return query

packages/typed-openapi/tests/integration-runtime-msw.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Integration test for generated query client using MSW
22
// This test ensures the generated client (TS types only, no schema validation) has no runtime errors
33

4-
import { mutationOptions, queryOptions } from "@tanstack/react-query";
4+
import { mutationOptions, QueryClient, queryOptions } from "@tanstack/react-query";
55
import { http, HttpResponse } from "msw";
66
import { setupServer } from "msw/node";
77
import { afterAll, beforeAll, describe, expect, it } from "vitest";
@@ -484,7 +484,7 @@ describe("Example API Client", () => {
484484
expect(result.data).toEqual({ code: 404, message: expect.any(String) });
485485
});
486486

487-
it("should allow aborting requests via AbortController signal (tanstack)", async () => {
487+
it("should allow aborting mutation requests via AbortController signal (tanstack)", async () => {
488488
const mutation = tanstack.mutation("get", "/pet/{petId}");
489489
const controller = new AbortController();
490490

@@ -496,7 +496,16 @@ describe("Example API Client", () => {
496496
// abort immediately
497497
controller.abort();
498498

499-
await expect(promise).rejects.toHaveProperty("name", "AbortError");
499+
await expect(promise).rejects.toThrowErrorMatchingInlineSnapshot(`[AbortError: This operation was aborted]`)
500+
});
501+
502+
it("should allow aborting query requests via AbortController signal (tanstack)", async () => {
503+
const client = new QueryClient()
504+
const queryOptions =tanstack.get("/pet/{petId}",{path: {petId: 111}}).queryOptions
505+
const promise = client.fetchQuery(queryOptions)
506+
await client.cancelQueries(queryOptions)
507+
508+
await expect(promise).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: CancelledError]`)
500509
});
501510

502511
it("should throw when throwOnStatusError is true (tanstack)", async () => {

packages/typed-openapi/tests/integration.types.tstyche.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,6 @@ describe("Example API Client", () => {
309309
const tanstack = {} as TanstackQueryApiClient;
310310

311311
const query = tanstack.get("/pet/{petId}", { path: { petId: 42 } });
312-
const input = {} as Parameters<typeof query.mutationOptions.mutationFn>[0];
313-
expect(input).type.toBeAssignableTo<Endpoints.get_GetPetById["parameters"]>();
314-
expect(input).type.toBeAssignableTo<{
315-
path: {
316-
petId: number;
317-
};
318-
}>();
319-
320312
const output = await query.queryOptions.queryFn?.({} as any);
321313
expect(output).type.toBe<Schemas.Pet | undefined>();
322314
});

packages/typed-openapi/tests/tanstack-query.generator.test.ts

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,14 @@ describe("generator", () => {
9797
const requestParams = {
9898
...(params[0] || {}),
9999
...(queryKey[0] || {}),
100-
signal,
100+
overrides: { signal },
101101
withResponse: false as const,
102102
};
103103
const res = await this.client.put(path, requestParams as never);
104104
return res as Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"];
105105
},
106106
queryKey: queryKey,
107107
}),
108-
mutationFn: {} as "You need to pass .mutationOptions to the useMutation hook",
109-
mutationOptions: {
110-
mutationKey: queryKey,
111-
mutationFn: async (localOptions: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) => {
112-
const requestParams = {
113-
...(params[0] || {}),
114-
...(queryKey[0] || {}),
115-
...(localOptions || {}),
116-
withResponse: false as const,
117-
};
118-
const res = await this.client.put(path, requestParams as never);
119-
return res as Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"];
120-
},
121-
},
122108
};
123109
124110
return query;
@@ -141,28 +127,14 @@ describe("generator", () => {
141127
const requestParams = {
142128
...(params[0] || {}),
143129
...(queryKey[0] || {}),
144-
signal,
130+
overrides: { signal },
145131
withResponse: false as const,
146132
};
147133
const res = await this.client.post(path, requestParams as never);
148134
return res as Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"];
149135
},
150136
queryKey: queryKey,
151137
}),
152-
mutationFn: {} as "You need to pass .mutationOptions to the useMutation hook",
153-
mutationOptions: {
154-
mutationKey: queryKey,
155-
mutationFn: async (localOptions: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) => {
156-
const requestParams = {
157-
...(params[0] || {}),
158-
...(queryKey[0] || {}),
159-
...(localOptions || {}),
160-
withResponse: false as const,
161-
};
162-
const res = await this.client.post(path, requestParams as never);
163-
return res as Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"];
164-
},
165-
},
166138
};
167139
168140
return query;
@@ -185,28 +157,14 @@ describe("generator", () => {
185157
const requestParams = {
186158
...(params[0] || {}),
187159
...(queryKey[0] || {}),
188-
signal,
160+
overrides: { signal },
189161
withResponse: false as const,
190162
};
191163
const res = await this.client.get(path, requestParams as never);
192164
return res as Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"];
193165
},
194166
queryKey: queryKey,
195167
}),
196-
mutationFn: {} as "You need to pass .mutationOptions to the useMutation hook",
197-
mutationOptions: {
198-
mutationKey: queryKey,
199-
mutationFn: async (localOptions: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) => {
200-
const requestParams = {
201-
...(params[0] || {}),
202-
...(queryKey[0] || {}),
203-
...(localOptions || {}),
204-
withResponse: false as const,
205-
};
206-
const res = await this.client.get(path, requestParams as never);
207-
return res as Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"];
208-
},
209-
},
210168
};
211169
212170
return query;
@@ -229,28 +187,14 @@ describe("generator", () => {
229187
const requestParams = {
230188
...(params[0] || {}),
231189
...(queryKey[0] || {}),
232-
signal,
190+
overrides: { signal },
233191
withResponse: false as const,
234192
};
235193
const res = await this.client.delete(path, requestParams as never);
236194
return res as Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"];
237195
},
238196
queryKey: queryKey,
239197
}),
240-
mutationFn: {} as "You need to pass .mutationOptions to the useMutation hook",
241-
mutationOptions: {
242-
mutationKey: queryKey,
243-
mutationFn: async (localOptions: TEndpoint extends { parameters: infer Parameters } ? Parameters : never) => {
244-
const requestParams = {
245-
...(params[0] || {}),
246-
...(queryKey[0] || {}),
247-
...(localOptions || {}),
248-
withResponse: false as const,
249-
};
250-
const res = await this.client.delete(path, requestParams as never);
251-
return res as Extract<InferResponseByStatus<TEndpoint, SuccessStatusCode>, { data: {} }>["data"];
252-
},
253-
},
254198
};
255199
256200
return query;

0 commit comments

Comments
 (0)