Apollo Federation in Go part2
Introducing Managed federation
In the previous post, I introduced how to implement Apollo Federation in the Go application.
In this post, I will walk you through the Managed federation feature in Apollo Federation.
Example Repository
Here’s the final codebase on GitHub. main
branch is a version of part-1. part-2
branch is used in this post.
Contents
Managed Federation
Apollo Federation is an essential architecture that allows your organization to scale GraphQL API with multiple implementing services in one graph.
It has its own gateway server which handles the request from the client-side and executes incoming operations across the services, like distributes shared data to each service.
This gateway fetches each schema from the implementing services and composes them into a single federated graph on startup.
So, when you update your schema, you have to update the configuration and re-run a server in the gateway.
With managed federation, you don’t have to modify or redeploy your gateway. Instead, you need to push the schema to the Apollo schema registry, which checks to see if the schema is valid, if so, compose them into a single schema.
After composition success, Apollo uploads the configuration in GCS so that your gateway can poll for config changes regularly.
So, Managed federation enables you to:
- safely validate your schema
- deploy automatically
- observe the changes in each graph
Set up a managed federation
Let’s try out the managed federation feature in our app.
The steps of setting up as follows:
Create the Apollo Studio account
To get started with the managed federation, we need to create the Apollo account.
First, go to https://studio.apollographql.com/ and create your account:
Create the graph
After the signup flow, create your first graph.
And specify your graph name, and click Next:
And select the Federation tab and copy your settings, and click Done.
Register schema
Now that we’ve had our graph on Apollo Studio. Next, we will register all of our implementing services’ schema.
First, we will upload the user
service schema.
Start the user server:
go run service-users/main.go
And run this:
npx apollo service:push \
--graph={your graph here} \
--key=service:{your key here} \
--variant=current \
--serviceName=users \
--serviceURL=http://localhost:4001/query \
--endpoint=http://localhost:4001/query
serviceName
… The implementing service name should be unique.
serviceURL
… The URL that the gateway will access.
serviceURL
is supposed to be the URL of which you deploy your server but in this case, we don’t have any deployed server yet so will use a local server instead.
Next, we will upload the profile
service schema.
Run the server:
go run service-profile/main.go
And upload it:
npx apollo service:push \
--graph={your graph here} \
--key=service:{your key here} \
--variant=current \
--serviceName=profile \
--serviceURL=http://localhost:4002/query \
--endpoint=http://localhost:4002/query
After done, go to https://studio.apollographql.com/ and you can see that it generated our schema.
After uploading them, the registry composes the latest version into a single federated schema.
Connect gateway to Apollo Studio
Now that our gateway can fetch the latest schema instead of the local one. With managed federation, the service list in the gateway is no longer needed because the gateway can regularly poll the information from the apollo registry.
Open up gateway/index.js
and remove the serviceList
:
const gateway = new ApolloGateway();
Next, we will set Apollo’s API key to the envrionment variables.
Go to https://studio.apollographql.com/ and Settings
/ ApiKeys
.
Click …
and copy the key’s value:
And set the value to the environment variable, APOLLO_KEY
, like so:
APOLLO_KEY=xxxxx
For simplicity, we will use the dotenv package.
yarn add -D dotenv
Add gateway/.env
:
APOLLO_KEY=xxxxx
Open up gateway/package.json
and change the script to:
"scripts": {
"start": "node -r dotenv/config index.js"
},
Then, run the gateway server:
yarn start
Access to http://localhost:4000
and do query:
Good — The local gateway server could fetch the configuration from the apollo registry on startup and provided the schema information as well.
Conclusion
We’ve walked through Apollo Federation features and Managed federation in these two posts.
Now that you could obtain the basic knowledge and implementation of composing multiple services into a graph with Apollo Federation.
But in real cases, the implementing services could get larger and more complicated with a lot of needs depending on your business.
For this challenge, I recommend that you should read these awesome posts:
- How Netflix Scales its API with GraphQL Federation (Part 1)
- How Netflix Scales its API with GraphQL Federation (Part 2)
- Managed federation overview in Apollo docs
I hope this post will help you get inspiration.