Skip to content

Commit 9ab21b1

Browse files
committed
feat(package/cli): add convert-params-to-args generator
Added convert-params-to-args.ts to provide helper functions for converting parameter tuples into argument objects. This module includes functions for dynamically mapping parameters to their respective argument names, enhancing runtime parameter handling.
1 parent c8e7b24 commit 9ab21b1

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { type Schema } from 'gqty';
2+
3+
/**
4+
* Generates TypeScript code that defines:
5+
* 1) convertParamsToArgsFn<T>(argNames: string[], params: unknown[]): T
6+
* - Creates a normal object (not null-prototype)
7+
* - Omits undefined values
8+
*
9+
* 2) convertParamsToArgs = {
10+
* Mutation: { ... },
11+
* Query: { ... }
12+
* }
13+
* - Each method calls convertParamsToArgsFn with the appropriate
14+
* ParamNames and typed parameters.
15+
*
16+
* @param generatedSchema - The GQty generated schema (with query/mutation definitions).
17+
* @returns A string of TypeScript code to be appended to the final schema file.
18+
*/
19+
export function generateConvertParamsToArgs(generatedSchema: Schema): string {
20+
// Start with the function definition
21+
let code = `
22+
export function convertParamsToArgsFn<T>(argNames: string[], params: unknown[]): T {
23+
const result: Record<string, unknown> = {};
24+
25+
argNames.forEach((key, index) => {
26+
const value = params[index];
27+
// Only set the property if it's not undefined
28+
if (value !== undefined) {
29+
result[key] = value;
30+
}
31+
});
32+
33+
return result as T;
34+
}
35+
`;
36+
37+
// Build the convertParamsToArgs object
38+
let mutationMethods = '';
39+
if (generatedSchema.mutation) {
40+
for (const fieldName of Object.keys(generatedSchema.mutation)) {
41+
if (fieldName === '__typename') continue;
42+
const fieldValue = generatedSchema.mutation[fieldName];
43+
// Only generate a method if the field has arguments
44+
if (!fieldValue.__args || !Object.keys(fieldValue.__args).length)
45+
continue;
46+
47+
mutationMethods += `
48+
${fieldName}(params: MutationTypes["${fieldName}"]["params"]): Parameters<Mutation["${fieldName}"]>[0] {
49+
return convertParamsToArgsFn<Parameters<Mutation["${fieldName}"]>[0]>(
50+
MutationParamNames["${fieldName}"],
51+
params
52+
);
53+
},`;
54+
}
55+
}
56+
57+
let queryMethods = '';
58+
if (generatedSchema.query) {
59+
for (const fieldName of Object.keys(generatedSchema.query)) {
60+
if (fieldName === '__typename') continue;
61+
const fieldValue = generatedSchema.query[fieldName];
62+
if (!fieldValue.__args || !Object.keys(fieldValue.__args).length)
63+
continue;
64+
65+
queryMethods += `
66+
${fieldName}(params: QueryTypes["${fieldName}"]["params"]): Parameters<Query["${fieldName}"]>[0] {
67+
return convertParamsToArgsFn<Parameters<Query["${fieldName}"]>[0]>(
68+
QueryParamNames["${fieldName}"],
69+
params
70+
);
71+
},`;
72+
}
73+
}
74+
75+
code += `
76+
export const convertParamsToArgs = {
77+
Mutation: {${mutationMethods}
78+
},
79+
Query: {${queryMethods}
80+
}
81+
};
82+
`;
83+
84+
return code;
85+
}

packages/cli/src/generate/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { formatPrettier } from '../prettier';
2424

2525
import { generateMutationQueryTypes } from './mutation-query-types';
2626
import { generateMutationQueryParamNames } from './mutation-query-param-names';
27+
import { generateConvertParamsToArgs } from './convert-params-to-args';
2728

2829
const {
2930
isEnumType,
@@ -878,6 +879,8 @@ export async function generate(
878879
${generateMutationQueryTypes(generatedSchema, scalarsEnumsHash)}
879880
880881
${generateMutationQueryParamNames(generatedSchema)}
882+
883+
${generateConvertParamsToArgs(generatedSchema)}
881884
`);
882885

883886
const imports = [

0 commit comments

Comments
 (0)