Building a GraphQL Server with Nest JS

Pragmatic Reviews
The Startup
Published in
2 min readApr 26, 2020
Building a GraphQL Server with Nest JS

In this video we are going to generate a GraphQL API with Nest JS.

Nest JS is a Node.js Framework that fully supports TypeScript (yet still enables developers to code in pure JavaScript) and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).

In order to generate the GraphQL API with NestJS we are going to create a new application from scratch using the NestJS CLI, that we can install like this: npm i -g @nestjs/cli

Then we can use the Nest JS CLI to create a new application:

nest new nestjs-graphql

cd nestjs-graphql

And then we have to install some GraphQL and Apollo server libraries.

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

npm i --save class-validator

The ‘class-validator’ library will allow us to validate the input for the mutations.

The GraphQL Schema that is going to be the source to generate the API will be:

type Video {
id: ID!
title: String!
url: String!
author: User!
}

type User {
id: ID!
name: String!
}

type Query {
videos: [Video!]!
}

input NewVideo {
title: String!
url: String!
userId: String!
}

type Mutation {
createVideo(input: NewVideo!): Video!
}

Taking that schema as the input, the generate-typings.ts script (below) is going to create the TypeScript classes to be used by the application.

These are the commands to generate the classes in TypeScript from the GraphQL schema:

tsc src/generate-typings.ts

node src/generate-typings.js

Once we have those classes we can work on the new Video Module that is going to include a Video DTO, a Video Resolver and a Video Service.

For more details, you can watch the video below. Thanks!

--

--

Pragmatic Reviews
The Startup

Online training on multiple technology topics such as Programming, Cloud Computing, Testing Automation, SEO and more.