Supergraph with Federated Microservice

Introduction

In modern software development, microservices architecture has become a popular approach to building scalable and maintainable applications. Each microservice is designed to handle a specific piece of business logic (single responsibility) and can be developed, deployed, and scaled independently.

However, as the number of microservices grows, managing and integrating them efficiently becomes a challenge. This is where the concept of a Supergraph comes in, combined with robust authentication and authorization mechanisms to ensure secure and seamless data access.

please find the scope of the article.

  • What is supergraph, and what are the problems it solves?
  • Federated microservice
  • High-level architecture
  • Use cases
  • Code setup
  • Deployment
  • Conclusion

What is supergraph, and what are the problems it solves?

A Supergraph is an overarching schema that unifies multiple GraphQL schemas from different microservices (also known as subgraphs) into a single, cohesive graph.

Apollo Federation is a framework that enables the creation of such a supergraph. It allows developers to manage and query a distributed graph of services as if it were a single graph, promoting better modularization and collaboration.

The problem that SuperGraph Solves.

  • Fragmented Data Access
  • Performance Optimization
  • Cross-Service Relationships
  • Service Composition and Scalability
  • Centralized API Management
  • Security and Governance
  • Developer Productivity and Collaboration

For Example

Consider an e-commerce application with the following microservices.

  • User Service: Manages user data.
  • Product Service: Manages product information.
  • Order Service: Manages orders and needs user and product information.

Without a Supergraph, the client might need to make separate requests to each service and handle the aggregation of data client-side. With a Supergraph, a single query can fetch the necessary data from all relevant services.

query {
  users {
    id
    name
    orders {
      id
      product {
        id
        name
      }
      quantity
    }
  }
}

Federated microservice

Federated microservices refer to a distributed architectural approach where individual services collaborate to form a cohesive, unified system.

Apollo Federation introduces the concept of federated microservices in GraphQL. It extends the GraphQL schema stitching approach by allowing services to define their own GraphQL schemas (subgraphs) and then federate them into a single, coherent schema (supergraph). So the consumer could connect to a single endpoint for all their business requirement.

High-level architecture

Federated microservice

Business Use cases

In a real-time scenario, please find some of the following areas where could use this tech stack.

  • E-commerce Platform
    • A customer logs into the e-commerce platform and browses products. The supergraph, via Apollo Federation, allows the client to fetch user details, product information, and personalized recommendations in a single query.
    • When the customer adds a product to the cart, the inventory service updates the stock levels in real-time.
    • The supergraph ensures that all this data is seamlessly integrated and consistently available across the services.
  • Financial Services
    • An investor logs into their portfolio management app to view real-time updates on their investments. The supergraph integrates data from the portfolio service, market data service, and transaction service. This allows the user to see their current portfolio value, receive real-time market updates, and execute transactions within a single interface. The supergraph ensures real-time data consistency and a seamless user experience.
  • Healthcare System
    • A patient uses a healthcare app to monitor their health metrics in real-time and schedule appointments. The supergraph aggregates data from the health monitoring service, appointment service, and doctor service. This allows the patient to view their current health status, receive alerts if any metrics are abnormal, and schedule a doctor’s appointment seamlessly. The supergraph ensures that the health data is up-to-date and consistent across services.
  • Fleet-Maintenance System
    • While the service center creates a job sheet for vehicles. s/he needs to call multiple systems, this super graph integrates all the services in a single endpoint such as vehicle data, fleet operator, driver, and vehicle maintenance history.
  • Logistics and Supply Chain
    • A logistics manager uses a dashboard to track shipments and manage warehouse inventory in real-time. The supergraph integrates data from the shipment service, inventory service, and order service. This allows the manager to view the current status and location of shipments, check stock levels in the warehouse, and process incoming and outgoing orders efficiently. The supergraph ensures real-time visibility and data synchronization across the logistics network.

Code setup

Supergraph with Apollo Federation involves several steps, including creating subgraph services, defining schemas, authentication, authorization, and setting up the Apollo Gateway. Here’s a step-by-step guide on how to set up a Supergraph

  1. Create UserService & OrderService Rest API.
  2. Define GraphQL Schema for UserService & OrderService Rest API. This we use to call a Sub-graph.
  3. Implement the Server with Authentication and Authorisation to connect UserService & OrderService Rest API.
  4. Implement Gateway with Authentication and Authorisation.
  5. Create a router file.
    federation_version: 1
    subgraphs:
      users:
        routing_url: http://localhost:5127/graphql
        schema:
          file: UserService/schema.graphql      
      orders:
        routing_url: http://localhost:5195/graphql
        schema:
          file: OrderService/schema.graphql
    
  6. Create a Supergraph schema from the router file using the Powershell script.
    set APOLLO_ELV2_ACCEPT=YES
    rover supergraph compose --config supergraph.yaml > supergraph-schema.graphql
  7. Run the gateway project.
    • This will run and display a unified schema which is a combination of user and order service
    • To test this gateway, please make sure subgraphs are up and running.

Deployment

Deployment

Conclusion

Integrating a Supergraph with federated microservices offers a scalable and efficient way to manage and query a distributed graph of services. By incorporating robust authentication and authorization mechanisms, you ensure that your services are secure and that users have appropriate access to resources. This setup not only enhances the modularity and collaboration among different services but also provides a seamless and secure user experience.

Thanks for reading, and happy learning. hope you enjoyed this article.


Similar Articles