import { SourceFile } from "ts-morph"; import { GeneratorOptions } from "./options"; export function generateHelpersFile( sourceFile: SourceFile, options: GeneratorOptions, ) { sourceFile.addStatements(/* ts */ ` export function transformFields(fields: Record): Record { return Object.fromEntries( Object.entries(fields) // remove __typename and others .filter(([key, value]) => !key.startsWith("__")) .map<[string, any]>(([key, value]) => { if (Object.keys(value).length === 0) { return [key, true]; } return [key, transformFields(value)]; }), ); } `); sourceFile.addStatements(/* ts */ ` export function getPrismaFromContext(context: any) { const prismaClient = context["${options.contextPrismaKey}"]; if (!prismaClient) { throw new Error("Unable to find Prisma Client in GraphQL context. Please provide it under the \`context[\\"${options.contextPrismaKey}\\"]\` key."); } return prismaClient; } `); sourceFile.addStatements(/* ts */ ` export function transformCountFieldIntoSelectRelationsCount(_count: object) { return { include: { _count: { select: { ...Object.fromEntries( Object.entries(_count).filter(([_, v]) => v != null) ), } }, }, } } `); }