Skip to content

Pritam-25/graphql_express_setup

Repository files navigation

GraphQL Server (Node.js + TypeScript + Prisma)

A simple GraphQL backend setup with Node.js, TypeScript, Express, and Prisma. The project demonstrates a clean backend architecture using GraphQL with modular entities, repositories, and services.


Features

  • GraphQL API
  • TypeScript support
  • Prisma ORM for database access
  • Modular entity-based structure
  • Authentication with hashed passwords
  • Clean separation of concerns (resolvers, repositories, services)

Tech Stack

  • Node.js
  • TypeScript
  • Express
  • GraphQL
  • Prisma
  • PostgreSQL
  • bcrypt (for password hashing)

Project Structure

src
│
├─ entities
│   ├─ user
│   │   ├─ resolver.ts
│   │   ├─ type-defs.graphql
│   │   └─ index.ts
│
├─ graphql
│   ├─ schema.ts
│   └─ context.ts
│
├─ repositories
│   └─ user.repository.ts
│
│
├─ middleware
|   |─ auth.middleware.ts
│
├─ services
│   └─ user.service.ts
|
├─ repositories
│   └─ user.repository.ts
|
├─ types
│   ├─ express.d.ts
│   ├─ graphql-types.ts
│   └─ graphql.d.ts
|
├─ lib
│   └─ prisma.ts
|
├─ utils
│   ├─ password.ts
│   └─ jwt.ts
│
│─ index.ts
└─ app.ts

Installation

Clone the repository:

git clone git@github.com:Pritam-25/graphql_express_setup.git

Install dependencies:

pnpm install

Environment Variables

Create a .env file in the root directory.

Example:

PORT=4000
NODE_ENV=development
DATABASE_URL=your_postgres_database_url
JWT_SECRET=your_secret_key
CLIENT_ORIGIN=http://localhost:3000

Setup Steps

Generate Prisma client:

pnpm prisma generate

Run database migrations:

pnpm prisma migrate dev --name init_db_setup

Generate GraphQL resolver types:

pnpm codegen

Running the Server

Start the development server:

pnpm dev

GraphQL server will run at:

http://localhost:4000/graphql

Example Query

Operation

query {
  users {
    id
    email
    name
    role
  }
}

Response

{
  "data": {
    "users": [
      {
        "id": "3f167d7f-edfa-434d-9b5d-bd0105c3bf73",
        "email": "atanu@gmail.com",
        "name": "Atanu",
        "role": "USER"
      },
      {
        "id": "7d0a12c8-8be1-4cc8-a33b-dceaf4de48ed",
        "email": "pritam@example.com",
        "name": "Pritam Maity",
        "role": "ADMIN"
      }
    ]
  }
}

Example Mutation

1. SignUp Mutation

Operation

mutation SignupUser($input: SignupInput!) {
  signup(input: $input) {
    id
    name
    email
    role
  }
}

Variables

{
  "input": {
    "name": "Pritam Maity",
    "email": "pritam@example.com",
    "password": "StrongPass123",
    "role": "USER"
  }
}

Response

{
  "data": {
    "signup": {
      "id": "7d0a12c8-8be1-4cc8-a33b-dceaf4de48ed",
      "name": "Pritam Maity",
      "email": "pritam@example.com",
      "role": "USER"
    }
  }
}

2. Login Mutation

Operation

mutation LoginUser($input: LoginInput!) {
  login(input: $input) {
    id
    name
    email
    role
  }
}

Variables

{
  "input": {
    "email": "pritam@example.com",
    "password": "StrongPass123"
  }
}

Response

{
  "data": {
    "login": {
      "id": "641744fa-24df-4d13-abfa-b7e0bbce25c0",
      "name": "Pritam Maity",
      "email": "pritam@example.com",
      "role": "USER"
    }
  }
}

License

This project is for learning purpose.

About

A simple GraphQL backend setup with Node.js, TypeScript, Express, and Prisma.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors