DEV Community

Yuval Hazaz for Amplication

Posted on • Updated on

Build a Node.js graphQL API with NestJS and Prisma

Building an API requires spending too much time on boilerplate and repetitive coding. Defining the data model, connecting the database to the server, creating the API endpoints, add security and permissions layer, logging, validation, identity management, sorting, filtering, pagination... the list is long.

In this post, I will show you how to create all these using Amplication. We will generate a GraphQL API for an e-commerce application, built with Node.JS, NestJS, Prisma, PostgreSQL, and some additional great open-source technologies.

Amplication is an open-source developers' platform that generates an API and a client based on your data model. It saves hours and even days of boilerplate coding.

You can use the UI or a CLI to define the data model, and Amplication generates everything you need to start building your next app. The generated source code is fully readable and editable, written in TypeScript, and it even includes tests.

By the end of this tutorial, you will have the source code of the backend and client, and you will be able to start writing the custom business logic of your API, or creating the coolest client application or mobile app to work with the API.

The Generated Code

So, let's first have a high-level overview of the generated source code.

image

The generated source-code shown in this post is available in https://github.com/amplication/e-commerece-sample

We can see that Amplication generates two separate projects for us - "admin-ui", and "server".
The "server" folder contains the Node.JS code for our GraphQL API, and the "admin-ui" contains the React-Admin code for an admin application that connects to the API and provides CRUD operations for all our data models.

Let's Start

For this post, I will use the CLI to create our data model. You can also use Amplication's UI to do it.

To learn how to install Amplication CLI and authenticate with the server, see this doc https://docs.amplication.com/docs/cli

# Create app
amp apps:create "my-e-commerce-api" --set-current

# Create "Customer" entity
amp entities:create Customer --set-current
amp entities:fields:create "First Name"
amp entities:fields:create "Last Name"
amp entities:fields:create "Email"
amp entities:fields:create "Phone"
amp entities:fields:create "Comments"

# Create "Address" entity
amp entities:create Address --set-current
amp entities:fields:create "First Name"
amp entities:fields:create "Last Name"
amp entities:fields:create "Address 1"
amp entities:fields:create "Address 2"
amp entities:fields:create "City"
amp entities:fields:create "State"
amp entities:fields:create "Country"
amp entities:fields:create "Zip"
amp entities:fields:create "Phone"
amp entities:fields:create "Is Default"
amp entities:fields:create "Customer"

# Create "Product" entity
amp entities:create Product --set-current
amp entities:fields:create "Title"
amp entities:fields:create "Vendor"
amp entities:fields:create "Price"

# Create "Image" entity
amp entities:create Image --set-current
amp entities:fields:create "Src"
amp entities:fields:create "Width"
amp entities:fields:create "Height"
amp entities:fields:create "Product"

# Create "Order" entity
amp entities:create Order --set-current
amp entities:fields:create "Customer"
amp entities:fields:create "Address"
amp entities:fields:create "Comments"
amp entities:fields:create "Total Price"
amp entities:fields:create "User"

# Create "Line Item" entity
amp entities:create LineItem --set-current
amp entities:fields:create "Order"
amp entities:fields:create "Product"
amp entities:fields:create "Price"
amp entities:fields:create "Quantity"

# Commit the new entities and generate the code
amp apps:commit --message="create initial entities"
Enter fullscreen mode Exit fullscreen mode

After executing this script, we can use Amplication's UI to see that all our data models were created, and if needed we can also change any of the settings.

image

That's it... Our source code is ready for download.

We can simply click on "Download code" to get a ZIP file.

Here is an example of the generated files for our customer entity, including the Customer model, DTOs, GraphQL resolver, Service, and tests.

image

Here is an example of the generated code for our Customer service
image

What's Next

At this point, you can proceed in any of the following ways:

  1. Connect to a GitHub account to automatically create a Pull Request with your source code in a Github repository.
    https://docs.amplication.com/docs/sync-with-github

  2. Use the Sandbox environment provided by Amplication with a live instance of your application for further development and testing.

  3. Deploy your application in a Docker container to any server or online service.
    https://docs.amplication.com/docs/deploy

  4. Customize your server code
    https://docs.amplication.com/docs/how-to/custom-code

  5. Build a custom client application to work with your new server.

  6. Keep making changes in your data models and re-generate your application code. When connected to a GitHub account you will also get a new Pull Request with every change you make.

Try it now

Start using Amplication by visiting https://amplication.com/

Open-Source

Amplication is an open-source project.

Please leave a comment and tell me what you think about it. We are also open to feature requests and suggestions on our GitHub repo
https://github.com/amplication/amplication

If you need support, you can reach us on our Discord channel https://discord.gg/KSJCZ24vj2

Top comments (2)

Collapse
 
chakshugautam profile image
ChakshuGautam

Tried this today. Was able to set up the whole thing in less than 30 mins. The generated code is easy to understand and the deployment is a breeze. I am never going back to setting up any NodeJs boilerplate again. :D

Collapse
 
yuvalhazaz profile image
Yuval Hazaz

Thanks! I am very glad to hear that.