Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions apps/www/content/glossary/graphql-federation.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
---
title: "GraphQL Federation: Comprehensive Guide"
description: Unlock GraphQL Federation power. Learn essentials from real-world examples. Dive into architecture and best practices.
h1: "GraphQL Federation: Essentials & Best Practices"
term: GraphQL Federation
categories: []
takeaways:
tldr: GraphQL Federation is an architectural pattern that allows multiple independent GraphQL services (subgraphs) to be combined into a unified schema, facilitating a microservices approach.
didYouKnow: Before the introduction of GraphQL Federation, the primary method for combining GraphQL schemas was 'schema stitching', which was complex and error-prone. Federation introduced new keywords to define connections between types in different services, simplifying the process.
usageInAPIs:
tags:
- GraphQL
- Federation
- Microservices
description: GraphQL Federation is used to combine multiple GraphQL APIs into a single endpoint, simplifying client interactions and supporting a microservices architecture. It allows for schema design at scale, where the federated schema appears monolithic to the client, abstracting away the underlying microservices implementation. This approach promotes distributed responsibility, independent scaling, and clear domain boundaries within an organization.
bestPractices:
- Design schemas with clear domain boundaries to promote independent scaling and distributed responsibility.
- Use a gateway as a central coordinator to compose separate schemas into a unified schema for client queries.
- Leverage federation-specific directives like '@key', '@external', '@requires', and '@extends' to define connections between types in different services.
historicalContext:
- key: Introduced
value: "2019"
- key: Origin
value: Microservices (GraphQL Federation)
- key: Evolution
value: Standardized GraphQL Federation
recommendedReading:
- url: https://www.apollographql.com/docs/graphos/schema-design/federated-schemas/federation
title: Introduction to Apollo Federation
- url: https://www.contentful.com/blog/understanding-federated-graphql/
title: Understanding federated GraphQL
- url: https://stackoverflow.com/questions/68754117/graphql-federation-vs-schema-stitching-when-to-pick-one-over-the-other
title: GraphQL Federation vs Schema Stitching
- url: https://github.com/graphql-python/graphene-federation
title: Graphene Federation
definitionAndStructure:
- key: Subgraphs
value: Independent Services
- key: Gateway
value: Central Coordinator
- key: Schema Composition
value: Unified Schema
faq:
- question: What is a federation in GraphQL?
answer: GraphQL Federation is a set of standards for building a unified GraphQL schema from multiple underlying GraphQL services. It allows different teams to develop parts of a GraphQL schema independently, and then combines these parts into a single, unified API. This approach is particularly useful for large-scale, distributed systems where different teams are responsible for different services. Each team can focus on their specific domain, while the federation ensures that the overall API remains consistent and seamless for the end users.
- question: What is the difference between federation and gateway in GraphQL?
answer: In the context of GraphQL, Federation and Gateway are two different components of the same architecture. Federation refers to the process of combining multiple GraphQL services into a single unified schema. Each of these services, known as subgraphs, defines its own GraphQL schema and resolvers. On the other hand, a Gateway is a specialized service that sits between the clients and the federated services. It is responsible for receiving client requests, delegating them to the appropriate subgraphs, and then assembling the results into a single response.
- question: Is GraphQL losing popularity?
answer: No, GraphQL is not losing popularity. Since its introduction in 2015, it has seen a steady increase in adoption among developers. According to the State of JavaScript 2020 report, nearly half of the developers surveyed had adopted GraphQL. It is being used by large companies such as Starbucks, Paypal, and Facebook, indicating its relevance and popularity in the industry. Its ability to allow clients to specify exactly what data they need makes it a powerful alternative to traditional REST APIs.
updatedAt: 2025-06-16T12:51:53.000Z
slug: graphql-federation
---

**GraphQL Federation** is a powerful method for building a single, cohesive GraphQL API from multiple underlying services. This architecture allows teams to work independently on their services while presenting a unified API to clients. Designed to handle complex, distributed architectures, GraphQL Federation enables a more scalable approach to API development.

## Understanding GraphQL Federation Architecture

GraphQL Federation involves combining multiple, separate GraphQL services into a single data graph without the need for a monolithic schema. Each service, referred to as a **federated service**, owns a portion of the schema and is responsible for the data it manages. The architecture relies on a **gateway** that orchestrates queries across these services, aggregating results into a seamless response for the client.

## Core Components of GraphQL Federation Explained

The primary components of **GraphQL Federation** include:

- **Federated Services**: Individual services that define and execute parts of the overall GraphQL schema.
- **Gateway**: A central service that composes the individual schemas into a unified schema and routes queries to the appropriate services.
- **Schema Definitions**: Each federated service defines its own part of the schema using SDL (Schema Definition Language), including types, queries, and mutations specific to its domain.

## Advantages of GraphQL Federation vs. Schema Stitching

**GraphQL Federation** offers several advantages over traditional schema stitching:

- **Performance**: Federation allows for more efficient query planning and execution by only involving the necessary services.
- **Decoupling**: Services can be developed and deployed independently, enhancing team autonomy and reducing bottlenecks.
- **Schema Evolution**: Services can evolve their schema without global coordination, as long as they maintain contracts with the gateway.

In contrast to schema stitching, which combines schemas at runtime, **GraphQL Federation** integrates at the schema level, allowing for more sophisticated optimizations and a cleaner developer experience.

## Operational Mechanics of GraphQL Federation

The operational flow of **GraphQL Federation** involves:

1. **Schema Composition**: The gateway fetches schemas from each federated service and composes them into a single, cohesive schema.
2. **Query Planning**: When a query is received, the gateway constructs a plan to resolve the query across the necessary federated services.
3. **Execution**: The gateway executes the query according to the plan, making calls to federated services and stitching together the results.

Here’s a simple **GraphQL Federation example** using Apollo:

```typescript
import { ApolloGateway } from '@apollo/gateway';
import { ApolloServer } from 'apollo-server';

const gateway = new ApolloGateway({
serviceList: [
{ name: 'accounts', url: 'http://localhost:4001' },
{ name: 'reviews', url: 'http://localhost:4002' }
]
});

const server = new ApolloServer({ gateway, subscriptions: false });
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
```

## Schema Design Principles in GraphQL Federation

Effective schema design in **GraphQL Federation** should:

- **Encapsulate Domain Logic**: Ensure that each service’s schema logically represents its underlying domain.
- **Use Extensible Types**: Leverage GraphQL's type extension features to add fields from one service to types owned by another service.
- **Minimize Overlap**: Avoid defining the same types in multiple services to prevent conflicts and confusion.

## Common Pitfalls and Best Practices in GraphQL Federation

Some common pitfalls include:

- **Overfetching**: Poorly planned queries can lead to overfetching from services.
- **Service Dependencies**: Excessive dependencies between services can complicate the architecture.

Best practices include:

- **Monitoring and Logging**: Implement comprehensive monitoring and logging to track the health and performance of the gateway and services.
- **Error Handling**: Design robust error handling at the gateway to manage partial failures gracefully.

By adhering to these principles and being aware of common pitfalls, developers can effectively leverage **GraphQL Federation** to build scalable, efficient, and maintainable APIs. Whether you're using **Apollo Federation**, **GraphQL Federation with Spring Boot**, or **GraphQL Federation with NestJS**, understanding these concepts will enhance your API development process.

For further exploration, consider looking into **open source GraphQL Federation** projects or how **Netflix** utilizes this architecture to manage their complex service landscape.