0
0
GraphQLquery~5 mins

Why federation scales GraphQL

Choose your learning style9 modes available
Introduction

Federation helps GraphQL grow by letting many teams work together easily. It breaks a big API into smaller parts that fit well together.

When multiple teams build different parts of a big app and want to share data smoothly.
When you want to add new features without changing the whole GraphQL API.
When you want to keep your GraphQL API fast and organized as it gets bigger.
When you want to reuse data from different services in one place.
When you want to avoid one team blocking others by working on separate parts.
Syntax
GraphQL
type Product @key(fields: "id") {
  id: ID!
  name: String
  price: Float
}

extend type Query {
  product(id: ID!): Product
}

The @key directive marks the unique field to identify an entity across services.

extend type lets you add fields to types defined in other services.

Examples
This example shows a User type identified by email and how another service can add a field to Review that links to User.
GraphQL
type User @key(fields: "email") {
  email: String!
  name: String
}

extend type Review {
  author: User
}
Here, Product is identified by UPC code, and Inventory extends it to add stock quantity.
GraphQL
type Product @key(fields: "upc") {
  upc: String!
  price: Float
}

extend type Inventory {
  product: Product
  quantity: Int
}
Sample Program

This shows two services: one for products and one for reviews. They share the Product type using federation. The gateway lets clients ask for product info and reviews together.

GraphQL
# Service A: Product service

type Product @key(fields: "id") {
  id: ID!
  name: String
}

extend type Query {
  product(id: ID!): Product
}

# Service B: Review service

type Review {
  id: ID!
  body: String
  product: Product @provides(fields: "name")
}

extend type Product @key(fields: "id") {
  id: ID! @external
  name: String @external
  reviews: [Review]
}

# Gateway combines these services so clients query one API

# Query example:
# {
#   product(id: "1") {
#     name
#     reviews {
#       body
#     }
#   }
# }
OutputSuccess
Important Notes

Federation lets teams own parts of the schema independently, improving collaboration.

It reduces the risk of one big schema becoming too complex to manage.

Common mistake: forgetting to mark keys or external fields, which breaks linking between services.

Summary

Federation splits a big GraphQL API into smaller, manageable parts.

It helps many teams work together without conflicts.

Clients get a single API that combines data from all services smoothly.