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,