Building a Serverless GraphQL API with Prisma, Apollo & TypeGrapQL-Prisma

March 14, 2023
11 min
Nuwan Perera

Building a Serverless GraphQL API with Prisma, Apollo& TypeGrapQL-Prisma

This blog will be a step-by-stepguide to teach you how to utilize Serverless Framework to build a simple GraphQL API.

Prisma,a next-generation ORM, will be used to interact with the database, and Apolloserver will be used to develop the GraphQL API.

In this article, we will examine each of the techstacks that we are using to create our serverless API. Afterwards, we willconfigure the foundational infrastructure that we will require moving forward.


·       Some knowledge of the fundamentals of GraphQL.

·       Some knowledge of the fundamentals of Prisma.

·       NodeJS (Preferably 16.x LTS) along with serverless CLI installed.

Why Apollo for GraphQL development?

·       Comprehensive Tooling: Apollo provides a suite of tools for every step of the GraphQL development process, including client and server libraries, development, and testing tools, and a robust GraphQL schema management system.

·       Large Community: Apollo has a large and active community of developers, which means there are many resources available for learning and troubleshooting, as well as many integrations and plugins available for extending the functionality of Apollo.

·       Performance: Apollo is optimized for performance, with features such as caching and batched requests that can improve the speed and efficiency of GraphQL APIs.

·       Open Source: Apollo is open source, meaning that it is freely available for use and can be customized and extended to meet specific needs.

Overall, Apollo provides a powerful and flexible platform for developing GraphQL APIs and applications, with a wide range of tools and a strong community to support developers.


Why Prisma?

Prisma is a popular open-source database toolkit and ORM (Object-Relational Mapping) that is commonly used in modern web development.

·       Modern and intuitive API: Prisma provides a modern and intuitive API that simplifies the database access layer of your application. It provides a type-safe and auto-generated client that can be used to interact with the database.

·       Database agnostic: Prisma is not tied to a specific database or technology, and supports many popular databases such as PostgreSQL, MySQL, MongoDB, and SQLite. It also allows you to switch databases easily without changing your application code.

·       Strong data modeling: Prisma comes with a powerful data modeling language that allows you to define your data models and relationships in a declarative way, making it easy to reason about your data structures.

·       Performance: Prisma is designed for performance, and it generates optimized SQL queries to minimize the number of database queries needed to fulfill a request. It also supports efficient data pagination, filtering, and sorting.

·       Community: Prisma has a growing community of developers who contribute to the project and provide support, making it easier to learn and troubleshoot issues.

Overall, Prisma is a powerful and flexible database toolkit that simplifies database access and provides a range of features for building scalable and performant applications.

Setting up the project

To initialize the project, we will use the serverless template called aws-nodejs-typescript. Our objective is to use Typescript in order to increase type-safety within the API as much as possible.

serverless create --template aws-nodejs-typescript--path serverless-api

Afterwards, you can navigate to the directory "serverless-api" using the command

cd serverless-api

and then open the directory in Visual Studio Code by using the command "code." This will open a new window in Visual Studio Code for the "serverless-api" directory.

To install the dependencies required for the serverless API boilerplate, you can run the command

npm install

.This will install all the dependencies listed in the package.json file in the current directory.

The boilerplate code structure is similar to what is seen below.

Boilerplate code structure


If you want to format your code and ensure it follows a consistent coding style, you can add Prettier and ESLint to your project. To do this, you can run the following commands:

1.   Install Prettier and ESLint as dev dependencies:

npm install --save-dev prettiereslint eslint-config-prettier eslint-plugin-prettier

2.   Create an ESLint configuration file:

npx eslint –init


3.   Create a Prettier configuration file in the root of yourproject:

touch .prettierrc


4.   Add your desired Prettier settings to the.prettierrc file.


5.   Add a "prettier" property to your ESLintconfiguration file, with the path to your .prettierrc file:



6.   Optionally, you can add a "lint" script to yourpackage.json file to run ESLint:

"scripts": {
"lint": "eslint ."

To emulate the Lambda API locally, you can add the"serverless-offline" plugin to your serverless project. To do this, you can run the following command:

npm install serverless-offline --save-dev

This will install the "serverless-offline" plugin as a development dependency in your project. After installing the plugin, you can add it to your serverless.ts file by adding the following lines under the "plugins" section:


This will enable you to test and debug your serverless API locally before deploying it to AWS Lambda. To start the local server, you can run the command "serverless offline" in your terminal.

Now that we've completed the basic setup of our serverlessAPI, we can run it locally using the "serverless offline" command. To start the local server, navigate to the root directory of your project and run the following command:


This will start the local server and provide you with a URL that you can use to access your API from your local machine as below.

URL that you can use to access your APIURL that you can use to access your API

You can use tools such as Postman or cURL to send requests to your API and test its functionality.

Testing with Postman

More news