You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -6,26 +6,37 @@ import { Tabs } from 'nextra/components'
6
6
7
7
# GraphQL Federation
8
8
9
-
As applications grow more complex, traditional monolithic approaches often fall short. At scale, monolithic architectures can become cumbersome, brittle, and increasingly hard to test and validate. This has led to a rise in the adoption of new patterns like distributed systems and microservices. In some ways, GraphQL Federation is like microservices for GraphQL – an architectural pattern that has found particular resonance in the GraphQL ecosystem.
10
-
11
-
GraphQL Federation gained widespread adoption after [Apollo GraphQL introduced Apollo Federation in 2019](https://www.apollographql.com/blog/apollo-federation-f260cf525d21).
12
-
Their implementation has become a reference point for the GraphQL community,
13
-
helping establish federation as a standard architectural pattern for building a distributed graph in the GraphQL ecosystem.
14
-
15
-
With more companies and developers seeing the benefits of building a distributed graph with federation, the GraphQL ecosystem is now moving towards standardization of federation patterns.
16
-
The GraphQL Foundation's [Composite Schema Working Group](https://github.com/graphql/composite-schemas-wg),
17
-
which includes engineers from various organizations across the industry including
18
-
Apollo GraphQL, ChilliCream, Graphile, Hasura, Netflix and The Guild,
19
-
is actively working on creating an official specification for GraphQL Federation.
20
-
This effort aims to standardize how GraphQL services can be composed and executed across distributed systems,
21
-
while ensuring room for innovation and different implementations.
9
+
As applications grow more complex, traditional monolithic approaches often fall short. At scale,
10
+
monolithic architectures can become cumbersome, brittle, and increasingly hard to test and validate.
11
+
This has led to a rise in the adoption of new patterns like distributed systems and microservices.
12
+
In some ways, GraphQL Federation is like microservices for GraphQL – an architectural pattern that
13
+
has found particular resonance in the GraphQL ecosystem.
14
+
15
+
GraphQL Federation gained widespread adoption after
16
+
[Apollo GraphQL introduced Apollo Federation in 2019](https://www.apollographql.com/blog/apollo-federation-f260cf525d21).
17
+
Their implementation has become a reference point for the GraphQL community, helping establish
18
+
federation as a standard architectural pattern for building a distributed graph in the GraphQL
19
+
ecosystem.
20
+
21
+
With more companies and developers seeing the benefits of building a distributed graph with
22
+
federation, the GraphQL ecosystem is now moving towards standardization of federation patterns. The
23
+
GraphQL Foundation's
24
+
[Composite Schema Working Group](https://github.com/graphql/composite-schemas-wg), which includes
25
+
engineers from various organizations across the industry including
[Netflix](https://www.netflix.com/) and [The Guild](https://the-guild.dev), is actively working on
29
+
creating
30
+
[an official specification for GraphQL Federation](https://github.com/graphql/composite-schemas-spec).
31
+
This effort aims to standardize how GraphQL services can be composed and executed across distributed
32
+
systems, while ensuring room for innovation and different implementations.
22
33
23
34
## What is federation?
24
35
25
-
Architecturally, federation is an approach to organizing and managing distributed systems.
26
-
At its core, federation allows autonomous components to work together while maintaining their independence.
27
-
Think of it like a federal government system: individual states maintain their sovereignty
28
-
while cooperating under a central authority for shared concerns.
36
+
Architecturally, federation is an approach to organizing and managing distributed systems. At its
37
+
core, federation allows autonomous components to work together while maintaining their independence.
38
+
Think of it like a federal government system: individual states maintain their sovereignty while
39
+
cooperating under a central authority for shared concerns.
29
40
30
41
In software architecture, federation enables organizations to:
31
42
@@ -35,35 +46,37 @@ In software architecture, federation enables organizations to:
35
46
- Enable autonomous development and deployment
36
47
- Reduce single points of failure
37
48
38
-
Think of the "Login with Google" or "Login with Facebook" buttons you see on websites.
39
-
This is federation in action: you can use your Google or Facebook account to log into many different websites,
40
-
even though each company manages their own login system separately.
49
+
Think of the "Login with Google" or "Login with Facebook" buttons you see on websites. This is
50
+
federation in action: you can use your Google or Facebook account to log into many different
51
+
websites, even though each company manages their own login system separately.
41
52
42
53
## What Is Federated GraphQL?
43
54
44
-
GraphQL Federation applies those principles to GraphQL APIs.
45
-
It enables organizations to build a unified GraphQL schema from multiple independent services (most often called subgraphs),
46
-
each responsible for its portion of the application's data graph.
55
+
GraphQL Federation applies those principles to GraphQL APIs. It enables organizations to build a
56
+
unified GraphQL schema from multiple independent services (most often called subgraphs), each
57
+
responsible for its portion of the application's data graph.
47
58
48
-
Consider an e-commerce platform: You might have separate teams managing products, user accounts, and order processing. With GraphQL Federation, each team can:
59
+
Consider an e-commerce platform: You might have separate teams managing products, user accounts, and
60
+
order processing. With GraphQL Federation, each team can:
49
61
50
62
- Define their own GraphQL schema
51
63
- Deploy and scale their service independently
52
64
- Contribute to a unified GraphQL API without tight coupling
53
65
- Maintain ownership of their domain-specific logic
54
66
55
-
The magic happens through a federated gateway that acts as the central coordinator, composing these separate schemas into a unified schema that clients can query.
67
+
The magic happens through a federated gateway that acts as the central coordinator, composing these
68
+
separate schemas into a unified schema that clients can query.
56
69
57
70
## How Federation Works in GraphQL
58
71
59
72
The federation process involves several key components:
60
73
61
74
-**Subgraphs**: Individual services that define their own GraphQL schemas and resolvers
62
75
-**Gateway**: A specialized service that sits between clients and your federated services
63
-
-**Schema composition**: The process of merging schemas while resolving references between them, often handled by schema registries.
76
+
-**Schema composition**: The process of merging schemas while resolving references between them,
@@ -74,8 +87,7 @@ type Product @key(fields: "id") {
74
87
}
75
88
```
76
89
77
-
</Tabs.Tab>
78
-
<Tabs.Tab>
90
+
</Tabs.Tab> <Tabs.Tab>
79
91
80
92
```graphql
81
93
typeOrder@key(fields: "id") {
@@ -89,8 +101,8 @@ type Product {
89
101
}
90
102
```
91
103
92
-
</Tabs.Tab>
93
-
<Tabs.Tab>
104
+
</Tabs.Tab> <Tabs.Tab>
105
+
94
106
```graphql
95
107
typeQuery {
96
108
user(id: ID!): User
@@ -108,16 +120,18 @@ type Order {
108
120
}
109
121
```
110
122
111
-
</Tabs.Tab>
112
-
</Tabs>
123
+
</Tabs.Tab> </Tabs>
113
124
114
125
### Schema Composition
115
126
116
-
Let'sbreakdownschema 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, though, because it needs to handle relationships, detect incompatibilities, and ensure types are properly connected across services and subgraphs.
127
+
Let'sbreakdownschema composition in GraphQL federation with more detail and examples. Schema
128
+
composition is the process where multiple subgraph schemas are combined into one unified schema.
129
+
It's more complex than simply merging schemas together, though, because it needs to handle
130
+
relationships, detect incompatibilities, and ensure types are properly connected across services and
131
+
subgraphs.
119
132
120
-
Based on the examples we provided before, here's the unified schema GraphQL clients will see and can query:
133
+
Based on the examples we provided before, here's the unified schema GraphQL clients will see and can
134
+
query:
121
135
122
136
```graphql
123
137
type Query {
@@ -145,14 +159,14 @@ type Product {
145
159
}
146
160
```
147
161
148
-
Thisunifiedschema combines types and fields from all three subgraphs (Users, Orders, and Products), allowing clients to seamlessly query across these domains.
162
+
Thisunifiedschema combines types and fields from all three subgraphs (Users, Orders, and
163
+
Products), allowing clients to seamlessly query across these domains.
149
164
150
165
### Gateway
151
166
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
-
and often provides caching and performance optimizations.
167
+
The federation gateway is the entry point to your distributed data graph. It presents a unified
168
+
GraphQL endpoint to clients and handles the complexity of routing queries to the appropriate
169
+
subgraphs and assembling the results, and often provides caching and performance optimizations.
156
170
157
171
```mermaid
158
172
graph TD
@@ -188,62 +202,73 @@ query {
188
202
}
189
203
```
190
204
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.
205
+
The gateway will route parts of the query to the appropriate subgraphs, collect the results, and
206
+
assemble them into a single response that the client can consume.
193
207
194
208
## Benefits of GraphQL Federation
195
209
196
210
### Domain-Driven Development
197
211
198
-
Teams can work independently on their services while contributing to a cohesive API.
199
-
This autonomy accelerates development and reduces coordination overhead.
212
+
Teams can work independently on their services while contributing to a cohesive API. This autonomy
213
+
accelerates development and reduces coordination overhead.
200
214
201
215
### Service Integrity Protection
202
216
203
-
The schema composition step verifies integration between services
204
-
by ensuring that changes in individual subgraphs
205
-
do not conflict with other subgraphs.
217
+
The schema composition step verifies integration between services by ensuring that changes in
218
+
individual subgraphs do not conflict with other subgraphs.
206
219
207
220
### Scalability and Performance
208
221
209
-
Subgraphs and services can be scaled independently based on their specific requirements.
210
-
The product catalog might need different scaling characteristics than the order processing system.
222
+
Subgraphs and services can be scaled independently based on their specific requirements. The product
223
+
catalog might need different scaling characteristics than the order processing system.
211
224
212
225
### Single, Unified API
213
226
214
227
Thanks to GraphQL, clients get a single endpoint with unified schema spanning multiple subgraphs.
215
-
The complexity of distributed systems is hidden.
216
-
The gateway ensures every query reaches its destination and returns with the right data.
228
+
The complexity of distributed systems is hidden. The gateway ensures every query reaches its
229
+
destination and returns with the right data.
217
230
218
-
## Federation vs. Monolithic Architecture
231
+
## Is GraphQL Federation Right for You?
219
232
220
-
While monolithic GraphQL APIs have their place, federated architectures offer several advantages for larger applications:
233
+
GraphQL Federation aligns naturally with Domain Driven Design (DDD) principles by allowing teams to
234
+
maintain clear boundaries around their domains, while maintaining explicit integration points
235
+
through the GraphQL schema. It is particularly valuable for organizations where multiple teams need
236
+
to work independently on different parts of the GraphQL API, with the flexibility to use different
and [Booking](https://youtu.be/2KsP_x50tGk?si=mu-MOG-xZQSDNDjh&t=478).
251
+
and [Booking](https://youtu.be/2KsP_x50tGk?si=mu-MOG-xZQSDNDjh&t=478) have adopted Federation to
252
+
better align with their organizational structures and microservices architecture.
236
253
237
-
Many industry leaders successfully use GraphQL federation at scale, proving that federation works reliably for large-scale production applications.
254
+
As you see, many industry leaders successfully federated their GraphQL APIs, proving that it works
255
+
reliably for large-scale production applications.
238
256
239
257
## Getting Started with GraphQL Federation
240
258
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 a federated 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 implementation, one service at a time.
259
+
If you're considering adopting GraphQL Federation, here are some steps to get started:
260
+
261
+
1.**Identify Service Boundaries**: Define clear boundaries between different domains in your
262
+
application
263
+
2.[**Design Schemas**](/learn/schema/'Learn about the different elements of the GraphQL type system'):
264
+
Create schemas that reflect these boundaries while considering how they'll interact
265
+
3.[**Implement Subgraphs**](/community/tools-and-libraries/?tags=server'Discover a list of GraphQL servers in our Tools and Libraries page'):
266
+
Build individual services that implement their portion of the schema
267
+
4.[**Set Up a Gateway**](/community/tools-and-libraries/?tags=gateways-supergraphs'Discover a list of GraphQL gateways in our Tools and Libraries page'):
268
+
Deploy a federation gateway to compose and serve the unified schema
269
+
5.[**Use a Schema Registry**](/community/tools-and-libraries/?tags=schema-registry'Discover a list of GraphQL schema registries in our Tools and Libraries page'):
270
+
Manage schema composition and validation to ensure integrity across subgraphs
271
+
272
+
When migrating from a monolithic GraphQL API to Federation, the simplest starting point is to treat
273
+
your existing schema as your first subgraph. From there, you can follow the steps above to gradually
0 commit comments