61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
import {
|
|
Args,
|
|
ID,
|
|
Mutation,
|
|
Parent,
|
|
Query,
|
|
ResolveField,
|
|
Resolver,
|
|
} from '@nestjs/graphql';
|
|
import { NewItemInput } from './dto/new-item.input';
|
|
import { EANService } from './ean/ean.service';
|
|
import { ItemModel, PaginatedItems } from './items.model';
|
|
import { ItemsService } from './items.service';
|
|
|
|
@Resolver((of) => ItemModel)
|
|
export class ItemsResolver {
|
|
constructor(private itemsService: ItemsService, private eans: EANService) {}
|
|
|
|
@Query((returns) => ItemModel, { nullable: true })
|
|
async item(@Args('id', { type: () => ID }) id: string) {
|
|
return this.itemsService.getItem(id);
|
|
}
|
|
|
|
@Query((returns) => PaginatedItems)
|
|
async itemList(
|
|
@Args('cursor', { type: () => ID, nullable: true }) cursor?: string,
|
|
) {
|
|
return this.itemsService.getItemList(cursor ? parseInt(cursor, 10) : 0);
|
|
}
|
|
|
|
@ResolveField()
|
|
ean13(@Parent() item: ItemModel) {
|
|
return this.eans.fromID(item.id);
|
|
}
|
|
|
|
@ResolveField()
|
|
async parent(@Parent() item: ItemModel) {
|
|
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);
|
|
}
|
|
|
|
@ResolveField()
|
|
async descendants(@Parent() item: ItemModel) {
|
|
return this.itemsService.getItemDescendants(item);
|
|
}
|
|
|
|
@Mutation((returns) => ItemModel)
|
|
async createItem(@Args('itemData') itemData: NewItemInput) {
|
|
return this.itemsService.createItem(itemData);
|
|
}
|
|
}
|