I am fascinated to work with GraphQL federated API. Recently, I was building a code generation framework that can generate actual compilable and deployable code in Java and connect automatically through GraphQL Federation API.
For this to shine, I needed to have a profound understanding of how GrahQL works and what is going underneath.
In this post, I will discuss what goes into the life cycle of a federated GraphQL API. We will take a very simple Federated GraphQL API built with NodeJS and intercept its traffic with HTTP Toolkit.
You can follow along with me at every step and I promise the result will be delicious. We will get to know how Gateway queries all the attached services.
The project is hosted at:
In a Federated GraphQL service, the responsibility of the gateway is to
Consolidate all the component GraphQL service schema structures.
Validate all the services by doing a query and making sure the graphql API exposed by all the component services is properly federated.
When the call comes to the gateway understand that query and decide what query to be made to all the intended services
Do the queries parallel y to all the relevant services and wait for the response
Obtain the responses and consolidate them and send them to the caller, also add respective errors if any error has been sent any error.
Here we have 2 federated services.
Service 1: Employee Service
Service 2: Address Service
The Address type is extending the Employee type and provides address data for each employee.
And here is my Gateway.
The code is ready and when running this:
npm run server
we get our gateway server ready
Let’s go to
Yep, it is running for sure and we can get address data for each employee. Let’s shut it down and debug the process.
Let’s start HTTP Toolkit and select Fresh Terminal.
and run the same command to run the service in the freshly opened terminal so that HTTP Toolkit can intercept the traffic and we can know what it going on.
Let’s see what is happening.
The first query made by the gateway to the Address and Employee services are the same and it is like below:
{"query": "query __ApolloGetServiceDefinition__ { _service{sdl} }"}
This is a gateway verifying the services by validating the schema of the services.
The Employee service responds:
And the Address service responds:
After verifying the schema structures, the gateway is ready to accept incoming calls. Let’s debug that too.
Here is our query:
{
employeeData(id:332233){
employeeId
address{
addressId
}
}
}
As expected, we are seeing 2 calls made from the gateway to the service.
The first call to Employee service.
and the call to Address service.
Observe here a couple of things.
Now the responses.
Employee service response to Gateway
Address service response to Gateway
Gateway finally consolidates and sends the response as :
That’s it, this concludes our journey.
A couple of things to keep in mind while developing GraphQL Federated API
{“query”: “query ApolloGetServiceDefinition { _service{sdl} }”}
I hope you liked the journey. See you soon.
Originally published here.