Federated services can be orchestrated behind a Gateway API. The Gateway is a single API that clients can use to access data from any of the federated services. In this lesson, we will create an Apollo Gateway that orchestrates the lift and trail services which will allow us to query both lifts and trails from a single endpoint.
Instructor: [00:00] Now, it's time for us to create another service, an Apollo gateway. Let's make a gateway directory and navigate to it. We'll need a new package.json, so I'm going to go ahead and create that with npm init.
[00:12] I'm going to send it this -y, which will automatically answer all the questions as yes. That creates a package.json for us. Now, we can go ahead and install the Apollo gateway, Apollo server, and GraphQL.
[00:28] I'll create a new index.js file, and I'll go ahead and open this file in VS Code. In this file, I'll import the Apollo server and the Apollo gateway. We'll need to set up a new instance of the Apollo gateway. When we create a gateway instance, we need to send it a service list.
[00:56] This is where we will place all of the federated services that we want this gateway to use. I still have the lift service running on port 4001 and the trail service running on port 4002. Now that we have a gateway, we can use it.
[01:11] First, I'm going to need to set up an async function. This is an anonymous JavaScript function that will be invoked immediately. We created it so that we can use async await syntax as we initialize the server.
[01:27] gateway.load returns a promise, so we need to wait for it before it can be resolved. Once this promise resolves, we can grab the gateway schema and the executor. We'll use these to create a new server instance with Apollo server.
[01:42] Now, we can start our gateway instance on port 4000. We'll add a little message to the gateway once it has loaded. Now, I'm going to go ahead and open a new terminal instance and start our gateway. I can see the playground is running on localhost:4000.
[02:05] From here, we can access all the lifts, their name and their status, and all the trails, their name, difficulty, and grooming status, in one query. We can see that the gateway resolves data from both federated services.
[02:20] What is happening? The gateway will take this query and break it up into parts, which it will execute on the appropriate downstream service. The executor executes a query plan, and the playground now comes with a query plan tab.
[02:35] This tab lets you see the plan that will be executed. Here, we see that two downstream requests will be made in parallel. The gateway has extracted the allLifts query to execute on the lift service, and it also grabbed the allTrails part of the query, which it will execute on the trail service. Parallel means that both of these requests will be made at the same time.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!