Development/Nest.js

[nest.js/graphql] 설치 및 세팅

Tunko 2021. 4. 10. 01:15

Documentation | NestJS - A progressive Node.js framework

터미널에 아래 입력 패키지 설치!

npm i @nestjs/graphql graphql-tools graphql apollo-server-express

// 정상 실행 확인

npm run start:dev 

프로젝트에 src 폴더 내부에

main.ts
// 앱플리케이션의 메인 파일 AppModule을 Nest를 기반으로 구동한다.

app.module.ts
여기에 GraphQL 을 연동한다.

비어있는 app.module.ts

@Module({
  imports: [],
  controllers: [],
  providers: [],
})
export class AppModule {}
  1. GraphQL 설정 추가

    @Module({
    imports: [
     GraphQLModule.forRoot({
       autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
     }),
    ],
    controllers: [],
    providers: [],
    })
    export class AppModule {}
  2. test module 생성

    nest g mo {모듈 이름}
  3. test 폴더 안에 test.module.ts 추가 확인 및 testResolver.ts 추가

    import { Module } from '@nestjs/common';
    import { TestResolver } from './test.resolver';
    

@Module({
providers: [TestResolver],
})
export class TestModule {}


```typescript
import { Query, Resolver } from '@nestjs/graphql';

@Resolver()
export class TestResolver {
    @Query(() => Boolean)
    isTest() {
        return true;
    }
} 
  1. npm run start:dev 구동확인

http://localhost:3000/graphql 접속 확인
GraphQL playground가 열리는것을 확인한다.

위에서처럼 하는 이유는 graphQL 을 구동하기위해선 1개 이상의 Query 가 정의되어 있어야 하기에 일단 추가해주었다.

반응형