|
| 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 | +} |
0 commit comments