Skip to main content

Apollo Federation: GraphQL Supergraph Architecture Guide

Apollo Federation lets you split a large GraphQL schema across independent services (subgraphs) owned by separate teams, then compose them into one unified supergraph. Clients see a single API; your teams stay decoupled.

โœ“ Team-owned subgraphs โœ“ Entity references across services โœ“ Apollo Router for high performance โœ“ Rover CLI for schema management

Subgraph Definition with @key

Each subgraph declares its entity types with @key. The Users subgraph owns the User entity; the Orders subgraph extends it with order data:

# users-subgraph schema.graphql
type User @key(fields: "id") {
  id: ID!
  name: String!
  email: String!
}

type Query {
  user(id: ID!): User
  me: User
}

# orders-subgraph schema.graphql
# Reference User without redefining all its fields
type User @key(fields: "id") {
  id: ID!
  orders: [Order!]!
}

type Order {
  id: ID!
  total: Float!
  status: OrderStatus!
  createdAt: String!
}

enum OrderStatus { PENDING SHIPPED DELIVERED }

type Query {
  order(id: ID!): Order
}

Subgraph Server Setup (Node.js)

Use @apollo/subgraph to build each federation-aware subgraph and implement the __resolveReference function for entity resolution:

import { ApolloServer } from '@apollo/server';
import { buildSubgraphSchema } from '@apollo/subgraph';
import { gql } from 'graphql-tag';

const typeDefs = gql`
  type User @key(fields: "id") {
    id: ID!
    name: String!
    email: String!
  }
  type Query { user(id: ID!): User }
`;

const resolvers = {
  User: {
    // Called when another subgraph references this entity by key
    __resolveReference(ref: { id: string }) {
      return db.users.findById(ref.id);
    },
  },
  Query: {
    user: (_: unknown, { id }: { id: string }) => db.users.findById(id),
  },
};

const server = new ApolloServer({
  schema: buildSubgraphSchema({ typeDefs, resolvers }),
});

// Start on a unique port per subgraph, e.g. 4001

Composing with Rover CLI

Use rover supergraph compose to combine subgraph schemas locally, or publish to Apollo Studio for managed composition:

# supergraph.yaml โ€” compose config
federation_version: =2.4.0
subgraphs:
  users:
    routing_url: http://users-service:4001/graphql
    schema:
      subgraph_url: http://users-service:4001/graphql
  orders:
    routing_url: http://orders-service:4002/graphql
    schema:
      subgraph_url: http://orders-service:4002/graphql

# Compose locally and output the supergraph SDL
rover supergraph compose --config supergraph.yaml > supergraph.graphql

# Start the Apollo Router with the composed schema
./router --supergraph supergraph.graphql --config router.yaml

# Publish a subgraph schema to Apollo Studio (CI step)
rover subgraph publish my-graph@prod \
  --name users \
  --schema ./users-schema.graphql \
  --routing-url https://users.prod.internal/graphql

Federation vs Schema Stitching vs Monolith

Criteria Federation 2 Schema Stitching Monolith Schema
Team ownership Per-subgraph Partial Shared codebase
Entity references @key directive Manual mergeTypes N/A (co-located)
Schema checks rover graph check Manual TypeScript types
Performance Apollo Router (Rust) Node.js gateway Direct resolvers
Best for Large orgs, microservices Legacy migration Small teams, startups
Share this tool: X / Twitter LinkedIn
โœ“ 60,000+ developers use DevPlaybook โ˜…โ˜…โ˜…โ˜…โ˜… 4.8 / 5 on Gumroad โœ“ MIT licensed ยท Instant download โœ“ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • โšก AI Code Review, Doc Generator & SQL Builder
  • โšก All premium templates & early access
  • โšก Member discounts on Gumroad products
Start Free Trial โ†’

$9/mo after trial ยท Cancel anytime

Want the full toolkit?

Get DevPlaybook Pro

Every template, guide, boilerplate, and automation script in one bundle. 13 premium products โ€” grab them all at once and save big.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro โ€” $79 โ†’

MIT licensed ยท Instant download ยท No subscription

See what's included โ†’ Browse all deals โ†’

Frequently Asked Questions

What is Apollo Federation?▼

Apollo Federation is an architecture for composing multiple GraphQL services (subgraphs) into a single unified API (supergraph). Instead of one monolithic GraphQL schema, each team owns their own subgraph. The Apollo Router or Apollo Gateway combines them into one schema that clients query as if it were a single service.

What is the difference between Federation 1 and Federation 2?▼

Federation 2 (Apollo Federation v2) introduced shareable types, removing the restriction that only one subgraph can define a given type. It replaced the @external directive with a simpler @shareable model, improved entity resolution with @interfaceObject, and introduced the rover CLI for schema composition and Rover Studio for visualization. Federation 1 is still supported but new projects should use Federation 2.

What is a subgraph @key directive?▼

The @key directive marks which field(s) uniquely identify an entity type in a subgraph. When other subgraphs reference the same entity, they use @key to declare how to resolve a reference to that entity. For example, @key(fields: 'id') on a User type means any subgraph can provide more fields for User as long as they can resolve it by id.

Can subgraphs be written in different languages?▼

Yes. Apollo Federation is language-agnostic at the protocol level. Subgraphs communicate via standard GraphQL and a federation specification extension. Libraries exist for Node.js, Java (DGS), Python (strawberry-graphql), Go, Rust, and more. Each subgraph just needs to implement the _entities query and the federation SDL directives.

What is the Rover CLI used for?▼

Rover is Apollo's CLI tool for managing supergraph schemas. You use rover subgraph publish to push a subgraph's schema to Apollo Studio, rover supergraph compose to compose a supergraph schema locally for development, and rover graph check to run schema checks against your registered schema before deploying.

What is schema stitching and why choose federation over it?▼

Schema stitching is an older approach to combining GraphQL schemas where a gateway manually merges type definitions and delegates resolvers. Federation improves on this by making subgraphs self-describing โ€” each subgraph declares its own entity keys and relationships without the gateway needing custom configuration per type. Federation is more maintainable at scale and is the recommended approach for new projects.