Apollo Federation Concepts
The SensorUp GraphQL API is built on Apollo Federation v2.3, which enables multiple independent GraphQL services (subgraphs) to be composed into a single, unified graph.What is Apollo Federation?
Apollo Federation is an architecture for building distributed GraphQL APIs. Instead of one monolithic GraphQL server, Federation allows you to:- Separate concerns: Each subgraph owns a specific domain
- Scale independently: Subgraphs can be deployed and scaled separately
- Compose seamlessly: The gateway stitches subgraphs into one unified API
- Extend types: Subgraphs can add fields to types defined elsewhere
Architecture Overview
When you query the gateway:- Gateway receives your GraphQL query
- Creates a query plan across relevant subgraphs
- Executes queries against subgraphs (in parallel where possible)
- Stitches results together
- Returns unified response
Key Federation Concepts
Entities
Entities are types that can be referenced and extended across subgraphs. They’re marked with the@key directive.
@key directive specifies the fields needed to uniquely identify an instance of this type.
Type Extensions
Subgraphs can extend types defined in other subgraphs using@extends:
su-issues subgraph to add the issueSubject field to the Asset type originally defined in su-assets.
Entity Resolution
When a subgraph needs to resolve an entity, it provides a reference resolver:SensorUp Federation Structure
Entity Ownership
| Entity | Owner | Extended By |
|---|---|---|
Asset | su-assets | su-issues, su-eventhub |
User | su-user | su-auth |
Site | su-sites | su-methane-ldar-backend |
Issue | su-issues | (none) |
EmissionObservation | su-methane-ldar-backend | (none) |
Shared Types
Some types are marked@shareable, meaning multiple subgraphs can define them:
PageInfo- Pagination metadataGeoJSON*types - Geospatial types- Scalar types -
DateTime,JSONObject
Cross-Subgraph Queries
Federation enables queries that span multiple subgraphs seamlessly.Example: Asset with Issue Information
- Queries
su-assetsfor asset data - Takes the asset reference
- Queries
su-issuesfor issue information - Merges the results
Example: User with Authentication
Federation Directives
@key
Marks a type as an entity with a unique key:@extends
Indicates a type is being extended from another subgraph:@external
Marks fields that are defined in another subgraph:@requires
Specifies fields needed from the base type to resolve a field:@provides
Optimizes queries by providing fields from related entities:@shareable
Allows multiple subgraphs to define the same type/field:Query Planning
The gateway creates an optimized query plan for each request.Simple Query
Cross-Subgraph Query
Performance Considerations
Parallel Execution
Federation executes independent fetches in parallel:N+1 Problem
Be aware of the N+1 problem with entity references:Debugging Federation
Query Plan Visualization
Use Apollo Studio to visualize query plans and identify performance bottlenecks.Subgraph Errors
When a subgraph fails, the error includes the service name:Trace Subgraph Calls
Monitor subgraph execution times:Best Practices
-
Design entity keys carefully: Choose stable, immutable fields for
@key - Minimize entity hops: Deeply nested cross-subgraph queries can be slow
- Use @provides sparingly: Only when it significantly reduces fetches
- Batch related queries: Combine multiple operations in one request
- Monitor query plans: Use Apollo Studio to identify inefficient plans
- Handle partial failures: Subgraph failures may return partial data
- Version subgraph changes: Coordinate breaking changes across subgraphs
- Test entity resolution: Ensure reference resolvers handle all key combinations
Differences from Schema Stitching
| Feature | Federation | Schema Stitching |
|---|---|---|
| Type ownership | Clear owner per type | Ambiguous |
| Type extension | Native support | Manual |
| Query planning | Automatic | Manual |
| Performance | Optimized | Variable |
| DX | Better | Complex |
Resources
- Apollo Federation Documentation
- Subgraph Reference - SensorUp subgraph schemas
- Performance Guide - Optimization strategies