DEV Community

Cover image for How to use Prisma with express, postgress, and Graphql?
Next Dev
Next Dev

Posted on • Edited on

How to use Prisma with express, postgress, and Graphql?

Welcome friends to this new blog. In this blog, you will learn how to set up APIs with the help of an express server. You will learn how to integrate graphql in the express server.

GitHub link — https://github.com/nextdev1111/prisma-postgres-graphql-express

Youtube Video in Hindi

📒Notes

  1. I will be using **pnpm for this project but the tutorial will show you how to do it with **npm.

  2. We will be using **typescript **for this project.

Step1: 📦 Setup

Make a folder by whichever name you want but you need to consider npm naming restrictions.

Then run this command

npm init -y
Enter fullscreen mode Exit fullscreen mode

Make another folder named src. Make a file named **index.ts **in the src folder.

[root folder]

--- src
--- --- index.ts
Enter fullscreen mode Exit fullscreen mode

To install dependencies and setup typescript

This will install dependencies 👇

npm i @graphql-tools/schema @prisma/client dotenv express graphql express-graphql
Enter fullscreen mode Exit fullscreen mode

This will install dev dependencies 👇

npm i -D [@types/dotenv](http://twitter.com/types/dotenv) [@types/express](http://twitter.com/types/express) [@types/node](http://twitter.com/types/node) prisma ts-node typescript
Enter fullscreen mode Exit fullscreen mode

Then type 👇 to create a **tsconfig **file.

npm install -g typescript
tsc --init

Step2: 🗃️ Prisma Setup with postgress database

Type this command in your terminal

npm prisma init
Enter fullscreen mode Exit fullscreen mode

Prisma will set up two things in your root folder.

  1. Prisma folder

  2. .env file

In the prisma folder, you can find schema.prisma file.



Let’s add a database now.

go to https://railway.app/

Click on start new project

You can set up a PostgreSQL database. If you find any problem, you can also watch the youtube video.

Then, you should copy the postgresql database connection key

Then go to .env your project’s root folder.

Then change the connection string in your .env file.

→ Remember to also include the password in the connection string.

Then type

prisma db push
Enter fullscreen mode Exit fullscreen mode

⚠️Errors

If you face any errors, you should check the connection string.

Step3: ✨Express Server

This is the basic express server



If you want to add an entry to your database.

You can also use prisma studio

npm prisma studio
Enter fullscreen mode Exit fullscreen mode

Step4: 💪Grahpql Integration



You can use graphiql on this path

[http://localhost:3000/graphql](http://localhost:3000/graphql)
Enter fullscreen mode Exit fullscreen mode

If you want to ask any questions, feel free to ask 👇
Join the Next Dev Discord Server!

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.