item ancestors
This commit is contained in:
parent
92993ef105
commit
9e22638fc2
|
@ -16,6 +16,9 @@ export class ItemModel {
|
|||
@Field({ nullable: true })
|
||||
parent?: ItemModel;
|
||||
|
||||
@Field((type) => [ItemModel])
|
||||
ancestors: ItemModel[];
|
||||
|
||||
@Field((type) => [ItemModel])
|
||||
children: ItemModel[];
|
||||
|
||||
|
|
|
@ -25,6 +25,11 @@ export class ItemsResolver {
|
|||
return this.itemsService.getItemParent(item);
|
||||
}
|
||||
|
||||
@ResolveField()
|
||||
async ancestors(@Parent() item: ItemModel) {
|
||||
return this.itemsService.getItemAncestors(item);
|
||||
}
|
||||
|
||||
@ResolveField()
|
||||
async children(@Parent() item: ItemModel) {
|
||||
return this.itemsService.getItemChildren(item);
|
||||
|
|
|
@ -28,6 +28,14 @@ export class ItemsService {
|
|||
return childItem.parent;
|
||||
}
|
||||
|
||||
async getItemAncestors(item: ItemModel): Promise<Item[]> {
|
||||
const childItem = await this.itemRepository.findOneOrFail(item.id, {
|
||||
relations: ['parent'],
|
||||
});
|
||||
// TreeRepository.findAncestors()[0] is always the child item
|
||||
return (await this.treeRepository.findAncestors(childItem)).slice(1);
|
||||
}
|
||||
|
||||
async getItemChildren(item: ItemModel): Promise<Item[]> {
|
||||
return this.itemRepository.find({
|
||||
where: {
|
||||
|
|
Loading…
Reference in a new issue