-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API Generator: Fix generated API for many-to-many-relations with cust…
…om relation entity (#1967) This fixes APIs generated by API Generator for many-to-many-relations that are not made using `@ManyToMany` but rather have a custom relation entity (to allow storage of additional data in the relation). See individual commits: - Correctly call `loadItems()` onto collection before set so existing entries will be overwritten - Fix input handling for n:m relations that can be async - Fix missing injected repository for n:m relation input. Move the building of the repositories to inject into `generateInputHandling` that will recurse - Create unique nested input DTO for n:m relation table. Needed if both sides have the API generated in the same target folder, as each side has different relation arguments - Demo API: Extend products by a product-to-tag-relation (example for this kind of relation) --------- Co-authored-by: Tobias Kuhn <tobias.kuhn@vivid-planet.com> Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
- Loading branch information
1 parent
74d1c9d
commit 94ac6b7
Showing
20 changed files
with
448 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@comet/cms-api": minor | ||
--- | ||
|
||
API Generator: Fix generated API for many-to-many-relations with custom relation entity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Migration } from '@mikro-orm/migrations'; | ||
|
||
export class Migration20240419135211 extends Migration { | ||
|
||
async up(): Promise<void> { | ||
this.addSql('create table "ProductToTag" ("id" uuid not null, "product" uuid not null, "tag" uuid not null, "exampleStatus" boolean not null default true, constraint "ProductToTags_pkey" primary key ("id"));'); | ||
|
||
this.addSql('alter table "ProductToTags" add constraint "ProductToTag_product_foreign" foreign key ("product") references "Product" ("id") on update cascade on delete cascade;'); | ||
this.addSql('alter table "ProductToTags" add constraint "ProductToTag_tag_foreign" foreign key ("tag") references "ProductTag" ("id") on update cascade on delete cascade;'); | ||
} | ||
|
||
async down(): Promise<void> { | ||
this.addSql('drop table if exists "ProductToTag" cascade;'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { BaseEntity, Entity, ManyToOne, PrimaryKey, Property, Ref, types } from "@mikro-orm/core"; | ||
import { Field, ObjectType } from "@nestjs/graphql"; | ||
import { v4 as uuid } from "uuid"; | ||
|
||
import { Product } from "./product.entity"; | ||
import { ProductTag } from "./product-tag.entity"; | ||
|
||
@Entity() | ||
@ObjectType() | ||
export class ProductToTag extends BaseEntity<ProductToTag, "id"> { | ||
@Field() | ||
@PrimaryKey({ type: "uuid" }) | ||
id: string = uuid(); | ||
|
||
@ManyToOne(() => Product, { onDelete: "cascade", ref: true }) | ||
product: Ref<Product>; | ||
|
||
@ManyToOne(() => ProductTag, { onDelete: "cascade", ref: true }) | ||
tag: Ref<ProductTag>; | ||
|
||
@Field() | ||
@Property({ type: types.boolean }) | ||
exampleStatus: boolean = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
demo/api/src/products/generated/dto/product-nested-product-to-tag.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// 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 { Field, ID, InputType } from "@nestjs/graphql"; | ||
import { IsBoolean, IsNotEmpty, IsUUID } from "class-validator"; | ||
|
||
@InputType() | ||
export class ProductNestedProductToTagInput { | ||
@IsNotEmpty() | ||
@Field(() => ID) | ||
@IsUUID() | ||
tag: string; | ||
|
||
@IsNotEmpty() | ||
@IsBoolean() | ||
@Field({ defaultValue: true }) | ||
exampleStatus: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
demo/api/src/products/generated/dto/product-tag-nested-product-to-tag.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// 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 { Field, ID, InputType } from "@nestjs/graphql"; | ||
import { IsBoolean, IsNotEmpty, IsUUID } from "class-validator"; | ||
|
||
@InputType() | ||
export class ProductTagNestedProductToTagInput { | ||
@IsNotEmpty() | ||
@Field(() => ID) | ||
@IsUUID() | ||
product: string; | ||
|
||
@IsNotEmpty() | ||
@IsBoolean() | ||
@Field({ defaultValue: true }) | ||
exampleStatus: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.