Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Create an Apollo Gateway

    Alex BanksAlex Banks
    apolloApollo
    graphqlGraphQL

    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.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    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.

    Discuss

    Discuss