getting item children list

master
Lauren Liberda 2021-07-17 18:34:30 +02:00
parent c894da4449
commit 1f4a51f8d4
3 changed files with 17 additions and 1 deletions

View File

@ -15,4 +15,7 @@ export class ItemModel {
@Field({ nullable: true })
parent?: ItemModel;
@Field((type) => [ItemModel])
children: ItemModel[];
}

View File

@ -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);

View File

@ -21,13 +21,21 @@ export class ItemsService {
return this.itemRepository.findOne(id);
}
async getItemParent(item: ItemModel) {
async getItemParent(item: ItemModel): Promise<Item | undefined> {
const childItem = await this.itemRepository.findOneOrFail(item.id, {
relations: ['parent'],
});
return childItem.parent;
}
async getItemChildren(item: ItemModel): Promise<Item[]> {
return this.itemRepository.find({
where: {
parent: item.id,
},
});
}
async createItem(input: NewItemInput): Promise<Item> {
const item = this.itemRepository.create({
...input,