diff --git a/src/generator/model-type-class.ts b/src/generator/model-type-class.ts index b454bcea..51a14027 100644 --- a/src/generator/model-type-class.ts +++ b/src/generator/model-type-class.ts @@ -56,7 +56,10 @@ export default function generateObjectTypeClassFromModel( ); const countField = modelOutputType.fields.find(it => it.name === "_count"); - if (countField) { + const shouldEmitCountField = + countField !== undefined && + dmmfDocument.shouldGenerateBlock("crudResolvers"); + if (shouldEmitCountField) { generateResolversOutputsImports(sourceFile, [countField.typeGraphQLType]); } @@ -113,7 +116,7 @@ export default function generateObjectTypeClassFromModel( }), }; }), - ...(countField + ...(shouldEmitCountField ? [ { name: countField.name, diff --git a/tests/regression/__snapshots__/emit-only.ts.snap b/tests/regression/__snapshots__/emit-only.ts.snap index 52c08e84..df43832f 100644 --- a/tests/regression/__snapshots__/emit-only.ts.snap +++ b/tests/regression/__snapshots__/emit-only.ts.snap @@ -1600,7 +1600,33 @@ exports[`emitOnly generator option when only 'inputs' is set should generate pro " `; -exports[`emitOnly generator option when only 'models' is set should generate proper files, enhance and index: enhance 1`] = ` +exports[`emitOnly generator option when only 'models' is set should generate model without _count, proper files, enhance and index: User 1`] = ` +"import * as TypeGraphQL from \\"type-graphql\\"; +import * as GraphQLScalars from \\"graphql-scalars\\"; +import { Prisma } from \\"../../../helpers/prisma-client-mock\\"; +import { DecimalJSScalar } from \\"../scalars\\"; +import { Post } from \\"../models/Post\\"; + +@TypeGraphQL.ObjectType({ + isAbstract: true +}) +export class User { + @TypeGraphQL.Field(_type => TypeGraphQL.Int, { + nullable: false + }) + id!: number; + + @TypeGraphQL.Field(_type => String, { + nullable: false + }) + email!: string; + + posts?: Post[]; +} +" +`; + +exports[`emitOnly generator option when only 'models' is set should generate model without _count, proper files, enhance and index: enhance 1`] = ` "import { ClassType } from \\"type-graphql\\"; import * as models from \\"./models\\"; @@ -1691,7 +1717,7 @@ export function applyModelsEnhanceMap(modelsEnhanceMap: ModelsEnhanceMap) { " `; -exports[`emitOnly generator option when only 'models' is set should generate proper files, enhance and index: mainIndex 1`] = ` +exports[`emitOnly generator option when only 'models' is set should generate model without _count, proper files, enhance and index: mainIndex 1`] = ` "import { NonEmptyArray } from \\"type-graphql\\"; export * from \\"./enums\\"; @@ -1701,7 +1727,7 @@ export * from \\"./scalars\\"; " `; -exports[`emitOnly generator option when only 'models' is set should generate proper files, enhance and index: structure 1`] = ` +exports[`emitOnly generator option when only 'models' is set should generate model without _count, proper files, enhance and index: structure 1`] = ` " [type-graphql] enhance.ts diff --git a/tests/regression/emit-only.ts b/tests/regression/emit-only.ts index 8364f68d..57459b99 100644 --- a/tests/regression/emit-only.ts +++ b/tests/regression/emit-only.ts @@ -56,17 +56,19 @@ describe("emitOnly generator option", () => { }); describe("when only 'models' is set", () => { - it("should generate proper files, enhance and index", async () => { + it("should generate model without _count, proper files, enhance and index", async () => { await generateCodeFromSchema(schema, { emitOnly: ["models"], outputDirPath, }); + const userModelTSFile = await readGeneratedFile("/models/User.ts"); const enhanceTSFile = await readGeneratedFile("/enhance.ts"); const mainIndexTSFile = await readGeneratedFile("/index.ts"); const directoryStructureString = getDirectoryStructureString(outputDirPath); + expect(userModelTSFile).toMatchSnapshot("User"); expect(enhanceTSFile).toMatchSnapshot("enhance"); expect(mainIndexTSFile).toMatchSnapshot("mainIndex"); // FIXME: replace with `.toMatchInlineSnapshot()` when it starts working again