Skip to content

Commit 70cfd72

Browse files
committed
refactor(codegen): replace 'as any' with generic type parameters on parseFindManyArgs/parseFindFirstArgs
Instead of casting ORM args to 'any', the helpers are now generic: parseFindManyArgs<T>() / parseFindFirstArgs<T>() The codegen passes specific table types: parseFindManyArgs<FindManyArgs<CarSelect, CarFilter, CarCondition, CarsOrderBy>>(...) This gives proper type safety at the ORM call site without escape hatches. The internal 'as unknown as T' cast in the helpers is the single known bridge between the untyped CLI argv layer and the typed ORM interface.
1 parent 50162ac commit 70cfd72

3 files changed

Lines changed: 165 additions & 81 deletions

File tree

graphql/codegen/src/__tests__/codegen/__snapshots__/cli-generator.test.ts.snap

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,8 @@ import { CLIOptions, Inquirerer, extractFirst } from "inquirerer";
697697
import { getClient } from "../executor";
698698
import { coerceAnswers, parseFindFirstArgs, parseFindManyArgs, stripUndefined } from "../utils";
699699
import type { FieldSchema } from "../utils";
700-
import type { CreateCarInput, CarPatch } from "../../orm/input-types";
700+
import type { CreateCarInput, CarPatch, CarSelect, CarFilter, CarCondition, CarsOrderBy } from "../../orm/input-types";
701+
import type { FindManyArgs, FindFirstArgs } from "../../orm/select-types";
701702
const fieldSchema: FieldSchema = {
702703
id: "uuid",
703704
make: "string",
@@ -756,9 +757,9 @@ async function handleList(argv: Partial<Record<string, unknown>>, _prompter: Inq
756757
isElectric: true,
757758
createdAt: true
758759
};
759-
const findManyArgs = parseFindManyArgs(argv, defaultSelect);
760+
const findManyArgs = parseFindManyArgs<FindManyArgs<CarSelect, CarFilter, CarCondition, CarsOrderBy>>(argv, defaultSelect);
760761
const client = getClient();
761-
const result = await client.car.findMany(findManyArgs as any).execute();
762+
const result = await client.car.findMany(findManyArgs).execute();
762763
console.log(JSON.stringify(result, null, 2));
763764
} catch (error) {
764765
console.error("Failed to list records.");
@@ -778,9 +779,9 @@ async function handleFindFirst(argv: Partial<Record<string, unknown>>, _prompter
778779
isElectric: true,
779780
createdAt: true
780781
};
781-
const findFirstArgs = parseFindFirstArgs(argv, defaultSelect);
782+
const findFirstArgs = parseFindFirstArgs<FindFirstArgs<CarSelect, CarFilter, CarCondition>>(argv, defaultSelect);
782783
const client = getClient();
783-
const result = await client.car.findFirst(findFirstArgs as any).execute();
784+
const result = await client.car.findFirst(findFirstArgs).execute();
784785
console.log(JSON.stringify(result, null, 2));
785786
} catch (error) {
786787
console.error("Failed to find record.");
@@ -1160,7 +1161,8 @@ import { CLIOptions, Inquirerer, extractFirst } from "inquirerer";
11601161
import { getClient } from "../executor";
11611162
import { coerceAnswers, parseFindFirstArgs, parseFindManyArgs, stripUndefined } from "../utils";
11621163
import type { FieldSchema } from "../utils";
1163-
import type { CreateDriverInput, DriverPatch } from "../../orm/input-types";
1164+
import type { CreateDriverInput, DriverPatch, DriverSelect, DriverFilter, DriverCondition, DriversOrderBy } from "../../orm/input-types";
1165+
import type { FindManyArgs, FindFirstArgs } from "../../orm/select-types";
11641166
const fieldSchema: FieldSchema = {
11651167
id: "uuid",
11661168
name: "string",
@@ -1213,9 +1215,9 @@ async function handleList(argv: Partial<Record<string, unknown>>, _prompter: Inq
12131215
name: true,
12141216
licenseNumber: true
12151217
};
1216-
const findManyArgs = parseFindManyArgs(argv, defaultSelect);
1218+
const findManyArgs = parseFindManyArgs<FindManyArgs<DriverSelect, DriverFilter, DriverCondition, DriversOrderBy>>(argv, defaultSelect);
12171219
const client = getClient();
1218-
const result = await client.driver.findMany(findManyArgs as any).execute();
1220+
const result = await client.driver.findMany(findManyArgs).execute();
12191221
console.log(JSON.stringify(result, null, 2));
12201222
} catch (error) {
12211223
console.error("Failed to list records.");
@@ -1232,9 +1234,9 @@ async function handleFindFirst(argv: Partial<Record<string, unknown>>, _prompter
12321234
name: true,
12331235
licenseNumber: true
12341236
};
1235-
const findFirstArgs = parseFindFirstArgs(argv, defaultSelect);
1237+
const findFirstArgs = parseFindFirstArgs<FindFirstArgs<DriverSelect, DriverFilter, DriverCondition>>(argv, defaultSelect);
12361238
const client = getClient();
1237-
const result = await client.driver.findFirst(findFirstArgs as any).execute();
1239+
const result = await client.driver.findFirst(findFirstArgs).execute();
12381240
console.log(JSON.stringify(result, null, 2));
12391241
} catch (error) {
12401242
console.error("Failed to find record.");
@@ -3191,7 +3193,8 @@ import { CLIOptions, Inquirerer, extractFirst } from "inquirerer";
31913193
import { getClient } from "../../executor";
31923194
import { coerceAnswers, parseFindFirstArgs, parseFindManyArgs, stripUndefined } from "../../utils";
31933195
import type { FieldSchema } from "../../utils";
3194-
import type { CreateUserInput, UserPatch } from "../../../orm/input-types";
3196+
import type { CreateUserInput, UserPatch, UserSelect, UserFilter, UserCondition, UsersOrderBy } from "../../../orm/input-types";
3197+
import type { FindManyArgs, FindFirstArgs } from "../../../orm/select-types";
31953198
const fieldSchema: FieldSchema = {
31963199
id: "uuid",
31973200
email: "string",
@@ -3244,9 +3247,9 @@ async function handleList(argv: Partial<Record<string, unknown>>, _prompter: Inq
32443247
email: true,
32453248
name: true
32463249
};
3247-
const findManyArgs = parseFindManyArgs(argv, defaultSelect);
3250+
const findManyArgs = parseFindManyArgs<FindManyArgs<UserSelect, UserFilter, UserCondition, UsersOrderBy>>(argv, defaultSelect);
32483251
const client = getClient("auth");
3249-
const result = await client.user.findMany(findManyArgs as any).execute();
3252+
const result = await client.user.findMany(findManyArgs).execute();
32503253
console.log(JSON.stringify(result, null, 2));
32513254
} catch (error) {
32523255
console.error("Failed to list records.");
@@ -3263,9 +3266,9 @@ async function handleFindFirst(argv: Partial<Record<string, unknown>>, _prompter
32633266
email: true,
32643267
name: true
32653268
};
3266-
const findFirstArgs = parseFindFirstArgs(argv, defaultSelect);
3269+
const findFirstArgs = parseFindFirstArgs<FindFirstArgs<UserSelect, UserFilter, UserCondition>>(argv, defaultSelect);
32673270
const client = getClient("auth");
3268-
const result = await client.user.findFirst(findFirstArgs as any).execute();
3271+
const result = await client.user.findFirst(findFirstArgs).execute();
32693272
console.log(JSON.stringify(result, null, 2));
32703273
} catch (error) {
32713274
console.error("Failed to find record.");
@@ -3420,7 +3423,8 @@ import { CLIOptions, Inquirerer, extractFirst } from "inquirerer";
34203423
import { getClient } from "../../executor";
34213424
import { coerceAnswers, parseFindFirstArgs, parseFindManyArgs, stripUndefined } from "../../utils";
34223425
import type { FieldSchema } from "../../utils";
3423-
import type { CreateMemberInput, MemberPatch } from "../../../orm/input-types";
3426+
import type { CreateMemberInput, MemberPatch, MemberSelect, MemberFilter, MemberCondition, MembersOrderBy } from "../../../orm/input-types";
3427+
import type { FindManyArgs, FindFirstArgs } from "../../../orm/select-types";
34243428
const fieldSchema: FieldSchema = {
34253429
id: "uuid",
34263430
role: "string"
@@ -3471,9 +3475,9 @@ async function handleList(argv: Partial<Record<string, unknown>>, _prompter: Inq
34713475
id: true,
34723476
role: true
34733477
};
3474-
const findManyArgs = parseFindManyArgs(argv, defaultSelect);
3478+
const findManyArgs = parseFindManyArgs<FindManyArgs<MemberSelect, MemberFilter, MemberCondition, MembersOrderBy>>(argv, defaultSelect);
34753479
const client = getClient("members");
3476-
const result = await client.member.findMany(findManyArgs as any).execute();
3480+
const result = await client.member.findMany(findManyArgs).execute();
34773481
console.log(JSON.stringify(result, null, 2));
34783482
} catch (error) {
34793483
console.error("Failed to list records.");
@@ -3489,9 +3493,9 @@ async function handleFindFirst(argv: Partial<Record<string, unknown>>, _prompter
34893493
id: true,
34903494
role: true
34913495
};
3492-
const findFirstArgs = parseFindFirstArgs(argv, defaultSelect);
3496+
const findFirstArgs = parseFindFirstArgs<FindFirstArgs<MemberSelect, MemberFilter, MemberCondition>>(argv, defaultSelect);
34933497
const client = getClient("members");
3494-
const result = await client.member.findFirst(findFirstArgs as any).execute();
3498+
const result = await client.member.findFirst(findFirstArgs).execute();
34953499
console.log(JSON.stringify(result, null, 2));
34963500
} catch (error) {
34973501
console.error("Failed to find record.");
@@ -3631,7 +3635,8 @@ import { CLIOptions, Inquirerer, extractFirst } from "inquirerer";
36313635
import { getClient } from "../../executor";
36323636
import { coerceAnswers, parseFindFirstArgs, parseFindManyArgs, stripUndefined } from "../../utils";
36333637
import type { FieldSchema } from "../../utils";
3634-
import type { CreateCarInput, CarPatch } from "../../../orm/input-types";
3638+
import type { CreateCarInput, CarPatch, CarSelect, CarFilter, CarCondition, CarsOrderBy } from "../../../orm/input-types";
3639+
import type { FindManyArgs, FindFirstArgs } from "../../../orm/select-types";
36353640
const fieldSchema: FieldSchema = {
36363641
id: "uuid",
36373642
make: "string",
@@ -3690,9 +3695,9 @@ async function handleList(argv: Partial<Record<string, unknown>>, _prompter: Inq
36903695
isElectric: true,
36913696
createdAt: true
36923697
};
3693-
const findManyArgs = parseFindManyArgs(argv, defaultSelect);
3698+
const findManyArgs = parseFindManyArgs<FindManyArgs<CarSelect, CarFilter, CarCondition, CarsOrderBy>>(argv, defaultSelect);
36943699
const client = getClient("app");
3695-
const result = await client.car.findMany(findManyArgs as any).execute();
3700+
const result = await client.car.findMany(findManyArgs).execute();
36963701
console.log(JSON.stringify(result, null, 2));
36973702
} catch (error) {
36983703
console.error("Failed to list records.");
@@ -3712,9 +3717,9 @@ async function handleFindFirst(argv: Partial<Record<string, unknown>>, _prompter
37123717
isElectric: true,
37133718
createdAt: true
37143719
};
3715-
const findFirstArgs = parseFindFirstArgs(argv, defaultSelect);
3720+
const findFirstArgs = parseFindFirstArgs<FindFirstArgs<CarSelect, CarFilter, CarCondition>>(argv, defaultSelect);
37163721
const client = getClient("app");
3717-
const result = await client.car.findFirst(findFirstArgs as any).execute();
3722+
const result = await client.car.findFirst(findFirstArgs).execute();
37183723
console.log(JSON.stringify(result, null, 2));
37193724
} catch (error) {
37203725
console.error("Failed to find record.");

0 commit comments

Comments
 (0)