This repository implements a scalable, modular, and robust backend architecture using:
- GraphQL (Apollo Server) for the API gateway.
- Express.js for middleware and server configuration.
- TypeScript for type safety and maintainability.
- A service-oriented, reusable structure to ensure scalability.
src/
├── modules/ # Feature-based modular structure
│ ├── user/ # Example module
│ │ ├── resolvers/ # GraphQL resolvers
│ │ ├── services/ # Business logic
│ │ ├── models/ # Data models (TypeScript interfaces/classes)
│ │ ├── loaders/ # DataLoaders (batching & caching)
│ │ ├── schema.ts # GraphQL schema (type definitions)
│ │ └── index.ts # Module entry point
│ └── product/ # Another feature module
├── utils/ # Shared utility functions (e.g., error handling, logging)
├── config/ # Configuration files (e.g., environment variables)
├── graphql/ # Apollo Server setup
│ ├── context.ts # Context generation (auth, loaders, etc.)
│ ├── schema.ts # Root schema stitching
│ └── server.ts # Apollo Server instance
├── middlewares/ # Express middlewares (e.g., auth, rate limiting)
├── types/ # Global TypeScript types
├── services/ # Reusable services (e.g., email, notifications)
├── database/ # Database connection & ORM models
├── tests/ # Unit and integration tests
└── app.ts # Express server entry point
-
GraphQL Gateway:
- Modular schemas and resolvers for scalability.
- Schema stitching for combining multiple modules.
-
Express.js Middleware:
- Secure with Helmet and rate limiting.
- Logging with Morgan.
-
TypeScript Integration:
- End-to-end type safety.
- Easy-to-maintain interfaces and models.
-
Efficient Data Fetching:
- Batch and cache database queries with DataLoader.
-
Service-Oriented Architecture:
- Reusable, stateless services for business logic.
-
Error Handling:
- Centralized error handling with GraphQL extensions.
- Node.js >= 18.x
- npm or yarn
- Docker (optional for containerization)
-
Clone the repository:
git clone https://github.com/arpitv970/graphql-prod-setup cd graphql-prod-setup
<-- OR --> Clean duplicate repository:
git clone --depth=1 https://github.com/arpitv970/graphql-prod-setup
-
Install dependencies:
npm i
-
Configure environment variables:
- Create a
.env
file in the root directory, by simply coping.env.example
. - Add necessary variables like database connection strings, secret keys, etc.
- Create a
-
Start the development server:
npm run dev
- Start development server:
npm run dev
- Build for production:
npm run build
- Run tests:
npm run test
Here's an example query to test your GraphQL API:
query {
getUserById(id: "123") {
id
name
email
}
}
- Apollo Server - GraphQL API Gateway
- Express.js - Web framework for Node.js
- TypeScript - Type-safe JavaScript
- DataLoader - Batching and caching for efficient database queries (Not Integrated Yet)
- Prisma - ORM for database operations (optional)
Contributions are welcome! Please fork the repository and submit a pull request with your changes.
For questions or support, feel free to reach out at [email protected]
.