dto

김종민·2022년 6월 26일
0

Nuber-Server

목록 보기
5/34

들어가기
mutation 및 Query에 사용될 input(argument)과 output(return)을
정의해주는 dto에 대해서 알아보겠습니다.

1. users/dtos/create-account.dto.ts

createAccount Mutation에서 사용될 agument라고 사용하면 됨. 그리고 return까지.
그래서, createAccountInput, createAccountOutput 두개를 만듬.

import { InputType, ObjectType, PickType } from '@nestjs/graphql';
import { MutationOutput } from 'src/common/dtos/output.dto';
import { User } from '../entities/user.entity';

@InputType()
export class CreateAccountInput extends PickType(User, [
  'email',
  'password',
  'role',
]) {}
///type은 InputType으로 한다, 압력할 거니까, 그리고
///extends해서 PickType는 User Entity에서 사용할 것만 골른다, 초이스한다.
///생각하면 된다. omitType등등 여러타입이 있다, 공식문서 참조 바람.


@ObjectType()
export class CreateAccountOutput extends MutationOutput {}
///Output은 ObjectType으로 만들고, 공통으로 사용될 여지가 많아서
///common dto에 MutationOutput을 만들어 놓음.

///import from 되는 부분, @nestjs/graphql등등을 잘 확인한다.

2. common/dtos/output.dto.ts

import { Field, ObjectType } from '@nestjs/graphql';

@ObjectType()
export class MutationOutput {
  @Field(() => String, { nullable: true })
  error?: string;

  @Field(() => Boolean)
  ok: boolean;
}

ok, error는 거의 모든 mutation Output에 사용될 확률이 매우 커서 따로 만들어 놓음.
MutationOutput으로~
nullable는 없어도 된다라는 뜻임. 필수가 아니라는 뜻.

3. users/dtos/login.dto.ts

import { Field, InputType, ObjectType, PickType } from '@nestjs/graphql';
import { MutationOutput } from 'src/common/dtos/output.dto';
import { User } from '../entities/user.entity';

@InputType()
export class LoginInput extends PickType(User, ['email', 'password']) {}
///Input은 Picktype을 사용해서 User Entity의 email과 password를 사용
///역시 InputType임.

@ObjectType()
export class LoginOutput extends MutationOutput {
  @Field(() => String, { nullable: true })
  token?: string;
}
///OutPut은 ObjectType임..
///MutationOutput에 token을 추가해줌. 
///token은 nullable:true로 정의함.
profile
코딩하는초딩쌤

0개의 댓글