A GraphQL server for client applications to access data from services. This server integrates with the users-service REST API to provide GraphQL access to user management functionality.
- Node.js (v22.16.0 or higher)
- npm (v10.0.0 or higher)
- Clone the repository
- Use the correct Node.js version (if using nvm):
nvm use
- Set up environment variables:
cp .env.example .env # Edit .env with your configuration - Install dependencies:
npm install
Start the development server:
npm run devThe GraphQL server will be available at http://localhost:4000/graphql
💡 Apollo Studio Sandbox: Visit
http://localhost:4000/graphqlin your browser for the interactive GraphQL playground.
npm run dev- Start development server with hot reloadnpm run build- Build the project for productionnpm start- Start the production servernpm test- Run tests in watch modenpm run test:run- Run tests oncenpm run test:ui- Run tests with UI interfacenpm run commitlint- Check the last commit message formatnpm run release- Generate changelog and create a new releasenpm run release:patch- Create a patch release (1.0.0 → 1.0.1)npm run release:minor- Create a minor release (1.0.0 → 1.1.0)npm run release:major- Create a major release (1.0.0 → 2.0.0)
- Apollo Server 4: Standalone GraphQL server with built-in Apollo Studio sandbox
- TypeScript: Full type safety and modern JavaScript features
- GraphQL: Schema-first API design with introspection support
- Node.js: Runtime environment (v22.16.0+)
@apollo/server: GraphQL server implementationgraphql: GraphQL query language and runtimeaxios: HTTP client for REST API integrationjsonwebtoken: JWT token handling for authenticationdotenv: Environment variable managementgraphql-query-complexity: Query complexity analysis for security
- Vitest: Fast unit testing framework with coverage
- Husky: Git hooks for code quality
- Commitlint: Conventional commit message validation
- Standard Version: Automated changelog and versioning
- Rate Limiting: Request-based rate limiting (100 requests per 15 minutes)
- Query Complexity Analysis: Prevents expensive GraphQL queries (max complexity: 1000)
- JWT Authentication: Secure token-based authentication
- Role-Based Access Control: Admin and user role separation
- CORS Configuration: Cross-origin request handling
- Heroku: Cloud platform with automatic deployment
- GitHub Integration: Continuous deployment from master branch
- TypeScript Build: Automatic compilation on deployment
Once the server is running, you can access the interactive Apollo Studio sandbox at:
http://localhost:4000/graphql
The sandbox provides:
- Interactive query editor with syntax highlighting
- Schema exploration and documentation
- Query history and saved operations
- Real-time query execution and results
The server provides a comprehensive GraphQL API that integrates with the users-service. Here are some example queries:
query {
health {
status
timestamp
service
}
}# Register a new user
mutation {
register(input: {
email: "user@example.com"
password: "securePassword123"
firstName: "John"
lastName: "Doe"
}) {
token
user {
id
email
firstName
lastName
role
}
}
}
# Login
mutation {
login(input: {
email: "user@example.com"
password: "securePassword123"
}) {
token
user {
id
email
role
}
}
}# Get current user
query {
me {
id
email
firstName
lastName
role
}
}
# Get all users
query {
users {
id
email
firstName
lastName
role
}
}Note: For authenticated queries, include the JWT token in the Authorization header:
Authorization: Bearer <your-jwt-token>
The API implements rate limiting to prevent abuse and ensure fair usage:
- Limit: 100 requests per 15-minute window per client
- Identification: Based on IP address for anonymous users, user ID for authenticated users
- Headers: Rate limit information is included in response headers:
X-RateLimit-Limit: Maximum requests allowedX-RateLimit-Remaining: Requests remaining in current windowX-RateLimit-Reset: Unix timestamp when the rate limit resets
Rate Limit Exceeded Response:
{
"errors": [{
"message": "Rate limit exceeded. Try again in 60 seconds.",
"extensions": {
"code": "RATE_LIMIT_EXCEEDED",
"rateLimitReset": 1234567890,
"rateLimitRemaining": 0,
"rateLimitTotal": 100
}
}]
}See examples/queries.graphql for more comprehensive examples.
This GraphQL server acts as a gateway to the users-service REST API.
Set the users-service URL in your .env file:
USERS_SERVICE_URL=http://localhost:3000- Authentication: Register and login users via GraphQL mutations
- User Management: CRUD operations for users with proper authorization
- Role-Based Access: Admin-only operations for user promotion/demotion
- Real-time Data: Direct integration with users-service ensures data consistency
- Error Handling: Comprehensive error handling with meaningful GraphQL error codes
- Register/Login: Use public mutations to get a JWT token
- Include Token: Add
Authorization: Bearer <token>header to requests - Access Protected Data: Query user data and perform operations based on your role
Make sure the users-service is running before starting this GraphQL server:
# In your users-service directory
npm run devThe users-service should be available at http://localhost:3000 (or your configured URL).
This project uses Vitest for comprehensive testing with full TypeScript support. Tests are located in the src/__tests__ directory.
# Run tests in watch mode
npm test
# Run tests once
npm run test:run
# Run tests with coverage report
npm run test:coverage
# Run tests with UI
npm run test:ui-
Unit Tests:
basic.test.ts- Basic functionality testscontext.test.ts- JWT context and authentication testsusersService.test.ts- Users service client testsresolvers.test.ts- GraphQL resolver testsintegration.test.ts- Full GraphQL schema integration tests
-
Test Coverage: Comprehensive coverage including:
- Authentication and authorization flows
- GraphQL schema validation
- Error handling scenarios
- JWT token processing
- Users service integration
- Admin role verification
- Mocking: Uses Vitest's built-in mocking for:
- External HTTP requests (axios)
- JWT token decoding
- Users service methods
- Type Safety: Full TypeScript support in tests
- Coverage Reports: HTML and JSON coverage reports
- Integration Testing: Tests actual GraphQL operations
The test suite provides excellent coverage of core functionality:
- Context and authentication: 100% coverage
- GraphQL schema: 100% coverage
- Resolvers and services: 50%+ coverage
- Overall project coverage: ~57%
Tests are automatically run before each commit via Husky hooks to ensure code quality and prevent regressions.
This project uses standard-version to automatically generate changelogs and manage releases based on conventional commits.
# Automatically determine the next version based on commits
npm run release
# Force a specific release type
npm run release:patch # 1.0.0 → 1.0.1
npm run release:minor # 1.0.0 → 1.1.0
npm run release:major # 1.0.0 → 2.0.0- Analyzes commits since the last release
- Determines the next version number
- Updates
package.jsonversion - Generates/updates
CHANGELOG.md - Creates a git tag
- Commits the changes
The changelog is automatically generated from conventional commit messages. See CHANGELOG.md for the full project history.
This project uses Conventional Commits to ensure consistent commit message formatting. Commit messages are automatically validated using commitlint.
<type>: <description>
[optional body]
[optional footer(s)]
feat: A new featurefix: A bug fixdocs: Documentation only changesstyle: Changes that do not affect the meaning of the coderefactor: A code change that neither fixes a bug nor adds a featureperf: A code change that improves performancetest: Adding missing tests or correcting existing testschore: Changes to the build process or auxiliary toolsci: Changes to CI configuration files and scriptsbuild: Changes that affect the build system or external dependenciesrevert: Reverts a previous commit
feat: add user authentication
fix: resolve GraphQL query timeout issue
docs: update API documentation
chore: update dependenciesThis application is configured for easy deployment to Heroku.
This application is configured for automatic deployment from GitHub to Heroku. Any changes pushed to the main branch will automatically trigger a new deployment.
The app is already configured with:
- GitHub Integration: Automatic deployments from the
mainbranch - Environment Variables: Pre-configured with the correct users-service URL
- Build Process: Automatic TypeScript compilation via
heroku-postbuild
If you need to deploy manually:
-
Set environment variables (if not already set):
heroku config:set USERS_SERVICE_URL=https://wingedearth-users-service-174104f65795.herokuapp.com heroku config:set NODE_ENV=production
-
Deploy via Git (if GitHub integration is disabled):
# Add Heroku remote if not already added heroku git:remote -a wingedearth-united-api # Deploy to Heroku git push heroku main
Set these environment variables in your Heroku app:
USERS_SERVICE_URL: URL of your deployed users-serviceNODE_ENV: Set toproductionPORT: Automatically set by Heroku
The app includes:
Procfile: Defines the web processapp.json: Heroku app configuration.slugignore: Excludes unnecessary files from deploymentheroku-postbuild: Automatically builds TypeScript on deployment
You can also deploy directly from GitHub using the Deploy to Heroku button:
- Continuous Integration: Every push to
mastertriggers automatic deployment - Zero Downtime: Heroku handles rolling deployments
- Build Validation: Failed builds prevent deployment
- Rollback Support: Easy rollback to previous versions if needed
- Apollo Studio Sandbox: Interactive GraphQL playground available in production
- Environment Variables: All required variables are pre-configured
- Users Service Integration: Connected to deployed users-service API
- Introspection Enabled: Full GraphQL schema introspection for development
- CSRF Protection Disabled: Easier access for development and testing
- Standalone Server: Clean, lightweight Apollo Server setup
- TypeScript Build: Automatic TypeScript compilation on deployment
After deployment, test your GraphQL endpoint:
curl -X POST https://wingedearth-united-api-76c9d860a852.herokuapp.com/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ health { status timestamp service } }"}'Live Apollo Studio Sandbox: https://wingedearth-united-api-76c9d860a852.herokuapp.com/graphql
💡 Tip: Visit the GraphQL endpoint URL in your browser to access the interactive Apollo Studio sandbox for testing queries and exploring the schema.