Overview

This demonstrates how REST and GraphQL coexist in the same application using Hexagonal Architecture. Both protocols share the same domain logic and services.
Layered Architecture & Request Flow Layered Architecture & Request Flow

File Structure


Example 1: Create User

REST (POST /api/v1/auth/register)

Request:
Response:

GraphQL (POST /graphql)

Request:
Response:
Notice: GraphQL has a displayName field that REST doesn’t have!

Example 2: Get User

REST (GET /api/v1/users/:id)

Request:
Response:

GraphQL (POST /graphql)

Request:
Response:

Example 3: List Users

REST (GET /api/v1/users?limit=10&offset=0)

Request:
Response:

GraphQL (POST /graphql)

Request:
Response:

GraphQL-Specific Features

1. Flexible Field Selection

Only get what you need:
Response:

2. Computed Fields

GraphQL has fields that REST doesn’t:

3. Multiple Queries in One Request


Code Comparison

REST Handler

GraphQL Resolver

Notice: Both call 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:
2. Get user:
3. List users:

Summary

Key Takeaways:
  1. ✅ Both REST and GraphQL coexist using the same domain services
  2. ✅ No code duplication - domain logic is shared
  3. ✅ Hexagonal Architecture makes this trivial
  4. ✅ GraphQL can have extra features (computed fields) without affecting REST
  5. ✅ Easy to add more protocols (gRPC, WebSocket) in the future
This is the power of Hexagonal Architecture!