Skip to main content

Rust GraphQL Federated Gateway

A production-ready GraphQL federation gateway using Apollo Router, designed to orchestrate multiple GraphQL services into a unified API.

Overview

This archetype creates a GraphQL federated gateway that acts as a single entry point for multiple GraphQL services. Built on Apollo Router, it provides high-performance schema federation, intelligent query planning, and comprehensive monitoring capabilities.

Technology Stack

  • Apollo Router: High-performance GraphQL federation runtime
  • YAML Configuration: Declarative service configuration
  • Docker: Containerization and deployment
  • Prometheus: Metrics and monitoring
  • OpenTelemetry: Distributed tracing

Key Features

Federation Capabilities

  • Schema Composition: Automatic federation of multiple GraphQL schemas
  • Query Planning: Intelligent query execution across federated services
  • Type Merging: Seamless entity resolution across services
  • Real-time Updates: Dynamic schema composition updates

Performance & Reliability

  • High Throughput: Rust-based performance with minimal overhead
  • Connection Pooling: Efficient upstream service connections
  • Caching: Intelligent query and schema caching
  • Health Checks: Comprehensive service health monitoring

Observability

  • Metrics: Detailed performance and usage metrics
  • Tracing: End-to-end request tracing
  • Logging: Structured logging with correlation IDs
  • Dashboards: Pre-configured monitoring dashboards

Project Structure

rust-graphql-federated-gateway/
├── router.yaml # Apollo Router configuration
├── Dockerfile # Container build configuration
├── docker-compose.yml # Local development setup
├── schemas/ # Federated service schemas
│ ├── user-service.graphql
│ ├── product-service.graphql
│ └── order-service.graphql
├── config/
│ ├── telemetry.yaml # Observability configuration
│ └── cors.yaml # CORS settings
└── docs/
├── deployment.md # Deployment guide
└── federation.md # Federation setup

Configuration

Router Configuration (router.yaml)

supergraph:
introspection: true

federation_version: 2

telemetry:
apollo:
schema_reporting:
enabled: true
metrics:
prometheus:
enabled: true
tracing:
jaeger:
enabled: true

cors:
origins:
- "https://app.example.com"
allow_credentials: true

upstreams:
user-service:
url: http://user-service:4000/graphql
product-service:
url: http://product-service:4000/graphql
order-service:
url: http://order-service:4000/graphql

Federation Schema Example

# User Service Schema
extend schema @link(url: "https://specs.apollo.dev/federation/v2.0")

type User @key(fields: "id") {
id: ID!
email: String!
profile: UserProfile
}

type UserProfile {
firstName: String!
lastName: String!
avatar: String
}

# Product Service Schema
type Product @key(fields: "id") {
id: ID!
name: String!
price: Money!
category: Category!
}

# Order Service Schema
type Order @key(fields: "id") {
id: ID!
user: User! @provides(fields: "email")
items: [OrderItem!]!
total: Money!
}

Deployment Options

Docker Deployment

FROM apollographql/router:latest

COPY router.yaml /dist/config/router.yaml
COPY schemas/ /dist/schemas/

EXPOSE 4000

CMD ["--config", "/dist/config/router.yaml"]

Kubernetes Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
name: graphql-gateway
spec:
replicas: 3
selector:
matchLabels:
app: graphql-gateway
template:
spec:
containers:
- name: router
image: your-registry/graphql-gateway:latest
ports:
- containerPort: 4000
env:
- name: APOLLO_GRAPH_REF
value: "your-graph@current"
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"

Federation Best Practices

Schema Design

  • Use @key directives for entity identification
  • Implement @provides for field optimization
  • Design entities with clear ownership boundaries
  • Avoid circular dependencies between services

Performance Optimization

  • Implement query complexity analysis
  • Use DataLoader patterns in federated services
  • Configure appropriate timeout values
  • Enable query batching where possible

Security

  • Implement authentication at the gateway level
  • Use field-level authorization
  • Validate queries against complexity limits
  • Enable CORS with specific origins

Monitoring & Observability

Key Metrics

  • Request latency and throughput
  • Federation query planning time
  • Upstream service response times
  • Error rates by service and field

Alerting

  • High error rates (>5%)
  • Slow query performance (>2s)
  • Upstream service failures
  • Schema composition failures

Quick Start

  1. Generate the project:

    archetect render git@github.com:p6m-archetypes/rust-graphql-federated-gateway.archetype.git
  2. Configure services:

    # Update router.yaml with your service URLs
    vim router.yaml
  3. Start locally:

    docker-compose up -d
  4. Access GraphQL Playground:

    http://localhost:4000/
  5. Test federation:

    query TestFederation {
    users {
    id
    email
    orders {
    id
    total
    items {
    product {
    name
    price
    }
    }
    }
    }
    }

Advanced Features

  • Query Caching: Redis-based query result caching
  • Rate Limiting: Per-client and per-operation limits
  • Schema Registry: Centralized schema management
  • A/B Testing: Traffic splitting for schema versions