๐Nest.JS์์ ๐๏ธgraphQl์ด ์ด๋ป๊ฒ ๋์ํ๋์ง
1.
nest g application
- nest.js app ์์ฑ์ ์ํ ๋ช ๋ น์ด
2.? What name would you like to use for the new project?
- ํ๋ก์ ํธ ๋ช ์ ๋ ฅ
npm i
-package.json์ ์์ฑ๋ ํจํค์ง ๋ชจ๋ ํ์น
- ๊น ์ฐ๊ฒฐ
-git init
-git remote add origin <git url>
-๋ช ๋ น์ด ํ๋ ํธ,gitignore
=> node ๊ฒ์, ignore ํ์ผ ์์ฑ
-add, commit, push ํ์ฌ main ๋ธ๋์น ์ฌ๋ฆฌ๊ธฐ
-์์ ๋ธ๋์น ์์ฑ (issue#1)
WHAT ISโ
npm i @nestjs/graphql graphql apollo-server-express
- nestjs/graphql์ nest.js๋ง์ ๋ฐฉ์์ผ๋ก apollo-server-express๋ฅผ ๊ธฐ๋ฐํ์ฌ ์๋ํ๋ค.
import { Module } from '@nestjs/common'; @Module({ imports: [], controllers: [], providers: [], }) export class AppModule {}
- main.ts๋ก import๋๋ ์ ์ผํ ๋ชจ๋์ด๋ค,
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000); } bootstrap();
- app์ ์คํํ๊ธฐ ์ํ ํ์ผ
- NestFactory๊ฐ AppModule๋ก ๋ถํฐ app์ ์์ฑํ๋ค.
- ๊ฒฐ๊ตญ, ๋ชจ๋ ๊ฑด AppModule๋ก import๋๋ค.
EX> ๋ฐ์ดํฐ ๋ฒ ์ด์ค๋ฅผ ๊ฐ์ ธ์ค๊ณ , GraphGL, User ๋ฑ- ๋ฐ๋ผ์ AppModule์ GraphGL ๋ชจ๋ ์ญ์ ์ถ๊ฐํด์ผํ๋ค.
HOW TO USEโโ GraphGL ๋ชจ๋
GraphQLModule.forRoot()
forRoot()
๋ root module์ ์ค์ ํ๊ธฐ ์ํ ๋ช ๋ น์ด๋ก ์ด ๊ฒฝ์ฐ GraphQL์ด forRoot์ ํด๋น๋๋ค.Apollo Server requires either an existing schema, modules or typeDefs
- ์ด๋ Apollo ์๋ฒ๊ฐ schema, modules ๋๋ typeDefs ์ค ํ๋๋ฅผ ํ์๋ก ํ๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค.
=> ์ฆ, GraphQL ์๋ฒ๋ ์๋ฌด๋ฐ ์ ๋ณด ์์ด ์์ฑํ ์ ์๋ค.
=> GraphQL ์๋ฒ๋ ์ด๋ป๊ฒ ๊ตฌ์ฑ๋์ด์๋์ง๋ฅผ ์ค๋ช ํด์ฃผ๊ธฐ ์ํด์ 1>resolver
์ 2>GraphQL Schema
๋ฅผ ํ์๋ก ํ๋ค.
nest.js์ graphQL์ด ๊ฐ์ด ๋์ํ๋ ๋ฐฉ์์ ๋ํด์ ์ตํ๋์.
- ๋๊ผด๋ผ์ค ์ค์ ์ฐ๋ฒ์ด์ธ ๊ฐ์ ์ค