Skip to content

Commit

Permalink
Fix generating _count field when no crudResolvers generated
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Oct 20, 2021
1 parent 30e4e16 commit 9ae4a0c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/generator/model-type-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down Expand Up @@ -113,7 +116,7 @@ export default function generateObjectTypeClassFromModel(
}),
};
}),
...(countField
...(shouldEmitCountField
? [
{
name: countField.name,
Expand Down
32 changes: 29 additions & 3 deletions tests/regression/__snapshots__/emit-only.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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\\";
Expand Down Expand Up @@ -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\\";
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/regression/emit-only.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9ae4a0c

Please sign in to comment.