🚀 Spring Boot + GraphQL Federation (DGS + Hasura + Apollo Federation)
The Most Trending Spring Boot Topic in 2024–2025
Modern companies rarely use single-service backends. They have:
✔ user-service
✔ product-service
✔ order-service
✔ payment-service
All containing separate databases → but the UI needs unified data.
GraphQL Federation solves this.
⭐ Why GraphQL Federation is Trending
Traditional REST fails with:
❌ Over-fetching
❌ Multiple network calls
❌ Complex API versioning
❌ Cross-service joins (N+1 problem)
GraphQL Federation provides:
✔ One unified supergraph
✔ Strong schema contract
✔ Automatic merging of microservice schemas
✔ Cross-service reference resolution
✔ Sub-second multi-API queries
✔ Reduced payload size
✔ Better performance for mobile/web apps
This is why companies like Netflix, Amazon, PayPal, Walmart are adopting it.
🧩 Tech Stack
Spring Boot + Netflix DGS → GraphQL service
Hasura / Apollo Gateway → Federation layer
Apollo Router → High-performance supergraph
⭐ 1. Building a GraphQL Microservice Using Spring Boot + DGS
Add dependency:
<dependency>
<groupId>com.netflix.graphql.dgs</groupId>
<artifactId>graphql-dgs-spring-boot-starter</artifactId>
<version>8.0.1</version>
</dependency>✔ Example GraphQL Schema (product-service)
schema.graphqls
type Product @key(fields: "id") {
id: ID!
name: String
price: Float
}@key→ used for GraphQL Federationiduniquely identifies the product entity across microservices
✔ Resolver Using DGS
@DgsComponent
public class ProductDataFetcher {
@DgsQuery
public Product product(@InputArgument String id) {
return new Product(id, "Laptop", 79999.0);
}
}⭐ 2. Another Microservice Extends the Entity (order-service)
Example: Order service wants product details for each order.
Schema:
type Order {
id: ID!
productId: ID!
quantity: Int
product: Product
}
extend type Product @key(fields: "id") {
id: ID! @external
}✔ Resolver for Federated Product in Order Service
@DgsComponent
public class ProductReferenceResolver implements DgsEntityFetcher {
@Override
public Object fetch(Map<String, Object> values) {
String productId = (String) values.get("id");
return new Product(productId, null, null);
}
}⭐ 3. Apollo Federation Gateway (Supergraph Layer)
Your microservices expose:
supergraph-config.json
{
"subgraphs": {
"product": { "url": "http://localhost:8081/graphql" },
"order": { "url": "http://localhost:8082/graphql" }
}
}Apollo Gateway automatically merges the schemas:
npx @apollo/rover supergraph compose --config ./supergraph-config.jsonThis produces a supergraph schema.
⭐ 4. Frontend Query (One Query → Many Microservices)
query {
orders {
id
quantity
product {
id
name
price
}
}
}🚀 Spring Boot microservices work together as one API.
🚀 Apollo Gateway aggregates all data.
🚀 No REST chaining required.
⭐ 5. Adding Real-time Data (GraphQL Subscriptions)
Spring Boot supports Subscriptions for:
Get Dolly’s stories in your inbox
Join Medium for free to get updates from this writer.
✔ Live order tracking
✔ Live inventory updates
✔ Live notifications
DGS example:
@DgsSubscription
public Publisher<Order> orderUpdates() {
return Flux.interval(Duration.ofSeconds(1))
.map(i -> new Order("ORD" + i, "P001", 1));
}⭐ 6. GraphQL Performance Enhancements
🚀 DataLoader
Prevents N+1 database queries.
public class ProductBatchLoader implements BatchLoader<String, Product> {
public CompletionStage<List<Product>> load(List<String> ids) {
return CompletableFuture.supplyAsync(() -> fetchProducts(ids));
}
}⭐ 7. Security in GraphQL Federation
✔ JWT authentication
✔ Role-based authorization with DataFetchers
✔ API gateway-level auth
✔ Field-level security
Example (method-level security):
@PreAuthorize("hasRole('ADMIN')")
@DgsQuery
public Product productSecure(String id) { ... }⭐ 8. Why Companies Prefer GraphQL Federation Over REST
⭐ 9. Real-World Use Cases
✔ eCommerce
- Orders + Payments + Products combined into one query
✔ Banking
- Customer + Transactions + Loans unified
✔ Healthcare
- Patient + Reports + Appointments in one graph
✔ Logistics
- Real-time tracking + shipment status
✔ SaaS Platforms
- Multiple backend services → one GraphQL API
⭐ 10. Interview Questions (Frequently Asked)
- What is GraphQL Federation?
- Difference between DGS and Spring Boot GraphQL?
- Role of
@key,@external,@provides,@requires? - How does Apollo Gateway merge schemas?
- What is a supergraph?
- How do subscriptions work in Spring Boot?
- How does DataLoader solve the N+1 problem?
- Compare REST vs GraphQL vs GraphQL Federation.











