장재혁

Add typeorm ,graphql settings for api testings

1 +import { Field, InputType } from '@nestjs/graphql';
2 +import { IsAlpha } from 'class-validator';
3 +import { Entity } from 'typeorm';
4 +
5 +@Entity()
6 +@InputType()
7 +export class CreateMyInput {
8 + @IsAlpha()
9 + @Field()
10 + name: string;
11 +
12 + @Field({ nullable: true })
13 + type?: string;
14 +}
1 +import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
2 +import { MypageService } from './mypage.service';
3 +import { MyPage } from './mypage.entity';
4 +import { CreateMyInput } from './dto/create-mypage.input';
5 +
6 +@Resolver((of) => MyPage)
7 +export class MypageResolver {
8 + constructor(private myPageService: MypageService) {}
9 +
10 + @Query((returns) => [MyPage])
11 + myPage(): Promise<MyPage[]> {
12 + return this.myPageService.findAll();
13 + }
14 +
15 + @Mutation((returns) => MyPage)
16 + createMyPage(
17 + @Args('createMyInput') createMyPage: CreateMyInput,
18 + ): Promise<MyPage> {
19 + return this.myPageService.createMy(createMyPage);
20 + }
21 +}
1 +import { Test, TestingModule } from '@nestjs/testing';
2 +import { MypageService } from './mypage.service';
3 +
4 +describe('MypageService', () => {
5 + let service: MypageService;
6 +
7 + beforeEach(async () => {
8 + const module: TestingModule = await Test.createTestingModule({
9 + providers: [MypageService],
10 + }).compile();
11 +
12 + service = module.get<MypageService>(MypageService);
13 + });
14 +
15 + it('should be defined', () => {
16 + expect(service).toBeDefined();
17 + });
18 +});
1 +import { Injectable } from '@nestjs/common';
2 +import { InjectRepository } from '@nestjs/typeorm';
3 +import { Repository } from 'typeorm';
4 +import { CreateMyInput } from './dto/create-mypage.input';
5 +import { MyPage } from './mypage.entity';
6 +
7 +@Injectable()
8 +export class MypageService {
9 + constructor(
10 + @InjectRepository(MyPage) private myPageRepository: Repository<MyPage>,
11 + ) {}
12 +
13 + async createMy(createMyInput: CreateMyInput): Promise<MyPage> {
14 + const newPage = this.myPageRepository.create(createMyInput);
15 +
16 + return this.myPageRepository.save(newPage);
17 + }
18 +
19 + async findAll(): Promise<MyPage[]> {
20 + return this.myPageRepository.find();
21 + }
22 +}