Become a member
to unlock all features

Level Up!

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


    Set Up Apollo Federated Services

    Alex BanksAlex Banks

    In order to implement Apollo Federation, you must make your GraphQL APIs federated services. A federated service can communicate with the Apollo Gateway and extend other federated services. In this lesson, we will convert two regular GraphQL APIs into Apollo federated services.



    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




    Instructor: I already have the code base for two separate GraphQL APIs living in these separate folders. Let's start with the lift service. From the terminal, I will navigate to the lift directory and I'll install Apollo Federation. I'll also make sure that I have the latest versions of the Apollo server and GraphQL. Federation will not work with old versions of the Apollo server. Here, we have a regular GraphQL API that resolves data about lifts. We need to make this a federated API. We'll have to import a function to build a federated schema from Apollo Federation. We'll still need the typeDefs and resolvers. I'm just going to go ahead and cut these two and put them on the clipboard. Then, when we create the Apollo server, we'll need to add a schema key, where we will use the buildFederatedSchema function with our typedefs and our resolvers to build a federated schema. I can go ahead and start this service. We have a federated service that's running on port 4001. I can test this service out by adding a query for all lifts, we'll get the name in the status, and we can see that our service is working. We can also run this service query, which returns the schema for the service. You can see that we also have some additional directives that were added to the schema. These directives will allow us to extend and resolve other federated services. I'm going to open a new terminal instance. I'll navigate to the trails folder and repeat the process to make this service federated as well. We'll install Apollo Federation, Apollo Server, and GraphQL. I'll import the buildFederatedSchema function. We'll use this function to create the schema for our second federated service. We'll start the trail service. I can come over here and test it. We can see that this service is running on 4002. It will give us the trail count, and a list of all of the trails. We have created two federated services that can work behind in Apollo Gateway.