An Introduction Tutorial To Apollo GraphQL Federation

In this article, we will see what is Apollo Federation and what kind of problem that federation is trying to solve with a real-time example. Introduction to Apollo GraphQL Federation


Apollo Federation is an architecture of composing multiple GraphQL services into a single endpoint.


when you start to think about building a microservice, it would be difficult to divide the GraphQL layer for different services. For example, we are dividing it something like this –



This may seem to make sense at first. but, the problem is adding a new feature. Let’s say that we want to add top comments in Post. Post-service doesn’t know how to resolve a query for top comments because data about comment will be stored in the Comment Service.


On the other hand, Apollo Federation allows us to extend the Post type in Any Service, Here Comment service with a extend type functionality.



Therefore, this keeps all the code for a given feature in a single service and separated from unrelated concerns.


Federation Core Concepts

Mainly, let us see the core concept of apollo federation which compiles different services together to form a single graph


Entities and keys

Firstly, It is a type which is referenced by another service. it creates a connection between services and form a federated graph. entities have a primary key which identifies the specific instance of the type.

entities can be declared by using a keyword @key in the Type

type User @key(fields: "_id") {
   _id : ID
   name : String
   email : String
}


External Type Reference

Once, the entity is declared, other services can reference this type from their own types. Let’s see how the Comment Service refer the User Service in our example

type Comment {
   user: User
}

extend type User @key(fields : "_id") {
   _id : ID @external
}