@IsOptional()Checks if value is missing and if so, ignores all validators.
해당 필드(컬럼,파라미터.. 다 통합되어 있으니 뭐라고 불러야 할지 모르겠다.)
아무튼 쿼리를 입력할 때 해당 필드 자체가 없어도 된다는 것. (값이 없는 nullable과 구분하자)
// "isVegan" field 자체가 없는 것
mutation{
createRestaurant(input:{
name:"test3"
address:"asdfasdf"
})
}
// "isVegan" 의 값이 없는 것
import { Field, InputType, ObjectType } from '@nestjs/graphql';
import { IsBoolean, IsOptional, IsString, Length } from 'class-validator';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@InputType({ isAbstract: true })
@ObjectType() // GraphQL decorator : 자동으로 스키마를 빌드하기 위해 사용
@Entity() // TypeORM decorator : DB에 해당 클래스를 테이블과 매핑하여 저장
export class Restaurant {
@PrimaryGeneratedColumn() // for typeORM (database)
@Field((type) => Number) // for GraphQL
id: number;
// @Field() : 역시 타입을 리턴하는 함수를 1번째 인자로 갖는다.
@Field((type) => String)
@Column()
@IsString()
@Length(5, 10)
name: string;
// GraphQL과 typeORM(database) 에 default 값을 알려줄 수 있다.
// validation 적용내용: 해당 필드 입력은 선택사항, 만약 value가 있다면 boolean이어야 한다.
@Field((type) => Boolean, { defaultValue: true }) // for GraphQL schema
@Column({ default: true }) // for typeORM (database)
@IsOptional() // for DTOs : validation decorater
@IsBoolean() // for DTOs : validation decorater
isVegan?: boolean;
@Field((type) => String)
@Column()
@IsString()
address: string;
}
localhost:3000/graphql

default 값이 true로 지정된 것은 잘 적용되었다.
근데..
isVegan: Boolean! = true
강의에서는 여기서 ! 도 없다고 나오는데 나는 있다..
뭐가 또 잘못된거니… 싶었는데..
막상 쿼리를 날려보면 제대로 Optional 이 적용된다..