From ef997286f685a2565573b8609d76cd5af3417e6e Mon Sep 17 00:00:00 2001 From: Neil Gentleman Date: Wed, 17 May 2023 12:35:18 -0700 Subject: [PATCH] Support generating ESM output Setting `emitESM=true` in the generator's configuration will switch from using `require` to `import` calls in the generated code. I haven't been successful in actually using the ESM output. Bi-directional relationships between models lead to circular dependencies in `*WhereInput` and `*ListRelationFilter` and cause the following error at start-up: > ReferenceError: Cannot access 'FooWhereInput' before initialization > at file:///app/node_modules/@generated/type-graphql/resolvers/inputs/FooListRelationFilter.js:10:31 Regardless of ESM setting, create a package.json if generating into node_modules. This makes sure that the generated code doesn't inherit a parent package's `type`. --- package-lock.json | 2 +- src/cli/prisma-generator.ts | 1 + src/generator/args-class.ts | 2 + src/generator/generate-code.ts | 48 ++++++++- src/generator/generate-enhance.ts | 28 +++-- src/generator/imports.ts | 119 ++++++++++++++++----- src/generator/model-type-class.ts | 8 +- src/generator/options.ts | 1 + src/generator/resolvers/full-crud.ts | 17 ++- src/generator/resolvers/relations.ts | 5 +- src/generator/resolvers/separate-action.ts | 11 +- src/generator/type-class.ts | 10 +- 12 files changed, 201 insertions(+), 51 deletions(-) diff --git a/package-lock.json b/package-lock.json index b88d9d868..97beef757 100644 --- a/package-lock.json +++ b/package-lock.json @@ -59,7 +59,7 @@ "graphql-scalars": "^1.21.3", "prisma": "^4.14.0", "tslib": "^2.5.0", - "type-graphql": "^1.1.1 || 2.0.0-beta.1" + "type-graphql": "^1.1.1 || >=1.2.0-rc.1 || >=2.0.0-beta" } }, "node_modules/@ampproject/remapping": { diff --git a/src/cli/prisma-generator.ts b/src/cli/prisma-generator.ts index f552adca2..2b82ac401 100644 --- a/src/cli/prisma-generator.ts +++ b/src/cli/prisma-generator.ts @@ -67,6 +67,7 @@ export async function generate(options: GeneratorOptions) { "formatGeneratedCode", ["prettier", "tsc"] as const, ), + emitESM: parseStringBoolean(generatorConfig.emitESM), }; const internalConfig: InternalGeneratorOptions = { outputDirPath: outputDir, diff --git a/src/generator/args-class.ts b/src/generator/args-class.ts index 041d4ab6f..7626db046 100644 --- a/src/generator/args-class.ts +++ b/src/generator/args-class.ts @@ -34,6 +34,7 @@ export default function generateArgsTypeClassFromArgs( generateGraphQLScalarsImport(sourceFile); generateInputsImports( sourceFile, + dmmfDocument.options, fields .map(arg => arg.selectedInputType) .filter(argInputType => argInputType.location === "inputObjectTypes") @@ -42,6 +43,7 @@ export default function generateArgsTypeClassFromArgs( ); generateEnumsImports( sourceFile, + dmmfDocument.options, fields .map(field => field.selectedInputType) .filter(argType => argType.location === "enumTypes") diff --git a/src/generator/generate-code.ts b/src/generator/generate-code.ts index 509fc7d89..3dd736b20 100644 --- a/src/generator/generate-code.ts +++ b/src/generator/generate-code.ts @@ -89,6 +89,9 @@ export default async function generateCode( const project = new Project({ compilerOptions: { ...baseCompilerOptions, + ...(options.emitESM && { + module: ModuleKind.ES2020, + }), ...(emitTranspiledCode && { declaration: true, importHelpers: true, @@ -122,7 +125,11 @@ export default async function generateCode( undefined, { overwrite: true }, ); - generateEnumsBarrelFile(enumsBarrelExportSourceFile, emittedEnumNames); + generateEnumsBarrelFile( + enumsBarrelExportSourceFile, + dmmfDocument.options, + emittedEnumNames, + ); } if (dmmfDocument.shouldGenerateBlock("models")) { @@ -146,6 +153,7 @@ export default async function generateCode( ); generateModelsBarrelFile( modelsBarrelExportSourceFile, + dmmfDocument.options, dmmfDocument.datamodel.models.map(it => it.typeName), ); } @@ -186,6 +194,7 @@ export default async function generateCode( ); generateOutputsBarrelFile( outputsBarrelExportSourceFile, + dmmfDocument.options, outputTypesToGenerate.map(it => it.typeName), outputTypesFieldsArgsToGenerate.length > 0, ); @@ -215,6 +224,7 @@ export default async function generateCode( ); generateArgsBarrelFile( outputsArgsBarrelExportSourceFile, + dmmfDocument.options, outputTypesFieldsArgsToGenerate.map(it => it.argsTypeName!), ); } @@ -237,6 +247,7 @@ export default async function generateCode( ); generateInputsBarrelFile( inputsBarrelExportSourceFile, + dmmfDocument.options, dmmfDocument.schema.inputTypes.map(it => it.typeName), ); } @@ -267,6 +278,7 @@ export default async function generateCode( ); generateResolversBarrelFile( relationResolversBarrelExportSourceFile, + dmmfDocument.options, dmmfDocument.relationModels.map(relationModel => ({ resolverName: relationModel.resolverName, modelName: relationModel.model.typeName, @@ -302,7 +314,11 @@ export default async function generateCode( undefined, { overwrite: true }, ); - generateArgsBarrelFile(barrelExportSourceFile, argTypeNames); + generateArgsBarrelFile( + barrelExportSourceFile, + dmmfDocument.options, + argTypeNames, + ); } }); @@ -325,6 +341,7 @@ export default async function generateCode( ); generateArgsIndexFile( relationResolversArgsIndexSourceFile, + dmmfDocument.options, relationModelsWithArgs.map( relationModelData => relationModelData.model.typeName, ), @@ -342,6 +359,7 @@ export default async function generateCode( ); generateResolversIndexFile( relationResolversIndexSourceFile, + dmmfDocument.options, "relations", relationModelsWithArgs.length > 0, ); @@ -399,6 +417,7 @@ export default async function generateCode( ); generateResolversBarrelFile( crudResolversBarrelExportSourceFile, + dmmfDocument.options, generateMappingData, ); const crudResolversActionsBarrelExportSourceFile = project.createSourceFile( @@ -413,6 +432,7 @@ export default async function generateCode( ); generateResolversActionsBarrelFile( crudResolversActionsBarrelExportSourceFile, + dmmfDocument.options, generateMappingData, ); const crudResolversIndexSourceFile = project.createSourceFile( @@ -425,7 +445,12 @@ export default async function generateCode( undefined, { overwrite: true }, ); - generateResolversIndexFile(crudResolversIndexSourceFile, "crud", true); + generateResolversIndexFile( + crudResolversIndexSourceFile, + dmmfDocument.options, + "crud", + true, + ); log("Generating crud resolvers args..."); dmmfDocument.modelMappings.forEach(async mapping => { @@ -459,6 +484,7 @@ export default async function generateCode( ); generateArgsBarrelFile( barrelExportSourceFile, + dmmfDocument.options, actionsWithArgs.map(it => it.argsTypeName!), ); } @@ -475,6 +501,7 @@ export default async function generateCode( ); generateArgsIndexFile( crudResolversArgsIndexSourceFile, + dmmfDocument.options, dmmfDocument.modelMappings .filter(mapping => mapping.actions.some(it => it.argsTypeName !== undefined), @@ -523,11 +550,26 @@ export default async function generateCode( ); generateIndexFile( indexSourceFile, + dmmfDocument.options, dmmfDocument.relationModels.length > 0, dmmfDocument.options.blocksToEmit, ); log("Emitting generated code files"); + if (options.outputDirPath.includes("node_modules")) { + await project.getFileSystem().writeFile( + baseDirPath + "/package.json", + JSON.stringify( + { + type: dmmfDocument.options.emitESM ? "module" : "commonjs", + main: "./index.js", + types: "./index.d.ts", + }, + null, + 2, + ), + ); + } if (emitTranspiledCode) { await project.emit(); } else { diff --git a/src/generator/generate-enhance.ts b/src/generator/generate-enhance.ts index f339e84db..f9dfd15ea 100644 --- a/src/generator/generate-enhance.ts +++ b/src/generator/generate-enhance.ts @@ -42,11 +42,15 @@ export function generateEnhanceMap( if (dmmfDocument.shouldGenerateBlock("crudResolvers")) { sourceFile.addImportDeclaration({ - moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}/resolvers-crud.index`, + moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}/resolvers-crud.index${ + dmmfDocument.options.emitESM ? ".js" : "" + }`, namespaceImport: "crudResolvers", }); sourceFile.addImportDeclaration({ - moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}/args.index`, + moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}/args.index${ + dmmfDocument.options.emitESM ? ".js" : "" + }`, namespaceImport: "argsTypes", }); sourceFile.addVariableStatement({ @@ -67,7 +71,9 @@ export function generateEnhanceMap( trailingTrivia: "\r\n", }); sourceFile.addImportDeclaration({ - moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}/resolvers-actions.index`, + moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}/resolvers-actions.index${ + dmmfDocument.options.emitESM ? ".js" : "" + }`, namespaceImport: "actionResolvers", }); sourceFile.addVariableStatement({ @@ -237,7 +243,9 @@ export function generateEnhanceMap( if (hasRelations && dmmfDocument.shouldGenerateBlock("relationResolvers")) { sourceFile.addImportDeclaration({ - moduleSpecifier: `./${resolversFolderName}/${relationsResolversFolderName}/resolvers.index`, + moduleSpecifier: `./${resolversFolderName}/${relationsResolversFolderName}/resolvers.index${ + dmmfDocument.options.emitESM ? ".js" : "" + }`, namespaceImport: "relationResolvers", }); sourceFile.addVariableStatement({ @@ -367,7 +375,9 @@ export function generateEnhanceMap( if (dmmfDocument.shouldGenerateBlock("models")) { sourceFile.addImportDeclaration({ - moduleSpecifier: `./${modelsFolderName}`, + moduleSpecifier: `./${modelsFolderName}${ + dmmfDocument.options.emitESM ? "/index.js" : "" + }`, namespaceImport: "models", }); sourceFile.addVariableStatement({ @@ -435,7 +445,9 @@ export function generateEnhanceMap( if (dmmfDocument.shouldGenerateBlock("outputs")) { sourceFile.addImportDeclaration({ - moduleSpecifier: `./${resolversFolderName}/${outputsFolderName}`, + moduleSpecifier: `./${resolversFolderName}/${outputsFolderName}${ + dmmfDocument.options.emitESM ? "/index.js" : "" + }`, namespaceImport: "outputTypes", }); sourceFile.addVariableStatement({ @@ -497,7 +509,9 @@ export function generateEnhanceMap( if (dmmfDocument.shouldGenerateBlock("inputs")) { sourceFile.addImportDeclaration({ - moduleSpecifier: `./${resolversFolderName}/${inputsFolderName}`, + moduleSpecifier: `./${resolversFolderName}/${inputsFolderName}${ + dmmfDocument.options.emitESM ? "/index.js" : "" + }`, namespaceImport: "inputTypes", }); sourceFile.addVariableStatement({ diff --git a/src/generator/imports.ts b/src/generator/imports.ts index b93666fd3..50f37e06a 100644 --- a/src/generator/imports.ts +++ b/src/generator/imports.ts @@ -56,20 +56,34 @@ export function generateGraphQLScalarTypeImport(sourceFile: SourceFile) { }); } -export function generateCustomScalarsImport(sourceFile: SourceFile, level = 0) { +export function generateCustomScalarsImport( + sourceFile: SourceFile, + options: GeneratorOptions, + level = 0, +) { sourceFile.addImportDeclaration({ moduleSpecifier: (level === 0 ? "./" : "") + - path.posix.join(...Array(level).fill(".."), "scalars"), + path.posix.join( + ...Array(level).fill(".."), + `scalars${options.emitESM ? ".js" : ""}`, + ), namedImports: ["DecimalJSScalar"], }); } -export function generateHelpersFileImport(sourceFile: SourceFile, level = 0) { +export function generateHelpersFileImport( + sourceFile: SourceFile, + options: GeneratorOptions, + level = 0, +) { sourceFile.addImportDeclaration({ moduleSpecifier: (level === 0 ? "./" : "") + - path.posix.join(...Array(level).fill(".."), "helpers"), + path.posix.join( + ...Array(level).fill(".."), + `helpers${options.emitESM ? ".js" : ""}`, + ), namedImports: [ "transformInfoIntoPrismaArgs", "getPrismaFromContext", @@ -97,13 +111,14 @@ export function generatePrismaNamespaceImport( export function generateArgsBarrelFile( sourceFile: SourceFile, + options: GeneratorOptions, argsTypeNames: string[], ) { sourceFile.addExportDeclarations( argsTypeNames .sort() .map>(argTypeName => ({ - moduleSpecifier: `./${argTypeName}`, + moduleSpecifier: `./${argTypeName}${options.emitESM ? ".js" : ""}`, namedExports: [argTypeName], })), ); @@ -111,26 +126,30 @@ export function generateArgsBarrelFile( export function generateArgsIndexFile( sourceFile: SourceFile, + options: GeneratorOptions, typeNames: string[], ) { sourceFile.addExportDeclarations( typeNames .sort() .map>(typeName => ({ - moduleSpecifier: `./${typeName}/args`, + moduleSpecifier: `./${typeName}/args${ + options.emitESM ? "/index.js" : "" + }`, })), ); } export function generateModelsBarrelFile( sourceFile: SourceFile, + options: GeneratorOptions, modelNames: string[], ) { sourceFile.addExportDeclarations( modelNames .sort() .map>(modelName => ({ - moduleSpecifier: `./${modelName}`, + moduleSpecifier: `./${modelName}${options.emitESM ? ".js" : ""}`, namedExports: [modelName], })), ); @@ -138,13 +157,14 @@ export function generateModelsBarrelFile( export function generateEnumsBarrelFile( sourceFile: SourceFile, + options: GeneratorOptions, enumTypeNames: string[], ) { sourceFile.addExportDeclarations( enumTypeNames .sort() .map>(enumTypeName => ({ - moduleSpecifier: `./${enumTypeName}`, + moduleSpecifier: `./${enumTypeName}${options.emitESM ? ".js" : ""}`, namedExports: [enumTypeName], })), ); @@ -152,13 +172,14 @@ export function generateEnumsBarrelFile( export function generateInputsBarrelFile( sourceFile: SourceFile, + options: GeneratorOptions, inputTypeNames: string[], ) { sourceFile.addExportDeclarations( inputTypeNames .sort() .map>(inputTypeName => ({ - moduleSpecifier: `./${inputTypeName}`, + moduleSpecifier: `./${inputTypeName}${options.emitESM ? ".js" : ""}`, namedExports: [inputTypeName], })), ); @@ -166,6 +187,7 @@ export function generateInputsBarrelFile( export function generateOutputsBarrelFile( sourceFile: SourceFile, + options: GeneratorOptions, outputTypeNames: string[], hasSomeArgs: boolean, ) { @@ -173,7 +195,7 @@ export function generateOutputsBarrelFile( outputTypeNames .sort() .map>(outputTypeName => ({ - moduleSpecifier: `./${outputTypeName}`, + moduleSpecifier: `./${outputTypeName}${options.emitESM ? ".js" : ""}`, namedExports: [outputTypeName], })), ); @@ -184,25 +206,34 @@ export function generateOutputsBarrelFile( export function generateIndexFile( sourceFile: SourceFile, + options: GeneratorOptions, hasSomeRelations: boolean, blocksToEmit: EmitBlockKind[], ) { if (blocksToEmit.includes("enums")) { sourceFile.addExportDeclaration({ - moduleSpecifier: `./${enumsFolderName}`, + moduleSpecifier: `./${enumsFolderName}${ + options.emitESM ? "/index.js" : "" + }`, }); } if (blocksToEmit.includes("models")) { sourceFile.addExportDeclaration({ - moduleSpecifier: `./${modelsFolderName}`, + moduleSpecifier: `./${modelsFolderName}${ + options.emitESM ? "/index.js" : "" + }`, }); } if (blocksToEmit.includes("crudResolvers")) { sourceFile.addExportDeclaration({ - moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}`, + moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}${ + options.emitESM ? "/index.js" : "" + }`, }); sourceFile.addImportDeclaration({ - moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}/resolvers-crud.index`, + moduleSpecifier: `./${resolversFolderName}/${crudResolversFolderName}/resolvers-crud.index${ + options.emitESM ? ".js" : "" + }`, namespaceImport: "crudResolversImport", }); sourceFile.addVariableStatement({ @@ -212,16 +243,21 @@ export function generateIndexFile( { name: "crudResolvers", initializer: `Object.values(crudResolversImport) as unknown as NonEmptyArray`, + type: "NonEmptyArray", }, ], }); } if (hasSomeRelations && blocksToEmit.includes("relationResolvers")) { sourceFile.addExportDeclaration({ - moduleSpecifier: `./${resolversFolderName}/${relationsResolversFolderName}`, + moduleSpecifier: `./${resolversFolderName}/${relationsResolversFolderName}${ + options.emitESM ? "/index.js" : "" + }`, }); sourceFile.addImportDeclaration({ - moduleSpecifier: `./${resolversFolderName}/${relationsResolversFolderName}/resolvers.index`, + moduleSpecifier: `./${resolversFolderName}/${relationsResolversFolderName}/resolvers.index${ + options.emitESM ? ".js" : "" + }`, namespaceImport: "relationResolversImport", }); sourceFile.addVariableStatement({ @@ -231,24 +267,29 @@ export function generateIndexFile( { name: "relationResolvers", initializer: `Object.values(relationResolversImport) as unknown as NonEmptyArray`, + type: "NonEmptyArray", }, ], }); } if (blocksToEmit.includes("inputs")) { sourceFile.addExportDeclaration({ - moduleSpecifier: `./${resolversFolderName}/${inputsFolderName}`, + moduleSpecifier: `./${resolversFolderName}/${inputsFolderName}${ + options.emitESM ? "/index.js" : "" + }`, }); } if (blocksToEmit.includes("outputs")) { sourceFile.addExportDeclaration({ - moduleSpecifier: `./${resolversFolderName}/${outputsFolderName}`, + moduleSpecifier: `./${resolversFolderName}/${outputsFolderName}${ + options.emitESM ? "/index.js" : "" + }`, }); } sourceFile.addExportDeclarations([ - { moduleSpecifier: `./enhance` }, - { moduleSpecifier: `./scalars` }, + { moduleSpecifier: `./enhance${options.emitESM ? ".js" : ""}` }, + { moduleSpecifier: `./scalars${options.emitESM ? ".js" : ""}` }, ]); sourceFile.addImportDeclarations([ { @@ -282,6 +323,7 @@ export function generateIndexFile( export function generateResolversBarrelFile( sourceFile: SourceFile, + options: GeneratorOptions, resolversData: GenerateMappingData[], ) { resolversData @@ -290,13 +332,16 @@ export function generateResolversBarrelFile( ) .forEach(({ modelName, resolverName }) => { sourceFile.addExportDeclaration({ - moduleSpecifier: `./${modelName}/${resolverName}`, + moduleSpecifier: `./${modelName}/${resolverName}${ + options.emitESM ? ".js" : "" + }`, namedExports: [resolverName], }); }); } export function generateResolversActionsBarrelFile( sourceFile: SourceFile, + options: GeneratorOptions, resolversData: GenerateMappingData[], ) { resolversData @@ -307,7 +352,9 @@ export function generateResolversActionsBarrelFile( if (actionResolverNames) { actionResolverNames.forEach(actionResolverName => { sourceFile.addExportDeclaration({ - moduleSpecifier: `./${modelName}/${actionResolverName}`, + moduleSpecifier: `./${modelName}/${actionResolverName}${ + options.emitESM ? ".js" : "" + }`, namedExports: [actionResolverName], }); }); @@ -317,21 +364,32 @@ export function generateResolversActionsBarrelFile( export function generateResolversIndexFile( sourceFile: SourceFile, + options: GeneratorOptions, type: "crud" | "relations", hasSomeArgs: boolean, ) { if (type === "crud") { sourceFile.addExportDeclarations([ - { moduleSpecifier: `./resolvers-actions.index` }, - { moduleSpecifier: `./resolvers-crud.index` }, + { + moduleSpecifier: `./resolvers-actions.index${ + options.emitESM ? ".js" : "" + }`, + }, + { + moduleSpecifier: `./resolvers-crud.index${ + options.emitESM ? ".js" : "" + }`, + }, ]); } else { sourceFile.addExportDeclarations([ - { moduleSpecifier: `./resolvers.index` }, + { moduleSpecifier: `./resolvers.index${options.emitESM ? ".js" : ""}` }, ]); } if (hasSomeArgs) { - sourceFile.addExportDeclarations([{ moduleSpecifier: `./args.index` }]); + sourceFile.addExportDeclarations([ + { moduleSpecifier: `./args.index${options.emitESM ? ".js" : ""}` }, + ]); } } @@ -345,7 +403,12 @@ export const generateResolversOutputsImports = createImportGenerator( ); export const generateArgsImports = createImportGenerator(argsFolderName); function createImportGenerator(elementsDirName: string) { - return (sourceFile: SourceFile, elementsNames: string[], level = 1) => { + return ( + sourceFile: SourceFile, + options: GeneratorOptions, + elementsNames: string[], + level = 1, + ) => { const distinctElementsNames = [...new Set(elementsNames)].sort(); for (const elementName of distinctElementsNames) { sourceFile.addImportDeclaration({ @@ -354,7 +417,7 @@ function createImportGenerator(elementsDirName: string) { path.posix.join( ...Array(level).fill(".."), elementsDirName, - elementName, + `${elementName}${options.emitESM ? ".js" : ""}`, ), // TODO: refactor to default exports // defaultImport: elementName, diff --git a/src/generator/model-type-class.ts b/src/generator/model-type-class.ts index d6d99b033..0ac80cf64 100644 --- a/src/generator/model-type-class.ts +++ b/src/generator/model-type-class.ts @@ -37,9 +37,10 @@ export default function generateObjectTypeClassFromModel( generateTypeGraphQLImport(sourceFile); generateGraphQLScalarsImport(sourceFile); generatePrismaNamespaceImport(sourceFile, dmmfDocument.options, 1); - generateCustomScalarsImport(sourceFile, 1); + generateCustomScalarsImport(sourceFile, dmmfDocument.options, 1); generateModelsImports( sourceFile, + dmmfDocument.options, model.fields .filter(field => field.location === "outputObjectTypes") .filter(field => field.type !== model.name) @@ -51,6 +52,7 @@ export default function generateObjectTypeClassFromModel( ); generateEnumsImports( sourceFile, + dmmfDocument.options, model.fields .filter(field => field.location === "enumTypes") .map(field => field.type), @@ -61,7 +63,9 @@ export default function generateObjectTypeClassFromModel( countField !== undefined && dmmfDocument.shouldGenerateBlock("crudResolvers"); if (shouldEmitCountField) { - generateResolversOutputsImports(sourceFile, [countField.typeGraphQLType]); + generateResolversOutputsImports(sourceFile, dmmfDocument.options, [ + countField.typeGraphQLType, + ]); } sourceFile.addClass({ diff --git a/src/generator/options.ts b/src/generator/options.ts index 99b168295..a179b00c3 100644 --- a/src/generator/options.ts +++ b/src/generator/options.ts @@ -15,6 +15,7 @@ export interface ExternalGeneratorOptions { omitInputFieldsByDefault?: string[]; omitOutputFieldsByDefault?: string[]; formatGeneratedCode?: boolean | "prettier" | "tsc"; + emitESM?: boolean; } export interface InternalGeneratorOptions { diff --git a/src/generator/resolvers/full-crud.ts b/src/generator/resolvers/full-crud.ts index c5d265939..8467e876b 100644 --- a/src/generator/resolvers/full-crud.ts +++ b/src/generator/resolvers/full-crud.ts @@ -38,12 +38,13 @@ export default function generateCrudResolverClassFromMapping( generateGraphQLInfoImport(sourceFile); generateArgsImports( sourceFile, + dmmfDocument.options, mapping.actions .filter(it => it.argsTypeName !== undefined) .map(it => it.argsTypeName!), 0, ); - generateHelpersFileImport(sourceFile, 3); + generateHelpersFileImport(sourceFile, dmmfDocument.options, 3); const distinctOutputTypesNames = [ ...new Set(mapping.actions.map(it => it.outputTypeName)), @@ -54,8 +55,18 @@ export default function generateCrudResolverClassFromMapping( const otherOutputTypeNames = distinctOutputTypesNames.filter( typeName => !dmmfDocument.isModelTypeName(typeName), ); - generateModelsImports(sourceFile, modelOutputTypeNames, 3); - generateOutputsImports(sourceFile, otherOutputTypeNames, 2); + generateModelsImports( + sourceFile, + dmmfDocument.options, + modelOutputTypeNames, + 3, + ); + generateOutputsImports( + sourceFile, + dmmfDocument.options, + otherOutputTypeNames, + 2, + ); sourceFile.addClass({ name: mapping.resolverName, diff --git a/src/generator/resolvers/relations.ts b/src/generator/resolvers/relations.ts index 60f20392c..cbd24561c 100644 --- a/src/generator/resolvers/relations.ts +++ b/src/generator/resolvers/relations.ts @@ -57,6 +57,7 @@ export default function generateRelationsResolverClassesFromModel( generateGraphQLInfoImport(sourceFile); generateModelsImports( sourceFile, + dmmfDocument.options, [...relationFields.map(field => field.type), model.typeName], 3, ); @@ -64,8 +65,8 @@ export default function generateRelationsResolverClassesFromModel( const argTypeNames = relationFields .filter(it => it.argsTypeName !== undefined) .map(it => it.argsTypeName!); - generateArgsImports(sourceFile, argTypeNames, 0); - generateHelpersFileImport(sourceFile, 3); + generateArgsImports(sourceFile, dmmfDocument.options, argTypeNames, 0); + generateHelpersFileImport(sourceFile, dmmfDocument.options, 3); sourceFile.addClass({ name: resolverName, diff --git a/src/generator/resolvers/separate-action.ts b/src/generator/resolvers/separate-action.ts index ffc0b9a3e..1bd9578bd 100644 --- a/src/generator/resolvers/separate-action.ts +++ b/src/generator/resolvers/separate-action.ts @@ -39,10 +39,16 @@ export default function generateActionResolverClass( generateTypeGraphQLImport(sourceFile); generateGraphQLInfoImport(sourceFile); if (action.argsTypeName) { - generateArgsImports(sourceFile, [action.argsTypeName], 0); + generateArgsImports( + sourceFile, + dmmfDocument.options, + [action.argsTypeName], + 0, + ); } generateModelsImports( sourceFile, + dmmfDocument.options, [model.typeName, action.outputTypeName].filter(typeName => dmmfDocument.isModelTypeName(typeName), ), @@ -50,12 +56,13 @@ export default function generateActionResolverClass( ); generateOutputsImports( sourceFile, + dmmfDocument.options, [action.outputTypeName].filter( typeName => !dmmfDocument.isModelTypeName(typeName), ), 2, ); - generateHelpersFileImport(sourceFile, 3); + generateHelpersFileImport(sourceFile, dmmfDocument.options, 3); sourceFile.addClass({ name: action.actionResolverName, diff --git a/src/generator/type-class.ts b/src/generator/type-class.ts index 6e9af7ee1..e27c0cfb6 100644 --- a/src/generator/type-class.ts +++ b/src/generator/type-class.ts @@ -43,10 +43,11 @@ export function generateOutputTypeClassFromType( generateTypeGraphQLImport(sourceFile); generateGraphQLScalarsImport(sourceFile); generatePrismaNamespaceImport(sourceFile, dmmfDocument.options, 2); - generateCustomScalarsImport(sourceFile, 2); - generateArgsImports(sourceFile, fieldArgsTypeNames, 0); + generateCustomScalarsImport(sourceFile, dmmfDocument.options, 2); + generateArgsImports(sourceFile, dmmfDocument.options, fieldArgsTypeNames, 0); generateOutputsImports( sourceFile, + dmmfDocument.options, type.fields .filter(field => field.outputType.location === "outputObjectTypes") .map(field => field.outputType.type), @@ -54,6 +55,7 @@ export function generateOutputTypeClassFromType( ); generateEnumsImports( sourceFile, + dmmfDocument.options, type.fields .map(field => field.outputType) .filter(fieldType => fieldType.location === "enumTypes") @@ -161,9 +163,10 @@ export function generateInputTypeClassFromType( generateTypeGraphQLImport(sourceFile); generateGraphQLScalarsImport(sourceFile); generatePrismaNamespaceImport(sourceFile, options, 2); - generateCustomScalarsImport(sourceFile, 2); + generateCustomScalarsImport(sourceFile, options, 2); generateInputsImports( sourceFile, + options, inputType.fields .filter(field => field.selectedInputType.location === "inputObjectTypes") .map(field => field.selectedInputType.type) @@ -171,6 +174,7 @@ export function generateInputTypeClassFromType( ); generateEnumsImports( sourceFile, + options, inputType.fields .map(field => field.selectedInputType) .filter(fieldType => fieldType.location === "enumTypes")