Sitemap
JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

🚀 Spring Boot + GraphQL Federation (DGS + Hasura + Apollo Federation)

3 min readDec 2, 2025

--

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 Federation
  • id uniquely 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.json

This 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)

  1. What is GraphQL Federation?
  2. Difference between DGS and Spring Boot GraphQL?
  3. Role of @key, @external, @provides, @requires?
  4. How does Apollo Gateway merge schemas?
  5. What is a supergraph?
  6. How do subscriptions work in Spring Boot?
  7. How does DataLoader solve the N+1 problem?
  8. Compare REST vs GraphQL vs GraphQL Federation.

--

--

Dolly
Dolly

Written by Dolly

Java & Spring Boot Developer | Writing about Backend Development, Microservices, and Scalable System Design. Helping developers build production-ready APIs.

JavaScript in Plain English
JavaScript in Plain English

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.