nexus-graphql-mongoose-boilerplate 1일차
현재 회사에서 사용하고 있는 기술을 이해하기 위해 초기세팅 진행해보았습니다.
초기세팅 하면서 모르는 것들이 너무 많아서 며칠동안 블로그에 같이 정리해보도록 하겠습니다.
npm init -y
rootDir : 시작하는 루트 폴더.
outDir : 컴파일 후 생성되는 js파일이 생성될 폴더명.
target : 어떤 버전으로 컴파일할지 작성
outDir: './dist' rootDir: './src' 이렇게 설정하기
typescript 설치
yarn add -D typescript
npx tsc --init
tsconfig 파일이 생겨났습니다.
tsconfig.json
TypeScript로 짜여진 코드를 JavaScript로 컴파일하는 옵션을 설정하는 파일입니다. TypeScript 컴파일은 tsc 라는 명령어를 사용합니다.
yarn add -D @types/node ts-node ts-node-dev
ts-node 라이브러리 란
ts-node project. It allows you to run TypeScript in Node. js directly, without having to run the files through the TypeScript compiler (tsc)
"dev": "ts-node-dev src/index.ts"
이제 graphql, apollo-server-express, nexus 를 설치합니다.
yarn add apollo-server-express graphql nexus
Apollo 서버의 GraphQL 스키마는 typeDefs 와 resolvers 로 이뤄집니다. graphql-express 과 마찬가지입니다.
하지만 실행가능한 스키마를 만드는 방법은 여러방법이 있습니다.
graphql-tools 를 이용하여 makeExecutableSchema 를 호출하여 typeDefs 와 resolvers 를 통해 만드는 방법이 있지만,
우리에겐 code-first-development (CDL)를 사용하려고 합니다.
schema-first-development (SDL) 방식도 익혀둘 필요가 있습니다.
1. typeDefs
graphql query 및 mutation의 schema definition에 관한 정보
2. resolvers
typeDef에서 정의한 query 및 mutation의 실제 구현
3. context
인증과 같은 전처리가 필요할 때 사용하는 객체. 헤더에 원하는 값을 실어줄 수 있고, middleware와 같은 개념이라고 생각하면 될 것 같다.