typegraphql-prisma-nestjs
Version:
This project is a fork of another with minor changes, created for personal use.
81 lines • 5.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateCrudResolverClassMethodDeclaration = generateCrudResolverClassMethodDeclaration;
const ts_morph_1 = require("ts-morph");
const types_1 = require("../dmmf/types");
function generateCrudResolverClassMethodDeclaration(action, mapping, dmmfDocument, generatorOptions) {
return {
name: action.name,
isAsync: true,
returnType: `Promise<${action.returnTSType}>`,
decorators: [
{
name: `${action.operation}`,
arguments: [
`_returns => ${action.typeGraphQLType}`,
ts_morph_1.Writers.object({
nullable: `${!action.method.isRequired}`,
}),
],
},
],
parameters: [
{
name: "ctx",
// TODO: import custom `ContextType`
type: "any",
decorators: [{ name: "Context", arguments: [] }],
},
{
name: "info",
type: "GraphQLResolveInfo",
decorators: [{ name: "Info", arguments: [] }],
},
...(!action.argsTypeName
? []
: [
{
name: "args",
type: action.argsTypeName,
decorators: [
{
name: "Args",
arguments: generatorOptions.emitRedundantTypesInfo
? [`_type => ${action.argsTypeName}`]
: [],
},
],
},
]),
],
statements: action.kind === types_1.DMMF.ModelAction.aggregate
? [
/* ts */ `const afterProcessEvents: ((result:any) => Promise<any>)[] = [];`,
/* ts */ `const transformedArgsIntoPrismaArgs = await transformArgsIntoPrismaArgs(info, args, ctx, '${mapping.modelName}', '${mapping.collectionName}', '${action.prismaMethod}', afterProcessEvents);`,
/* ts */ `const transformedInfoIntoPrismaArgs = transformInfoIntoPrismaArgs(info, '${mapping.modelName}', '${mapping.collectionName}', '${action.prismaMethod}');`,
/* ts */ `const result = await getPrismaFromContext(ctx).${mapping.collectionName}.${action.prismaMethod}({ ...transformedArgsIntoPrismaArgs, ...transformedInfoIntoPrismaArgs, });`,
/* ts */ `for (const afterProcessEvent of afterProcessEvents){ await afterProcessEvent(result); }`,
/* ts */ `return result;`,
]
: action.kind === types_1.DMMF.ModelAction.groupBy
? [
/* ts */ `const afterProcessEvents: ((result:any) => Promise<any>)[] = [];`,
/* ts */ `const { _count, _avg, _sum, _min, _max } = transformInfoIntoPrismaArgs(info, '${mapping.modelName}', '${mapping.collectionName}', '${action.prismaMethod}');`,
/* ts */ `const transformedArgsIntoPrismaArgs = await transformArgsIntoPrismaArgs(info, args, ctx, '${mapping.modelName}', '${mapping.collectionName}', '${action.prismaMethod}', afterProcessEvents);`,
/* ts */ `const groupByArgs = Object.fromEntries( Object.entries({ _count, _avg, _sum, _min, _max }).filter(([_, v]) => v != null));`,
/* ts */ `const result = await getPrismaFromContext(ctx).${mapping.collectionName}.${action.prismaMethod}({ ...transformedArgsIntoPrismaArgs, ...groupByArgs,});`,
/* ts */ `for (const afterProcessEvent of afterProcessEvents){ await afterProcessEvent(result); }`,
/* ts */ `return result;`,
]
: [
/* ts */ ` const afterProcessEvents: ((result:any) => Promise<any>)[] = [];`,
/* ts */ ` const { _count } = transformInfoIntoPrismaArgs(info, '${mapping.modelName}', '${mapping.collectionName}', '${action.prismaMethod}');`,
/* ts */ ` const transformedArgsIntoPrismaArgs = await transformArgsIntoPrismaArgs(info, args, ctx, '${mapping.modelName}', '${mapping.collectionName}', '${action.prismaMethod}', afterProcessEvents);`,
/* ts */ ` const otherArgs = _count && transformCountFieldIntoSelectRelationsCount(_count, '${mapping.modelName}', '${mapping.collectionName}', '${action.prismaMethod}');`,
/* ts */ ` const result = await getPrismaFromContext(ctx).${mapping.collectionName}.${action.prismaMethod}({ ...transformedArgsIntoPrismaArgs, ...otherArgs, });`,
/* ts */ ` for (const afterProcessEvent of afterProcessEvents){ await afterProcessEvent(result); }`,
/* ts */ `return result;`,
],
};
}
//# sourceMappingURL=helpers.js.map