From 1f4a51f8d4729a97d356cd8e4f8d95a0bf27a668 Mon Sep 17 00:00:00 2001 From: Lauren Liberda Date: Sat, 17 Jul 2021 18:34:30 +0200 Subject: [PATCH] getting item children list --- src/items/items.model.ts | 3 +++ src/items/items.resolver.ts | 5 +++++ src/items/items.service.ts | 10 +++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/items/items.model.ts b/src/items/items.model.ts index fabb7ad..425ada8 100644 --- a/src/items/items.model.ts +++ b/src/items/items.model.ts @@ -15,4 +15,7 @@ export class ItemModel { @Field({ nullable: true }) parent?: ItemModel; + + @Field((type) => [ItemModel]) + children: ItemModel[]; } diff --git a/src/items/items.resolver.ts b/src/items/items.resolver.ts index 055931f..40eca4e 100644 --- a/src/items/items.resolver.ts +++ b/src/items/items.resolver.ts @@ -25,6 +25,11 @@ export class ItemsResolver { return this.itemsService.getItemParent(item); } + @ResolveField() + async children(@Parent() item: ItemModel) { + return this.itemsService.getItemChildren(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 1cdf8aa..6591e27 100644 --- a/src/items/items.service.ts +++ b/src/items/items.service.ts @@ -21,13 +21,21 @@ export class ItemsService { return this.itemRepository.findOne(id); } - async getItemParent(item: ItemModel) { + async getItemParent(item: ItemModel): Promise { const childItem = await this.itemRepository.findOneOrFail(item.id, { relations: ['parent'], }); return childItem.parent; } + async getItemChildren(item: ItemModel): Promise { + return this.itemRepository.find({ + where: { + parent: item.id, + }, + }); + } + async createItem(input: NewItemInput): Promise { const item = this.itemRepository.create({ ...input,