From f3155828612b3e42ef966c0bf98574a3c0075cb3 Mon Sep 17 00:00:00 2001 From: EndyKaufman Date: Mon, 10 Jul 2023 23:20:57 +0400 Subject: [PATCH] feat: append transformArgsIntoPrismaArgs and setTransformArgsIntoPrismaArgs for modify args before send it to prisma --- src/generator/generate-helpers.ts | 10 ++++++++++ src/generator/imports.ts | 1 + src/generator/resolvers/helpers.ts | 6 +++--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/generator/generate-helpers.ts b/src/generator/generate-helpers.ts index 33aa97ef0..dc7b090e0 100644 --- a/src/generator/generate-helpers.ts +++ b/src/generator/generate-helpers.ts @@ -74,4 +74,14 @@ export function generateHelpersFile( } } `); + + sourceFile.addStatements(/* ts */ ` + export let transformArgsIntoPrismaArgs = function , TContext = any>(info: GraphQLResolveInfo, args: TArgs, ctx: TContext): TArgs { + return args; + }; + + export function setTransformArgsIntoPrismaArgs(newTransformArgsIntoPrismaArgs: typeof transformArgsIntoPrismaArgs) { + transformArgsIntoPrismaArgs = newTransformArgsIntoPrismaArgs; + } + `); } diff --git a/src/generator/imports.ts b/src/generator/imports.ts index b93666fd3..2f4d333ba 100644 --- a/src/generator/imports.ts +++ b/src/generator/imports.ts @@ -71,6 +71,7 @@ export function generateHelpersFileImport(sourceFile: SourceFile, level = 0) { (level === 0 ? "./" : "") + path.posix.join(...Array(level).fill(".."), "helpers"), namedImports: [ + "transformArgsIntoPrismaArgs", "transformInfoIntoPrismaArgs", "getPrismaFromContext", "transformCountFieldIntoSelectRelationsCount", diff --git a/src/generator/resolvers/helpers.ts b/src/generator/resolvers/helpers.ts index 360783e4c..f606d3c87 100644 --- a/src/generator/resolvers/helpers.ts +++ b/src/generator/resolvers/helpers.ts @@ -58,7 +58,7 @@ export function generateCrudResolverClassMethodDeclaration( action.kind === DMMF.ModelAction.aggregate ? [ /* ts */ ` return getPrismaFromContext(ctx).${mapping.collectionName}.${action.prismaMethod}({ - ...args, + ...transformArgsIntoPrismaArgs(info, args, ctx), ...transformInfoIntoPrismaArgs(info), });`, ] @@ -66,7 +66,7 @@ export function generateCrudResolverClassMethodDeclaration( ? [ /* ts */ ` const { _count, _avg, _sum, _min, _max } = transformInfoIntoPrismaArgs(info);`, /* ts */ ` return getPrismaFromContext(ctx).${mapping.collectionName}.${action.prismaMethod}({ - ...args, + ...transformArgsIntoPrismaArgs(info, args, ctx), ...Object.fromEntries( Object.entries({ _count, _avg, _sum, _min, _max }).filter(([_, v]) => v != null) ), @@ -75,7 +75,7 @@ export function generateCrudResolverClassMethodDeclaration( : [ /* ts */ ` const { _count } = transformInfoIntoPrismaArgs(info); return getPrismaFromContext(ctx).${mapping.collectionName}.${action.prismaMethod}({ - ...args, + ...transformArgsIntoPrismaArgs(info, args, ctx), ...(_count && transformCountFieldIntoSelectRelationsCount(_count)), });`, ],