getting item children list
This commit is contained in:
parent
c894da4449
commit
1f4a51f8d4
|
@ -15,4 +15,7 @@ export class ItemModel {
|
||||||
|
|
||||||
@Field({ nullable: true })
|
@Field({ nullable: true })
|
||||||
parent?: ItemModel;
|
parent?: ItemModel;
|
||||||
|
|
||||||
|
@Field((type) => [ItemModel])
|
||||||
|
children: ItemModel[];
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,11 @@ export class ItemsResolver {
|
||||||
return this.itemsService.getItemParent(item);
|
return this.itemsService.getItemParent(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ResolveField()
|
||||||
|
async children(@Parent() item: ItemModel) {
|
||||||
|
return this.itemsService.getItemChildren(item);
|
||||||
|
}
|
||||||
|
|
||||||
@Mutation((returns) => ItemModel)
|
@Mutation((returns) => ItemModel)
|
||||||
async createItem(@Args('itemData') itemData: NewItemInput) {
|
async createItem(@Args('itemData') itemData: NewItemInput) {
|
||||||
return this.itemsService.createItem(itemData);
|
return this.itemsService.createItem(itemData);
|
||||||
|
|
|
@ -21,13 +21,21 @@ export class ItemsService {
|
||||||
return this.itemRepository.findOne(id);
|
return this.itemRepository.findOne(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getItemParent(item: ItemModel) {
|
async getItemParent(item: ItemModel): Promise<Item | undefined> {
|
||||||
const childItem = await this.itemRepository.findOneOrFail(item.id, {
|
const childItem = await this.itemRepository.findOneOrFail(item.id, {
|
||||||
relations: ['parent'],
|
relations: ['parent'],
|
||||||
});
|
});
|
||||||
return childItem.parent;
|
return childItem.parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getItemChildren(item: ItemModel): Promise<Item[]> {
|
||||||
|
return this.itemRepository.find({
|
||||||
|
where: {
|
||||||
|
parent: item.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async createItem(input: NewItemInput): Promise<Item> {
|
async createItem(input: NewItemInput): Promise<Item> {
|
||||||
const item = this.itemRepository.create({
|
const item = this.itemRepository.create({
|
||||||
...input,
|
...input,
|
||||||
|
|
Loading…
Reference in a new issue