장재혁

Add typeorm ,graphql settings for api testings

import { Field, InputType } from '@nestjs/graphql';
import { IsAlpha } from 'class-validator';
import { Entity } from 'typeorm';
@Entity()
@InputType()
export class CreateMyInput {
@IsAlpha()
@Field()
name: string;
@Field({ nullable: true })
type?: string;
}
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { MypageService } from './mypage.service';
import { MyPage } from './mypage.entity';
import { CreateMyInput } from './dto/create-mypage.input';
@Resolver((of) => MyPage)
export class MypageResolver {
constructor(private myPageService: MypageService) {}
@Query((returns) => [MyPage])
myPage(): Promise<MyPage[]> {
return this.myPageService.findAll();
}
@Mutation((returns) => MyPage)
createMyPage(
@Args('createMyInput') createMyPage: CreateMyInput,
): Promise<MyPage> {
return this.myPageService.createMy(createMyPage);
}
}
import { Test, TestingModule } from '@nestjs/testing';
import { MypageService } from './mypage.service';
describe('MypageService', () => {
let service: MypageService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [MypageService],
}).compile();
service = module.get<MypageService>(MypageService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateMyInput } from './dto/create-mypage.input';
import { MyPage } from './mypage.entity';
@Injectable()
export class MypageService {
constructor(
@InjectRepository(MyPage) private myPageRepository: Repository<MyPage>,
) {}
async createMy(createMyInput: CreateMyInput): Promise<MyPage> {
const newPage = this.myPageRepository.create(createMyInput);
return this.myPageRepository.save(newPage);
}
async findAll(): Promise<MyPage[]> {
return this.myPageRepository.find();
}
}