A Node.js backend API that exposes both a REST and a GraphQL interface for managing Users and Posts. Built with Express, Mongoose, and TypeScript, following a modular architecture with one file per use case.
- Node.js + Express + TypeScript
- MongoDB + Mongoose — data storage
- GraphQL via
express-graphql+@graphql-tools/schema - Jest + ts-jest — unit and E2E testing
- Supertest — HTTP testing
- mongodb-memory-server — in-memory MongoDB for fast tests
- Docker Compose — real MongoDB for E2E tests
- ESLint (airbnb-base) — code linting
- Node.js >= 18.0.0
- Docker (for E2E tests against a real MongoDB instance)
- Clone the repository:
git clone https://github.com/luizcurti/node-rest-graphql-api.git
cd node-rest-graphql-api- Install dependencies:
npm install- Start the development server:
npm run devThe server starts at http://localhost:4003.
| Method | Route | Description |
|---|---|---|
POST |
/users |
Create a user |
GET |
/users |
List all users |
GET |
/users/:id |
Get user by ID |
PUT |
/users/:id |
Update a user |
DELETE |
/users/:id |
Delete a user |
| Method | Route | Description |
|---|---|---|
POST |
/posts |
Create a post |
GET |
/posts |
List all posts |
GET |
/posts/:id |
Get post by ID |
GET |
/posts/user/:id |
Get all posts by a user |
PUT |
/posts/:id |
Update a post |
DELETE |
/posts/:id |
Delete a post |
The GraphQL endpoint is available at http://localhost:4003/graphql.
getAllUsers— list all usersgetUserById(id)— get user by IDgetAllPosts— list all postsgetPostById(id)— get post by IDgetPostsByUser(idUser)— get all posts by a user
createUser(input)— create a userupdateUser(id, input)— update a userdeleteUser(id)— delete a usercreatePost(input)— create a postupdatePost(id, input)— update a postdeletePost(id)— delete a post
Two Postman collections are available at the root of the project. Import either into Postman to get pre-configured requests:
REST API.postman_collection.json— all 11 REST endpointsgraphql_api.postman_collection.json— all GraphQL queries and mutations
npm testRuns 26 test suites, 162 tests.
npm run test:e2eSpins up the MongoDB Docker container defined in docker-compose.yml, runs 4 test suites (96 tests) against a real database, then tears down the test database.
npm run eslintnpm run build # compiles TypeScript to dist/
npm start # runs the compiled output