Overview
This demonstrates how REST and GraphQL coexist in the same application using Hexagonal Architecture. Both protocols share the same domain logic and services.
File Structure
Example 1: Create User
REST (POST /api/v1/auth/register)
Request:GraphQL (POST /graphql)
Request:displayName field that REST doesn’t have!
Example 2: Get User
REST (GET /api/v1/users/:id)
Request:GraphQL (POST /graphql)
Request:Example 3: List Users
REST (GET /api/v1/users?limit=10&offset=0)
Request:GraphQL (POST /graphql)
Request:GraphQL-Specific Features
1. Flexible Field Selection
Only get what you need:2. Computed Fields
GraphQL has fields that REST doesn’t:3. Multiple Queries in One Request
Code Comparison
REST Handler
GraphQL Resolver
userService.Get() - the SAME service!
Benefits of This Approach
1. Code Reuse
- ✅ Domain logic is written ONCE
- ✅ Both REST and GraphQL use the same services
- ✅ No duplication
2. Flexibility
- ✅ Different representations for different clients
- ✅ GraphQL can have computed fields
- ✅ REST can be simpler for mobile apps
3. Gradual Migration
- ✅ Start with REST
- ✅ Add GraphQL for web clients
- ✅ Keep both running simultaneously
4. Protocol-Specific Features
- ✅ GraphQL: Flexible queries, computed fields
- ✅ REST: Simple, cacheable, mobile-friendly
Testing
Start the Server
Access GraphQL Playground
Try These Queries
1. Create a user:Summary
Key Takeaways:- ✅ Both REST and GraphQL coexist using the same domain services
- ✅ No code duplication - domain logic is shared
- ✅ Hexagonal Architecture makes this trivial
- ✅ GraphQL can have extra features (computed fields) without affecting REST
- ✅ Easy to add more protocols (gRPC, WebSocket) in the future