From bf5d184b04f03c2aa1696fe9a16268b79b198ccc Mon Sep 17 00:00:00 2001 From: C1320805 Elias Feijo de Almeida Pereira Lima Date: Tue, 7 Feb 2023 18:39:49 -0300 Subject: [PATCH] feat: MenuItems resolver findOne feat: menu field on MenuItem --- src/menu-items/entities/menu-item.entity.ts | 1 + src/menu-items/menu-items.module.ts | 3 ++- src/menu-items/menu-items.resolver.spec.ts | 18 ++++++++++++++++++ src/menu-items/menu-items.resolver.ts | 13 +++++++++++++ src/menu-items/menu-items.service.ts | 2 +- src/schema.gql | 2 ++ 6 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/menu-items/menu-items.resolver.spec.ts create mode 100644 src/menu-items/menu-items.resolver.ts diff --git a/src/menu-items/entities/menu-item.entity.ts b/src/menu-items/entities/menu-item.entity.ts index 55bce83..987002d 100644 --- a/src/menu-items/entities/menu-item.entity.ts +++ b/src/menu-items/entities/menu-item.entity.ts @@ -47,6 +47,7 @@ export class MenuItem { @Column({ nullable: true }) parentId?: number; + @Field(() => Menu) @ManyToOne(() => Menu, (menu) => menu.items, { lazy: true, onDelete: 'CASCADE', diff --git a/src/menu-items/menu-items.module.ts b/src/menu-items/menu-items.module.ts index 147ada4..a8f234e 100644 --- a/src/menu-items/menu-items.module.ts +++ b/src/menu-items/menu-items.module.ts @@ -2,10 +2,11 @@ import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { MenuItem } from './entities/menu-item.entity'; import { MenuItemsService } from './menu-items.service'; +import { MenuItemsResolver } from './menu-items.resolver'; @Module({ imports: [TypeOrmModule.forFeature([MenuItem])], - providers: [MenuItemsService], + providers: [MenuItemsService, MenuItemsResolver], exports: [MenuItemsService], }) export class MenuItemsModule {} diff --git a/src/menu-items/menu-items.resolver.spec.ts b/src/menu-items/menu-items.resolver.spec.ts new file mode 100644 index 0000000..618f984 --- /dev/null +++ b/src/menu-items/menu-items.resolver.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { MenuItemsResolver } from './menu-items.resolver'; + +describe('MenuItemsResolver', () => { + let resolver: MenuItemsResolver; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [MenuItemsResolver], + }).compile(); + + resolver = module.get(MenuItemsResolver); + }); + + it('should be defined', () => { + expect(resolver).toBeDefined(); + }); +}); diff --git a/src/menu-items/menu-items.resolver.ts b/src/menu-items/menu-items.resolver.ts new file mode 100644 index 0000000..5fc0360 --- /dev/null +++ b/src/menu-items/menu-items.resolver.ts @@ -0,0 +1,13 @@ +import { Resolver, Query, Args, Int } from '@nestjs/graphql'; +import { MenuItem } from './entities/menu-item.entity'; +import { MenuItemsService } from './menu-items.service'; + +@Resolver(() => MenuItem) +export class MenuItemsResolver { + constructor(private readonly menusService: MenuItemsService) {} + + @Query(() => MenuItem, { name: 'menuItem' }) + findOne(@Args('id', { type: () => Int }) id: number) { + return this.menusService.findOne(id); + } +} diff --git a/src/menu-items/menu-items.service.ts b/src/menu-items/menu-items.service.ts index 09df647..7690141 100644 --- a/src/menu-items/menu-items.service.ts +++ b/src/menu-items/menu-items.service.ts @@ -32,7 +32,7 @@ export class MenuItemsService { } findOne(id: number) { - return `This action returns a #${id} menu item`; + return this.menuItemRepository.findOneBy({ id: id }); } async update(input: UpdateMenuItemInput, manager: EntityManager) { diff --git a/src/schema.gql b/src/schema.gql index 6dc787c..78cbca4 100644 --- a/src/schema.gql +++ b/src/schema.gql @@ -39,6 +39,7 @@ type MenuItem { meta: JSONObject! children: [MenuItem!] parentId: Int + menu: Menu! menuId: Int } @@ -50,6 +51,7 @@ scalar JSONObject type Query { menus(first: Int, after: String, last: Int, before: String, sort: MenuSort, direction: Direction): MenuConnection! menu(id: Int!): Menu! + menuItem(id: Int!): MenuItem! } enum MenuSort {