Skip to main content

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:
  1. Gateway receives your GraphQL query
  2. Creates a query plan across relevant subgraphs
  3. Executes queries against subgraphs (in parallel where possible)
  4. Stitches results together
  5. 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.
The @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:
This allows the 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

Shared Types

Some types are marked @shareable, meaning multiple subgraphs can define them:
Common shareable types:
  • PageInfo - Pagination metadata
  • GeoJSON* types - Geospatial types
  • Scalar types - DateTime, JSONObject

Cross-Subgraph Queries

Federation enables queries that span multiple subgraphs seamlessly.

Example: Asset with Issue Information

The gateway:
  1. Queries su-assets for asset data
  2. Takes the asset reference
  3. Queries su-issues for issue information
  4. Merges the results

Example: User with Authentication

Federation Directives

@key

Marks a type as an entity with a unique key:
Multiple keys are supported:

@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

Query plan:

Cross-Subgraph Query

Query plan:

Performance Considerations

Parallel Execution

Federation executes independent fetches in parallel:

N+1 Problem

Be aware of the N+1 problem with entity references:
Solution: Federation batches entity resolutions using DataLoader-like mechanisms.

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

  1. Design entity keys carefully: Choose stable, immutable fields for @key
  2. Minimize entity hops: Deeply nested cross-subgraph queries can be slow
  3. Use @provides sparingly: Only when it significantly reduces fetches
  4. Batch related queries: Combine multiple operations in one request
  5. Monitor query plans: Use Apollo Studio to identify inefficient plans
  6. Handle partial failures: Subgraph failures may return partial data
  7. Version subgraph changes: Coordinate breaking changes across subgraphs
  8. Test entity resolution: Ensure reference resolvers handle all key combinations

Differences from Schema Stitching

Resources

Studio Access

For detailed query planning and performance monitoring, access Apollo Studio: https://studio.apollographql.com/graph/su-graphql-t912yf/ (Contact your SensorUp account team for access)