|
| 1 | +--- |
| 2 | +sidebarTitle: Federation |
| 3 | +--- |
| 4 | + |
| 5 | +import { Tabs } from 'nextra/components' |
| 6 | + |
| 7 | +# GraphQL Federation |
| 8 | + |
| 9 | +In today's world of distributed systems and microservices, |
| 10 | +organizations face the challenge of efficiently managing and scaling their APIs. |
| 11 | +As applications grow more complex, traditional monolithic approaches often fall short. |
| 12 | +This is where federation comes into play - an architectural pattern that has found particular resonance in the GraphQL ecosystem. |
| 13 | + |
| 14 | +GraphQL Federation gained widespread adoption after [Apollo GraphQL introduced Apollo Federation in 2019](https://www.apollographql.com/blog/apollo-federation-f260cf525d21). |
| 15 | +Their implementation has become a reference point for the GraphQL community, |
| 16 | +helping establish federation as a standard architectural pattern in the GraphQL ecosystem. |
| 17 | + |
| 18 | +The GraphQL ecosystem is moving towards standardization of federation patterns. |
| 19 | +The GraphQL Foundation's [Composite Schema Working Group](https://github.com/graphql/composite-schemas-wg), |
| 20 | +which includes engineers from various organizations across the industry including |
| 21 | +Apollo GraphQL, ChilliCream, Google, Graphile, The Guild, Hasura, and IBM, |
| 22 | +is actively working on creating an official specification for GraphQL Federation. |
| 23 | +This effort aims to standardize how GraphQL services can be composed and executed across distributed systems, |
| 24 | +while ensuring room for innovation and different implementations. |
| 25 | + |
| 26 | +## What is federation? |
| 27 | + |
| 28 | +Federation is an approach to organizing and managing distributed systems. |
| 29 | +At its core, federation allows autonomous components to work together while maintaining their independence. |
| 30 | +Think of it like a federal government system: individual states maintain their sovereignty |
| 31 | +while cooperating under a central authority for shared concerns. |
| 32 | + |
| 33 | +In software architecture, federation enables organizations to: |
| 34 | + |
| 35 | +- Distribute responsibility across independent teams |
| 36 | +- Scale different components independently |
| 37 | +- Maintain clear boundaries between different domains |
| 38 | +- Enable autonomous development and deployment |
| 39 | +- Reduce single points of failure |
| 40 | + |
| 41 | +Think of the "Login with Google" or "Login with Facebook" buttons you see on websites. |
| 42 | +This is federation in action: you can use your Google or Facebook account to log into many different websites, |
| 43 | +even though each company manages their own login system separately. |
| 44 | + |
| 45 | +## What Is Federated GraphQL? |
| 46 | + |
| 47 | +GraphQL Federation applies those principles to GraphQL APIs. |
| 48 | +It enables organizations to build a unified GraphQL schema from multiple independent services (most often called subgraphs), |
| 49 | +each responsible for its portion of the application's data graph. |
| 50 | + |
| 51 | +Consider an e-commerce platform: You might have separate teams managing products, user accounts, and order processing. With GraphQL Federation, each team can: |
| 52 | + |
| 53 | +- Define their own GraphQL schema |
| 54 | +- Deploy and scale their service independently |
| 55 | +- Contribute to a unified API without tight coupling |
| 56 | +- Maintain ownership of their domain-specific logic |
| 57 | + |
| 58 | +The magic happens through a federated gateway that acts as the central coordinator, combining these separate schemas into a unified schema that clients can query. |
| 59 | + |
| 60 | +## How Federation Works in GraphQL |
| 61 | + |
| 62 | +The federation process involves several key components: |
| 63 | + |
| 64 | +- **Subgraphs**: Individual services that define their own GraphQL schemas and resolvers |
| 65 | +- **Gateway**: A specialized service that sits between clients and your federated services |
| 66 | +- **Schema composition**: The process of merging schemas while resolving references between them, often handled by schema registries. |
| 67 | + |
| 68 | +<Tabs items={['Products subgraph', 'Orders subgraph', 'Users subgraph']}> |
| 69 | +<Tabs.Tab> |
| 70 | +`graphql |
| 71 | + type Product @key(fields: "id") { |
| 72 | + id: ID! |
| 73 | + title: String! |
| 74 | + price: Float! |
| 75 | + inStock: Boolean! |
| 76 | + } |
| 77 | + ` |
| 78 | +</Tabs.Tab> |
| 79 | +<Tabs.Tab> |
| 80 | +```graphql |
| 81 | +type Order @key(fields: "id") { |
| 82 | +id: ID! |
| 83 | +products: [Product!]! |
| 84 | +total: Float! |
| 85 | +} |
| 86 | + |
| 87 | + type Product { |
| 88 | + id: ID! |
| 89 | + } |
| 90 | + ``` |
| 91 | + |
| 92 | +</Tabs.Tab> |
| 93 | +<Tabs.Tab> |
| 94 | +```graphql |
| 95 | +type Query { |
| 96 | +user(id: ID!): User |
| 97 | +} |
| 98 | + |
| 99 | + type User { |
| 100 | + id: ID! |
| 101 | + name: String! |
| 102 | + email: String |
| 103 | + orders: [Order!]! |
| 104 | + } |
| 105 | + |
| 106 | + type Order { |
| 107 | + id: ID! |
| 108 | + } |
| 109 | + ``` |
| 110 | + |
| 111 | +</Tabs.Tab> |
| 112 | +</Tabs> |
| 113 | + |
| 114 | +### Schema Composition |
| 115 | + |
| 116 | +Let's break down schema composition in GraphQL federation with more detail and examples. |
| 117 | +Schema composition is the process where multiple subgraph schemas are combined into one unified schema. |
| 118 | +It's more complex than simply merging schemas together, because it needs to handle relationships, detect incompatibilities, and ensure types are properly connected across services. |
| 119 | + |
| 120 | +Based on the examples we provided before, here's the unified schema GraphQL clients will see and can query: |
| 121 | + |
| 122 | +```graphql |
| 123 | +type Query { |
| 124 | + user(id: ID!): User |
| 125 | +} |
| 126 | + |
| 127 | +type User { |
| 128 | + id: ID! |
| 129 | + name: String! |
| 130 | + email: String |
| 131 | + orders: [Order!]! |
| 132 | +} |
| 133 | + |
| 134 | +type Order { |
| 135 | + id: ID! |
| 136 | + products: [Product!]! |
| 137 | + total: Float! |
| 138 | +} |
| 139 | + |
| 140 | +type Product { |
| 141 | + id: ID! |
| 142 | + title: String! |
| 143 | + price: Float! |
| 144 | + inStock: Boolean! |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +This unified schema combines types and fields from all three subgraphs (Users, Orders, and Products), allowing clients to seamlessly query across these domains. |
| 149 | + |
| 150 | +### Gateway |
| 151 | + |
| 152 | +The federation gateway is the entry point to your distributed data graph. |
| 153 | +It presents a unified GraphQL endpoint to clients |
| 154 | +and handles the complexity of routing queries to the appropriate subgraphs and assembling the results, |
| 155 | +often provides caching and performance optimizations. |
| 156 | + |
| 157 | +```mermaid |
| 158 | +graph TD |
| 159 | + Client --> FederationGateway |
| 160 | + FederationGateway --> UserService |
| 161 | + FederationGateway --> OrderService |
| 162 | + FederationGateway --> ProductService |
| 163 | + |
| 164 | + Client[Client] |
| 165 | + FederationGateway[Gateway] |
| 166 | + UserService[User Service] |
| 167 | + OrderService[Order Service] |
| 168 | + ProductService[Product Service] |
| 169 | +``` |
| 170 | + |
| 171 | +Take the following query as an example: |
| 172 | + |
| 173 | +```graphql |
| 174 | +query { |
| 175 | + user(id: "123") { |
| 176 | + # Resolved by Users subgraph |
| 177 | + name |
| 178 | + orders { |
| 179 | + # Resolved by Orders subgraph |
| 180 | + id |
| 181 | + products { |
| 182 | + # Resolved by Products subgraph |
| 183 | + title |
| 184 | + price |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +The gateway will route parts of the query to the appropriate subgraphs, collect the results, |
| 192 | +and assemble them into a single response that the client can consume. |
| 193 | + |
| 194 | +## Benefits of GraphQL Federation |
| 195 | + |
| 196 | +### Domain-Driven Development |
| 197 | + |
| 198 | +Teams can work independently on their services while contributing to a cohesive API. |
| 199 | +This autonomy accelerates development and reduces coordination overhead. |
| 200 | + |
| 201 | +### Service Integrity Protection |
| 202 | + |
| 203 | +The schema composition step verifies integration between services |
| 204 | +by ensuring that changes in individual subgraphs |
| 205 | +do not conflict with other subgraphs in the federation. |
| 206 | + |
| 207 | +### Scalability and Performance |
| 208 | + |
| 209 | +Services can be scaled independently based on their specific requirements. |
| 210 | +The product catalog might need different scaling characteristics than the order processing system. |
| 211 | + |
| 212 | +### Single, Unified API |
| 213 | + |
| 214 | +Thanks to GraphQL gateway, clients get a single endpoint with unified schema. |
| 215 | +The complexity of distributed systems is hidden. |
| 216 | +The gateway ensures every query reaches its destination and returns with the right data. |
| 217 | + |
| 218 | +## Federation vs. Monolithic Architecture |
| 219 | + |
| 220 | +While monolithic GraphQL APIs have their place, federation offers several advantages for larger applications: |
| 221 | + |
| 222 | +| **Aspect** | **Monolithic GraphQL** | **Federated GraphQL** | |
| 223 | +| ----------------- | --------------------------------- | ----------------------------- | |
| 224 | +| Development Speed | Initially faster | Better for long-term velocity | |
| 225 | +| Team Coordination | Requires significant coordination | Enables independence | |
| 226 | +| Deployment | All-or-nothing | Independent deployments | |
| 227 | +| Scaling | Entire system scales together | Independent scaling | |
| 228 | + |
| 229 | +## Real-World Implementations |
| 230 | + |
| 231 | +GraphQL Federation is adopted by tech giants such as |
| 232 | +[Netlifx](https://netflixtechblog.com/how-netflix-scales-its-api-with-graphql-federation-part-1-ae3557c187e2), |
| 233 | +[Expedia Group](https://youtu.be/kpeVT7J6Bsw?si=srGWsoxf3kTmneTu&t=79), |
| 234 | +[Volvo](https://www.apollographql.com/blog/volvo-cars-drives-into-the-future-of-online-car-shopping-with-the-supergraph), |
| 235 | +and [Booking](https://youtu.be/2KsP_x50tGk?si=mu-MOG-xZQSDNDjh&t=478). |
| 236 | + |
| 237 | +Many industry leaders successfully use GraphQL federation at scale, proving that federation works reliably for large-scale production applications. |
| 238 | + |
| 239 | +## Getting Started with GraphQL Federation |
| 240 | + |
| 241 | +To implement GraphQL federation, organizations should: |
| 242 | + |
| 243 | +1. **Identify Service Boundaries**: Define clear boundaries between different domains in your application |
| 244 | +2. **Design Schemas**: Create schemas that reflect these boundaries while considering how they'll interact |
| 245 | +3. **Implement Subgraphs**: Build individual services that implement their portion of the schema |
| 246 | +4. **Set Up Gateway**: Deploy a federation gateway to compose and serve the unified schema |
| 247 | +5. **Monitor and Optimize**: Implement monitoring and continuously optimize query performance |
| 248 | + |
| 249 | +Organizations can gradually migrate from a monolithic to federated GraphQL, one service at a time. |
0 commit comments