Instructor: 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.
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.
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.
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.
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.
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.
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.
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.
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.