Building GraphQL API with NestJS
Tags: webdev, node, graphql, showdev
Published: 2020-09-16 00:13:28

<p align="justify">I have developed web applications for more than a decade. I'd known the good times when `Web` was built around `PHP`, the dawn of new masters of the industry. In those days I was impressed by the pace of development for new frameworks and their features. But, mostly, I was disappointed by existing approaches of building large, reliable, and what's really important, scalable applications and APIs. Frameworks keep appearing at a fast pace. But most of them, aim at totally different principles, copying attitudes of existing analogs. Most of them have too raw responsibility rules, which, basically, allowed people to `express` themselves in a way and a manner that sometimes can't be or, even, should be, predicted by colleagues. I was seeking to find a framework, which will be flexible enough in a place, but offers predictable and straightforward responsibility rules, so scaling of the application won't raise maintenance effort geometrically.</p>

<p align="justify">At the moment when I decided to switch to `Node.js`, to pursue modern technology stack, performance reasons, SPAa development, better tools to work with instant messaging and etc, I was looking for the solution that can replace old frameworks in terms of reliability and scalability, but bring modern development technologies in my life. That's how I found `NestJs`,  which is, basically, is higher abstraction level framework on `Express`, which, is turn, built on top of `Node.js`.</p>

## Introduction

<p align="justify">NestJs is a rising star among `Node.js` frameworks, growing popularity it owes to modular, scalable and robust architecture. Initially inspired by <b>`Angular`</b>, it combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming)within full support of <b>`Typescript`</b>. With usage of <b>`Express`</b> (or <b>`Fastify`</b>) under the hood, which makes it compatible with the majority of <b>`Express`</b> middleware.</p>

### Why **NestJs**?

>

<ol>
<li><p align="justify">`Typescript` is a key-feature in modern <b>`web`</b> development, we always prefer well-typed codebase if possible.</p>
<ul>
    <li><p align="justify">Use decorators to observe, modify, validate or replace your classes, while preserving DRY principles and being concise.</p></li>
    <li><p align="justify">Stay predictable and easy-to-understand. Design fully typed business-logic for your application, for rest - ~~MasterCard~~ <b>SRP</b> (single-responsibility principle).</p></li>
    <li><p align="justify">You should definitely enjoy `NestJs` if you familiar with `Angular` . Stick to the well-known dependency injection principle, guards, interceptors, modular structure and etc..</p></li>
    </ul>
    </li>
<li><p align="justify">One of the `NestJs` advantages - modular approach, we always want to keep our applications robust and scalable.</p>
    <ul>
<li><p align="justify">Well-planed modular project structure includes a best practice for lion's share of operations you would want to perform, allows you colleagues to predict which code goes where, which kind of class should be used for this type of operation, etc.</p></li>
<li><p align="justify">Unlike most of popular frameworks, in NestJs you won't encounter a question how to validate your DTOs. Because of thoughtful structure your incoming data will be typed by DTOs, and validated with best NestJs practices by `class-validator` package.</p></li>
<li><p align="justify">You can easily scale your application with `NestJs`. Nest natively supports the microservice architectural style of development. Has a widely covered documentation and different approaches available.</p></li>
    </ul>
        </li>
<li><p align="justify">As I mentioned in previous para, most of necessary packages already has an integration with NestJs via `@nestjs/packagename` wrapper designed to keep the project structure still predictable even with third-party libraries.</p></li>  
<li><p align="justify">NestJs is growing very quickly, already passed 7th version and goes on. High community support encourages devs to keep the framework up to modern standards, at the moment more than 28.8k stars proving.After each release you can always find updated, well-structured comprehensive documentation, with step-by-step best practices designs for most of popular cases.</p></li> 
<li><p align="justify">Use modern approaches on building `GraphQL` APIs, wrapped by `@nestjs/graphql` package.</p></li> 
<li><p align="justify">Simple and straightforward request lifecycle makes it easier to understand how service all parts of the request - validation, data interception, transformation, response part and etc.</p></li> 
<li><p align="justify">Scaffold your project with style. Use convenient CLI to start a new project, generate module, build, install or update packages libs.</p></li> 
    </ul>
    </ol>

#### Cons
<ol>
<li><p align="justify">Due to single-responsibility principle (SRP), `NestJs` requires you to clearly understand which class should be responsible for the action you want to perform, so at the begging it make take some time to recognise / read according docs which class you should use.</p></li>
<li><p align="justify">Some features or integrations are built on top of certain libs, to follow basic responsibility pattern you should get used to this libs, accept their style and approaches.</p></li>
    </li>
</ol>

>

## Prerequisites

>

### Step 1 - Installing NestJs CLI

<p align="justify">To get started with NestJs, you will need to globally install the Nest CLI. It is a command-line tool created to scaffold fresh NestJs app.</p>

```bash
$ npm install -g @nestjs/cli
```

<p align="justify">This will give you access to the`nest`command for project installation and other project specific commands.</p>

<p align="justify">Next, run the following command to scaffold a new project named`todoapp-nest`within your development folder:</p>

```bash
$ nest new todoapp-nest
```

<p align="justify">You will be asked which package manager you would like to use, just follow the prompt and respond accordingly. Next, once the installation is complete, switch your working directory into the newly created project:</p>

```bash
$ cd todoapp-nest
```

<p align="justify">Start the application with:</p>

```bash
npm run start
```

<p align="justify">You can also run the followingcommand in order to use Nodemon for the project:</p>

```bash
// start the application using --watch option
npm run start:dev
```

<p align="justify">Navigate to`http://localhost:3000`in your browser and you will see the<b>Hello World</b>! message as shown in the following image:</p>

![Start-the-App](https://2muchcoffee.com/blog/content/images/2020/09/Start-the-App.png)

### Step 2 - Adding GraphQL

<p align="justify"><b>GraphQL</b> is a query language for APIs and a runtime for fulfilling those queries with your existing data. It provides a complete and understandable description of the data in your API, gives clients more flexibility and customization tools for developer needs.</p>

<p align="justify">While REST API is an architectural concept for network-based software. GraphQL, on the other hand, is a query language, a specification, and a set of tools that operates over a single endpoint using HTTP.</p>

<p align="justify">The advantage of GraphQL - you can ask data you need in particular, sustain security and increase performance.</p>

<p align="justify">NestJs provider two ways of building GraphQL APIs:</p>
<ul>
    <li><p align="justify"><u><a href="https://docs.nestjs.com/graphql/quick-start#code-first" rel="nofollow">Code first</a></u></p></li>
    <li><p align="justify"><u><a href="https://docs.nestjs.com/graphql/quick-start#schema-first" rel="nofollow">Schema first</a></u></p></li>
    </ul>

<p align="justify">We prefer <b>`Code First`</b> approach, because we allowed to use decorators and Typescript classes to generate the corresponding <b>`GraphqQL`</b> schema. Advantages of this approach - we can avoid context switching between different languages, use TS support while writing our schemas,  benefit from <b>`class-validator` and `class-transformer`</b> packages, validating our data, use `GraphQL CLI`, to generate our modules, which is accessible only for `code first` approach.</p>
<p align="justify">In order to get started with GraphQL, you need to install packages:</p>

```bash
$ npm i @nestjs/graphql graphql-tools graphql
```

<p align="justify">Depending on what underlying platform you use (`Express` or `Fastify`), you must also install either `apollo-server-express` or `apollo-server-fastify`.</p>

<p align="justify">Next, we should register GraphQL module in `app.module.ts`:</p>

```tsx
import { GraphQLModule } from '@nestjs/graphql';

@Module({
  imports: [
    GraphQLModule.forRoot({
      autoSchemaFile: true,
    }),
    TasksModule
  ],
...
```

<p align="justify">To maintain GraphQL hints add following code to your nest-cli.json:</p>

```bash
{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "plugins": ["@nestjs/graphql/plugin"]
  }
}
```

<p align="justify">With the packages installed, let's create task module.</p>

### Step 3 - Creating a Module

<p align="justify">In order to generate module, we need to run the following command:</p>

```bash
$ nest generate module tasks
```

<p align="justify">This creates a new folder named`tasks`within the`src`folder. Within the`books`folder you will find a`tasks.module.ts`file:</p>

```tsx
import { Module } from '@nestjs/common';
@Module({})
export class TasksModule {}
```

<p align="justify">This was generated by the command and the module has also been added to the`app.module.ts`which happens to be the root module of the application.</p>

<p align="justify">Next, we have to create resolver.</p>

>

### Step 4 - Creating a Resolver

<p align="justify">To generate resolver paste this in command prompt:</p>

```bash
$ nest generate resolver tasks
```

<p align="justify">The command above will create resolver `tasks.resolver.ts` file inside `tasks` folder and update `tasks.module.ts` listing new provider.</p>

```tsx
import { Resolver } from '@nestjs/graphql';

@Resolver('Tasks')
export class TasksResolver {}

```
### Step 5 - Add a Model

<p align="justify">To describe an object we want to work with, we should create a model for `Task` instance. To do this create `models` folder and add `task.model.ts` inside, than add this code:</p>

```tsx
import { Field, ID, ObjectType } from '@nestjs/graphql';

@ObjectType()
export class Task {
  @Field(() => ID)
  id: string;

  @Field()
  title: string;

  @Field()
  description: string;

  @Field()
  completed: boolean;
}
```

<p align="justify">To simplify things we won't be connecting to the database now, so let's create a sample mock data for our list. Create `mocks` folder in the `src` folder, than add file named `tasks.mock.ts` with following code:</p>

```tsx
export const TASKS = [
  { id: '1', title: 'Task #1', description: "This is the description for the task #1", completed: false },
  { id: '2', title: 'Task #2', description: "This is the description for the task #2", completed: false },
  { id: '3', title: 'Task #3', description: "This is the description for the task #3", completed: true },
  { id: '4', title: 'Task #4', description: "This is the description for the task #4", completed: false },
  { id: '5', title: 'Task #5', description: "This is the description for the task #5", completed: true },
  { id: '6', title: 'Task #6', description: "This is the description for the task #6", completed: false },
  { id: '7', title: 'Task #7', description: "This is the description for the task #7", completed: false },
];
```

<p align="justify">Next, we should create a service to implement all the logic for TodoApp.</p>

### Step 6 - Generating a Service

```bash
$ nest generate service tasks
```

<p align="justify">This command will create a new file named `tasks.service.ts` within ./src/tasks folder and update `tasks.module.ts` with a new provider.</p>

<p align="justify">First let's import our mocked data, so we can serve our queries with data.</p>

```tsx
import { Injectable } from '@nestjs/common';

import { TASKS } from '../mocks/tasks.mock';

@Injectable()
export class TasksService {
  tasks = TASKS;
}
```

<p align="justify">Than we should add 2 methods, `getTasks` and `getTask`, to receive all tasks or certain one respectively.</p>

```tsx
...
tasks = TASKS;

getTasks() {
  return this.tasks;
}

getTask(id: number) {
  return this.tasks.find(task => task.id === id);
}
...
```

<p align="justify">`getTask` method accepts `id` as a parameter, of type `number`, which we will ensure via validation in next steps.</p>

<p align="justify">So it's time to add typical `mutation` method to create entity of `Task`, let's name it `addTask`:</p>

```tsx
async addTask(input: AddTaskInput): Promise<Task[]> {
    const lastTask = this.tasks.slice(-1).pop();
    const task: Task = {
      id: lastTask.id + 1,
      title: input.title,
      description: input.description,
      completed: false,
    };

    this.tasks.push(task);
    return this.tasks;
  }
```

<p align="justify">You can always take advantage of `async/await` syntax, for asynchronous operations, since `NestJs` supports latest `Typescript`.</p>

<p align="justify">In the same manner adding update/delete methods:</p>

```tsx
  deleteTask(id: string): Task[] {
    const taskIndex = this.tasks.findIndex(item => item.id === id);
    if (taskIndex === -1) {
      throw new HttpException('Task not found', 404);
    }

    this.tasks.splice(taskIndex, 1);
    return this.tasks;
  }
```

### Step 7 - Add a DTO

<p align="justify">DTO is a data transfer object, a TypeScript class created for type-checking and to define the structures of what an object looks like upon Task creation.</p>

```tsx
import { Field, InputType } from '@nestjs/graphql';
import { IsNotEmpty } from 'class-validator';

@InputType()
export class AddTaskInput {
  @Field()
  @IsNotEmpty()
  title: string;

  @Field()
  description: string;
}
```

<p align="justify">Since we will generate a new `ID` for task, we don't want to include it into</p>

### Step 8 - Injecting a Service into Resolver

<p align="justify">Since `NestJS` is build around `Dependency Inject` concept. we can benefit in performance by injecting instance of `TasksService` into `TasksResolver`. To achieve this we need add respective code to class constructor:</p>

```tsx
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { TasksService } from './tasks.service';
import { AddTaskInput } from './dto/add-task.input';
import { UpdateTaskInput } from './dto/update-task.input';
import { Task } from './models/tasks.model';

@Resolver('Tasks')
export class TasksResolver {
  constructor(
    private readonly taskService: TasksService
  ) {}

  @Query(type => [Task])
  async getTasks() {
    return this.taskService.getTasks();
  }

  @Query(type => Task)
  async getTask(
    @Args('id') id: string,
  ) {
    return this.taskService.getTask(id);
  }

  @Mutation(type => [Task])
  async addTask(
    @Args('input') input: AddTaskInput,
  ) {
    return this.taskService.addTask(input);
  }

  @Mutation(type => Task)
  async updateTask(
    @Args('input') input: UpdateTaskInput,
  ) {
    return this.taskService.updateTask(input);
  }

  @Mutation(type => [Task])
  async deleteTask(
    @Args('id') id: string,
  ) {
    return this.taskService.deleteTask(id);
  }
}
```

<p align="justify">Note that `Query` decorator should be imported from `@nestjs/graphql`.</p>

<p align="justify">Next, we injected `TaskService` via constructor and created set of `Queries/Mutations`</p>

- getTasks() - returns all tasks;
- getTask(id) - returns task by id;
- addTask(input) - creates task;
- updateTask(input) - updates task state;
- deleteTask(input) - delete task;

<p align="justify">For each `Query/Mutation` we set return type for GraphQL according to the data returned from service methods.</p>

### Step 9 - Running the App!

<p align="justify">When all setup finished, it's time to test out API. For testing we recommend using `GraphQL Playground`, which is available by default.</p>

<p align="justify">By default server starts on `3000` port. To open `playground` visit `http://localhost:3000/graphql`</p>

Get all tasks:
![Running-the-App---Get-all-tasks](https://2muchcoffee.com/blog/content/images/2020/09/Running-the-App---Get-all-tasks.png)

Add task:
![Running-the-App---Add-task-2](https://2muchcoffee.com/blog/content/images/2020/09/Running-the-App---Add-task-2.png)

Update task:
![Running-the-App---update-task](https://2muchcoffee.com/blog/content/images/2020/09/Running-the-App---update-task.png)


Like the article? <u><a href="https://2muchcoffee.com/blog/">Click here</a></u> for more information.