A production-style GraphQL API built using Node.js, Apollo Server, and Express.
This project demonstrates real-world GraphQL concepts including authentication, authorization, pagination, filtering, DataLoader optimization, and performance best practices.
✅ GraphQL Schema Design
✅ Interfaces & Unions
✅ Custom Scalars
✅ Filtering & Sorting
✅ Offset Pagination
✅ Cursor Pagination
✅ JWT Authentication
✅ Role-Based Authorization
✅ DataLoader (N+1 Problem Fix)
✅ Query Depth Limiting
✅ Modular Folder Structure
✅ Clean Service-Based Architecture
- Node.js
- Express.js
- Apollo Server
- GraphQL
- JSON Server (Mock Database)
- JWT (Authentication)
- bcryptjs (Password Hashing)
- DataLoader
- graphql-depth-limit
src/
│
├── schema/
│ ├── typedefs/
│ │ ├── user.schema.js
│ │ ├── article.schema.js
│ │ ├── blog.schema.js
│ │
│ ├── resolvers/
│ │ ├── user.resolver.js
│ │ ├── article.resolver.js
│ │ ├── blog.resolver.js
│
├── loaders/
│ ├── user.loader.js
│ ├── blog.loader.js
│
├── services/
│ ├── user.service.js
│ ├── article.service.js
│
├── middleware/
│ ├── auth.middleware.js
│
├── utils/
│ ├── auth.js
│
├── config/
│ ├── db.js
│
├── server.jsClone the repository:
git clone https://github.com/your-username/graphql-blog-api.gitMove into project:
cd graphql-blog-apiInstall dependencies:
npm installStart JSON server (mock database):
npm run json-serverStart GraphQL server:
npm run devGraphQL Endpoint:
http://localhost:4000/graphql
Authentication is handled using JWT tokens.
mutation {
login(
input: {
email: "ali.khan@example.com"
password: "password123"
}
)
}Response:
{
"token": "JWT_TOKEN_HERE"
}Use token in headers:
{
"Authorization": "Bearer YOUR_TOKEN"
}Supported roles:
- ADMIN
- USER
Example:
mutation {
deleteArticle(id: "1")
}Allowed only for:
ADMIN role
This project uses DataLoader to fix:
N+1 Query Problem
Benefits:
- Batch requests
- Cache results
- Improve performance
Example:
context.loaders.userLoader.load(userId);Supports:
query {
articles(page: 1, limit: 5) {
id
title
}
}query {
articles(first: 5, after: "cursor-id") {
edges {
node {
id
title
}
}
}
}query {
articles(filter: {
published: true
rating_gte: 4
}) {
id
title
}
}Used:
- DateTime
- JSON
Example:
createdAt: DateTime!Implemented:
✅ DataLoader
✅ Depth Limiting
validationRules: [
depthLimit(5)
]Prevents:
Deep nested query attacks
query {
articles {
id
title
user {
id
name
}
comments {
id
text
}
}
}Create .env file:
PORT=4000
JWT_SECRET=my_secret_key- Modular architecture
- Service-based logic
- Middleware authentication
- Context-based loaders
- Error-safe resolvers
- Token verification
- Role-based access control
This project covers:
- GraphQL Core Concepts
- API Performance Optimization
- Authentication Systems
- Authorization Systems
- Scalable Backend Architecture
- Production-Level API Design
StackWise Dev GraphQL Backend Simple Learning Path.
This project is open-source and available for learning purposes.