This is a real-time chat application backend built using GraphQL, Apollo Server, Express, PostgreSQL, Prisma ORM, and WebSockets for real-time messaging.
/graphql-chat-backend
│── src/
│ ├── schema/
│ │ ├── message.js # GraphQL Type Definitions
│ ├── resolvers/
│ │ ├── message.js # Message Resolvers
│ │ ├── auth.js # Authentication Logic
│ ├── middleware/
│ │ ├── authMiddleware.js # JWT Authentication Middleware
│ ├── utils/
│ │ ├── jwt.js # JWT Helper Functions
│ ├── prisma/
│ │ ├── schema.prisma # Prisma Database Schema
│ ├── index.js # Server Entry Point
│── .env # Environment Variables
│── package.json # Dependencies
│── prisma/migrations/ # Prisma Migrations
git clone https://github.com/your-repo/graphql-chat-backend.git
cd graphql-chat-backendnpm installCreate a .env file in the root directory:
DATABASE_URL=postgresql://user:password@localhost:5432/chatdb
JWT_SECRET=your_secure_jwt_secret
PORT=4000
Make sure you have PostgreSQL installed and running. Then, run:
npx prisma migrate dev --name init
npx prisma generateThis initializes the database with required tables.
npm startYour GraphQL API is now running at:
🌍 http://localhost:4000/graphql
⚡ WebSockets active at ws://localhost:4000/graphql
- User Authentication (JWT)
- Real-time Messaging with WebSockets
- GraphQL Queries, Mutations, and Subscriptions
- Prisma ORM for Database Management
- Express Middleware for Authentication
- PostgreSQL Database Integration
type Message {
id: ID!
senderId: String!
content: String!
timestamp: String!
}
type User {
id: ID!
username: String!
email: String!
password: String!
}
type Query {
messages: [Message]
}
type Mutation {
createUser(username: String!, email: String!, password: String!): User!
sendMessage(senderId: String!, content: String!): Message
}
type Subscription {
messageSent: Message
}The database structure using Prisma ORM:
model User {
id String @id @default(uuid())
username String @unique
email String @unique
password String
messages Message[]
}
model Message {
id String @id @default(uuid())
sender User @relation(fields: [senderId], references: [id])
senderId String
content String
timestamp DateTime @default(now())
}mutation {
createUser(username: "Alice", email: "alice@email.com", password: "securepass") {
id
username
email
}
}mutation {
login(email: "alice@email.com", password: "securepass") {
token
}
}mutation {
sendMessage(senderId: "user-id", content: "Hello, GraphQL!") {
id
content
timestamp
}
}query {
messages {
id
senderId
content
timestamp
}
}subscription {
messageSent {
id
senderId
content
timestamp
}
}- Node.js + Express (Backend)
- GraphQL + Apollo Server (API)
- PostgreSQL (Database)
- Prisma ORM (Database Management)
- JWT Authentication (Security)
- WebSockets (GraphQL Subscriptions) (Real-Time Communication)
- Docker (Optional, for database setup)
- ✅ Complete Authentication (✔️ Done!)
- ✅ Setup WebSocket for Real-Time Messages (✔️ Done!)
- 🔜 Build Flutter Frontend
- 🔜 Improve Message Filtering & Pagination
- 🔜 Deploy to Production (Docker, AWS, or Heroku)
This project is open-source and free to use under the MIT License.
- Jude - Backend Developer
For issues, feel free to open an issue or contribute! 🚀