Introduction to Building API's with NestJS and Nrwl Nx
Tags: api, nest, nx, node
Published: 2020-08-05 14:34:12

---
title: "Introduction to Building API's with NestJS and Nrwl Nx"
series: "Building API's with NestJS and Nrwl Nx"
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/3lapwr5f7xlnzlqfs7vv.png
canonical_url: https://beeman.dev/posts/introduction-to-building-api-s-with-nestjs-and-nrwl-nx-1l2b/
published: true
tags: api, nest, nx, node
---

## Introduction

In this tutorial, you learn how to build API's with NestJS and Nx Workspace. The goal is to give you a nice starting point that's easy to extend. Additionally, it will serve as a reference for later posts and series that I intend to write.

This post serves as an introduction to the technology stack and goes over the tutorials in the series.

## NestJS

![NestJS logo](https://dev-to-uploads.s3.amazonaws.com/i/b2f1x6outcd3fzswwbrb.png)

[NestJS](https://nestjs.com/) is a framework for building API's using Node.js and TypeScript. NestJS provides an opinionated API on top of Express (or optionally Fastify) and makes heavy use of TypeScript classes and decorators, enabling a declarative way to quickly add new functionality without writing a lot of code.

NestJS has great [documentation](https://docs.nestjs.com/) and is actively maintained. There are a lot of useful libraries provided by the project itself, and there is a growing community that provides even more functionality. Besides that, NestJS is easy to extend if the functionality you need does not exist.

### NestJS concepts

Here are a few NestJS concepts you should be aware of when going through this series.

#### Modules

The architecture from NestJS is inspired by Angular. It uses _modules_ to encapsulate functionality in standalone, reusable blocks that can be imported by other modules. Modules tie the other components together, and make sure _dependency injection_ works.

#### Controllers

The _Controllers_ are used to build a RESTful API. They allow building any HTTP api that you can dream of, and does so in a clean and declarative matter by adding methods to TypeScript classes, and decorate them. These _Controllers_ define the external REST API, most of the heavy lifting is done through _Providers_.

#### Providers

The _Providers_ are injected into the _Controllers_, _Resolvers_ or other _Providers_, and handle their requests. Things like accessing a database or calling into third-party API's or libraries are all done in _Providers_. This keeps the _Controllers_ clean and focussed, and makes it easy to share the functionality with other parts of the application.

#### Resolvers

The _Resolvers_ are used to build a GraphQL API. They allow building a GraphQL API using a code-first approach. That means that you don't need use GraphQL's Schema Definition Language (SDL) to define the API, but rather TypeScript code. Like the controllers, the _Resolvers_ define the external API, and use _Providers_ to do the heavy lifting.

## Nx

![Nx Logo](https://dev-to-uploads.s3.amazonaws.com/i/jmsyzyk6pdkjf7bflwu2.png)

[Nx](https://nx.dev) combines a mono-repository structure with CLI tools to develop and manage one or more applications and libraries, all within the same repository. Nx encourages code collaboration, unified testing, and optimizes builds and testing performances.

Nx is an amazing tool for building medium or large sized applications. Using a mono-repo allows you to easily share code between various parts of the project, without having to worry about the these parts being out of sync. Nx is smart in the sense that it knows which parts of the app relate to each other. Read more about the _Dependency Graph_ below.

### Nx concepts

Here are a few Nx concepts you should be aware of when going through this series.

#### Workspace

The _workspace_ is defined on the root of the repository, and consists of one or more projects, which can be either an _application_ or a _library_. The workspace uses either the Nx CLI or Angular CLI to serve, test, build and lint the projects. The workspace is defined in the files `nx.json` and `workspace.json` (or `angular.json` if using the Angular CLI).

#### Applications

In Nx, the _applications_ (or apps), are the projects that contain a runnable application. Applications should be light-weight, and expose the functionality defined in the _libraries_.

#### Libraries

The _libraries_ (or libs), are the projects that implement the functionality for the apps. The libraries define an external API that can be consumed by the apps and other libs.

#### Plugins

The actual functionality of the Nx workspace is defined by the plugins that are installed. Nx comes with quite a few official plugins that add support for, for example, Angular, Node, Next, React and Web Components. Additionally, there is a growing list of community plugins.

#### Dependency Graph

The _Dependency Graph_, or _dep graph_, is an automatically generated schema that shows how different parts of the monorepo depend on each other. Nx uses this information to intelligently execute commands on parts of the workspace. For example, it will only re-build projects that are actually changed, or when one of the dependencies are change.

## The project structure

In this series though, we use a simple structure. We build an app called `api` that consumes the libraries `core` and `course`, like this:

```text
apps/api
libs/core
libs/course
libs/<library> (any other functionality goes here).
```

## Tutorial in this series

This series consists of four tutorials that, when finished, result in a project that is solid basis that's easy to extend.

### [Set up and configure a new Nx Workspace](https://dev.to/beeman/set-up-and-configure-a-new-nx-workspace-1oh6)

To start the project, we create an empty Nx Workspace and take a look around to see what it consists of. We install and configure some tools to automatically format the code on each commit. Having this in place makes sure all future code has a similar style.

### [Add a NestJS API to a Nx Workspace](https://dev.to/beeman/add-a-nestjs-api-to-a-nx-workspace-24f)

After creating the project, we use the official NestJS plugin for Nx to create an application called _api_ with a library called _core_. The _core_ library is responsible for exporting the runtime configuration, and is consumed by the _api_.

### [Add GraphQL to a NestJS API in a Nx Workspace](https://dev.to/beeman/add-graphql-to-a-nestjs-api-in-a-nx-workspace-2p8n)

When these parts are in place, we move on to adding actual functionality to the _api_. After installing the needed dependencies and configure the GraphQL Module for NestJS, we implement a GraphQL _resolver_ and add a simple query that returns the servers' uptime.

### [Deploy a NestJS API to Heroku from a Nx Workspace](https://dev.to/beeman/deploy-a-nestjs-api-to-heroku-from-a-nx-workspace-o85)

Once this is all done, the API is ready to ship! After building the project locally and making sure it all works as expected, we add the configuration to deploy it to Heroku.

## Thanks!

First of all, thanks to the reviewers of this series 🙏:

- [Bilal Haidar](https://dev.to/bhaidar)
- [James Allen](https://dev.to/TheSpiciestDev)
- [Ryan Hutchison](https://dev.to/rhutchison)
- [Thomas Burleson](https://twitter.com/ThomasBurleson)

The cover photo for this series is an edited version of [this photo](https://unsplash.com/photos/onnJOfF-okU) by [Todd Kent](https://unsplash.com/@churchoftodd) on unsplash.

Thank YOU for reading my article, I hope it was useful. Feel free to reach out and follow me on [Twitter](https://twitter.com/beeman_nl/) or leave a comment on DEV! 🐝