Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Generator: Don't generate an update input for the single generator #2848

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/blue-cups-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/cms-api": patch
---

API Generator: Don't generate an update input for the single generator
5 changes: 1 addition & 4 deletions demo/api/src/footer/generated/dto/footer.input.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file has been generated by comet api-generator.
// You may choose to use this file as scaffold by moving this file out of generated folder and removing this comment.
import { BlockInputInterface, isBlockInputInterface } from "@comet/blocks-api";
import { PartialType, RootBlockInputScalar } from "@comet/cms-api";
import { RootBlockInputScalar } from "@comet/cms-api";
import { Field, InputType } from "@nestjs/graphql";
import { Transform } from "class-transformer";
import { IsNotEmpty, ValidateNested } from "class-validator";
Expand All @@ -16,6 +16,3 @@ export class FooterInput {
@ValidateNested()
content: BlockInputInterface;
}

@InputType()
export class FooterUpdateInput extends PartialType(FooterInput) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { BaseEntity, Entity, MikroORM, PrimaryKey } from "@mikro-orm/core";
import { defineConfig } from "@mikro-orm/postgresql";
import { LazyMetadataStorage } from "@nestjs/graphql/dist/schema-builder/storages/lazy-metadata.storage";

import { generateCrudInput } from "./generate-crud-input";
import { lintSource } from "./utils/test-helper";

@Entity()
class TestEntity extends BaseEntity<TestEntity, "id"> {
@PrimaryKey({ type: "uuid" })
id: string;
}

describe("GenerateCrudInput", () => {
it("shouldn't generate an update input", async () => {
LazyMetadataStorage.load();
const orm = await MikroORM.init(
defineConfig({
dbName: "test-db",
entities: [TestEntity],
}),
);

const out = await generateCrudInput({ targetDirectory: __dirname }, orm.em.getMetadata().get("TestEntity"), {
nested: false,
excludeFields: [],
generateUpdateInput: false,
});

const lintedOutput = await lintSource(out[0].content);

expect(lintedOutput).not.toContain("TestEntityUpdateInput");

orm.close();
});
});
8 changes: 6 additions & 2 deletions packages/api/cms-api/src/generator/generate-crud-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export async function generateCrudInput(
generatorOptions: { targetDirectory: string },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
metadata: EntityMetadata<any>,
options: { nested: boolean; fileName?: string; className?: string; excludeFields: string[] } = { nested: false, excludeFields: [] },
options: { nested: boolean; fileName?: string; className?: string; excludeFields: string[]; generateUpdateInput?: boolean } = {
nested: false,
excludeFields: [],
generateUpdateInput: true,
},
): Promise<GeneratedFile[]> {
const generatedFiles: GeneratedFile[] = [];

Expand Down Expand Up @@ -425,7 +429,7 @@ export class ${className} {
}

${
!options.nested
options.generateUpdateInput && !options.nested
? `
@InputType()
export class ${className.replace(/Input$/, "")}UpdateInput extends PartialType(${className}) {}
Expand Down
5 changes: 4 additions & 1 deletion packages/api/cms-api/src/generator/generate-crud-single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,8 @@ export async function generateCrudSingle(generatorOptions: CrudSingleGeneratorOp
return generatedFiles;
}

return [...(await generateCrudInput(generatorOptions, metadata)), ...(await generateCrudResolver())];
return [
...(await generateCrudInput(generatorOptions, metadata, { nested: false, excludeFields: [], generateUpdateInput: false })),
...(await generateCrudResolver()),
];
}