From c1df170c6a7d8629e07efe78eda1e2ce07ab87ad Mon Sep 17 00:00:00 2001 From: Lauren Liberda Date: Sat, 17 Jul 2021 22:51:12 +0200 Subject: [PATCH] should be descendents but typeorm is broken --- src/items/items.model.ts | 3 +++ src/items/items.resolver.ts | 5 +++++ src/items/items.service.ts | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/src/items/items.model.ts b/src/items/items.model.ts index 425ada8..e837a05 100644 --- a/src/items/items.model.ts +++ b/src/items/items.model.ts @@ -18,4 +18,7 @@ export class ItemModel { @Field((type) => [ItemModel]) children: ItemModel[]; + + @Field((type) => [ItemModel], { deprecationReason: 'typeorm issues' }) + descendents: ItemModel[]; } diff --git a/src/items/items.resolver.ts b/src/items/items.resolver.ts index 40eca4e..78bac4d 100644 --- a/src/items/items.resolver.ts +++ b/src/items/items.resolver.ts @@ -30,6 +30,11 @@ export class ItemsResolver { return this.itemsService.getItemChildren(item); } + @ResolveField() + async descendents(@Parent() item: ItemModel) { + return this.itemsService.getItemDescendents(item); + } + @Mutation((returns) => ItemModel) async createItem(@Args('itemData') itemData: NewItemInput) { return this.itemsService.createItem(itemData); diff --git a/src/items/items.service.ts b/src/items/items.service.ts index 6591e27..ed3486a 100644 --- a/src/items/items.service.ts +++ b/src/items/items.service.ts @@ -36,6 +36,13 @@ export class ItemsService { }); } + async getItemDescendents(item: ItemModel): Promise { + const parentItem = await this.itemRepository.findOneOrFail(item.id, { + relations: ['parent'], + }); + return this.treeRepository.findDescendants(parentItem); + } + async createItem(input: NewItemInput): Promise { const item = this.itemRepository.create({ ...input,