A GraphQL API service built with Apollo Server, TypeScript, and PostgreSQL. This API provides endpoints for managing courses, collections, and user authentication.
You can view a live demo of the API by using the Apollo explorer sandbox and changing the endpoint to point at my VPC at https://damon314159.co.uk
You can add your Bearer tokens for auth purposes in the headers panel under the query editor after making a register or login request. Ensure you match the Authorization: Bearer <token> format.
- Install dependencies:
npm install
- Set up environment variables:
- Copy
.env.templateto.env - Fill in the required variables:
NODE_ENV: development/productionPORT: API server portDATABASE_URL: PostgreSQL connection stringJWT_SECRET: Secret key for JWT token generation
- Set up the database:
npm run db:migrate # Apply migrations npm run db:seed # Seed the database (optional)
- Start the development server
npm run build && npm run dev
The API will be available at http://localhost:7007 (or your configured port).
The seeded database contains two example users to get started with:
const user1 = {
username: "admin",
password: await hashPassword("admin123"),
role: "ADMIN",
};
const user2 = {
username: "user",
password: await hashPassword("user123"),
role: "USER",
};The API uses a modular GraphQL schema approach with separate schema files for different entities:
user.graphql- User authentication and management.course.graphql- Course management operations.collection.graphql- Collection-related types and operations.
- Type-safe GraphQL schema with TypeScript.
- JWT-based authentication.
- Role based access control. You must register/login as a user, and provide your JWT as a Bearer token to perform mutations. Additionally, you must have an ADMIN role to delete courses.
- Modular resolver organization.
- Postgres Database access via Drizzle ORM.
- Repository/Service/Resolver pattern with strong interfaces and DI.
- Comprehensive unit tests for Repository and Service layers with centrally managed mocks.
If there had been more time to work on this project, I would have liked to implement:
-
Auto Generated API Documentation
- Add a GraphQL schema documentation generator for convenient printouts.
- Add API usage examples to go along with the above.
-
Extra Features
- Implement pagination for courses and collections.
- Add filtering capabilities based on fields besides ID.
- Add relations between Users and other entities so that Users can own courses or collections.
-
Technical Improvements
- Add more comprehensive test coverage, including integration and e2e testing.
- Implement caching/batching layer for performance concerns.
- Set up CI/CD pipeline
- Add Docker support to speed up and automate deployment
-
Security
- Add input validation with a library like Zod to ensure fields are provided as expected.
- Implement request throttling
- Add security headers, particularly when interfacing with browser clients.
- Set up audit logging and monitoring with a service like Sentry or Cloudflare