반응형
1. graphql 내용
1. #2.0 Apollo Server Setup
1) Graphql은 apollo-server-express를 기반으로 동작함.
-- 명령어--
$npm i @nestjs/graphql graphql-tools graphql @apollo/server => 23년 6월 기준 아래로 변경
$npm i @nestjs/graphql @nestjs/apollo @apollo/server graphql
2) app.moudule에서 Grahqpl을 설정할 때, forRoot()는 root module 을 설정하는 것이다.
2. # 2.3
@Args 는 graphql 에서 받아오는 인자값을 뜻한다.
3. #2.4
1) @InputType()
InputType dto 를 만들어서 @Args에 하나씩 적기보다는 만든 inputType을 넣는 것이 효율적임
2) @ArgsType()
위의 inputType() 에 대해서 다시 각각의 변수들을 적을 수 있도록 해줌
4. #2.5
1) 검증
$npm i class-validator
$npm i class-transformer
위의 명령어를 통해, 우리가 만든 dto 또는, entity에 대해 검증을 할 수 있다.
사용법은 @IsString(), IsBoolean() 등 해당 타입에 맞추어서 데코레이터를 달아주면 된다.
```
import { Field, InputType } from '@nestjs/graphql';
import { IsBoolean, IsString } from 'class-validator';
@InputType()
export class CreateRestaurantDto {
@Field(() => String)
@IsString()
name: string;
@Field(() => Boolean, { nullable: true })
@IsBoolean()
isVegan: boolean;
@Field(() => String)
@IsString()
address: string;
@Field(() => String)
@IsString()
@Length(5, 10) // 최소5,최대 10글자
ownerName: string;
}
```
위에서 적용한 후, 해당 검증 데코레이터를 사용하려면 main.ts 에서 app.useGlobalPipes(new ValidationPipe()) 를 적어줘야 된다.
```
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe());
await app.listen(3456);
}
bootstrap();
```
2. typeorm
1. #3.3 TypeOrm Setup
1) $npm i --save @nestjs/typeorm typeorm pg
2. #3.4 Introducing ConfigService
1) $npm i --save @nestjs/config
해당 패키지를 설치하면 .env를 읽을 수 있다.
2) isGlobal은 어플리케이션 어디서나 config 모듈에 접근할 수 있는 것을 뜻함.
3. #3.5 Configuring ConfigService
1) $npm i cross-env
해당 패키지는 가상변수를 설정할 수 있게 해준다.
```
ConfigModule.forRoot({
isGlobal: true,
envFilePath: process.env.NODE_ENV === 'dev' ? '.dev.env' : '.test.env',
ignoreEnvFile: process.env.NODE_ENV === 'prod',
}),
반응형
'백엔드 > NestJs' 카테고리의 다른 글
[nestjs] new DataLoader (0) | 2023.07.02 |
---|---|
[nestjs] nx 모노레포?? (0) | 2023.06.25 |
typeorm 1:1, 1:N 관계 (0) | 2023.06.11 |
필수 패키지 설치 (0) | 2023.05.02 |
[nestjs] 미들웨어에서 method 지정 코드 (0) | 2023.03.21 |