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?

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.

For Example

Consider an e-commerce application with the following microservices.

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.

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.