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.
Instructor: [00:00] I already have the code base for [00:02] two separate GraphQL APIs living [00:04] in these separate folders. Let's [00:07] start with the lift service. [00:09] From the terminal, I will [00:10] navigate to the lift directory [00:12] and I'll install Apollo [00:14] Federation. I'll also make sure [00:15] that I have the latest versions [00:17] of the Apollo server and GraphQL. [00:19] Federation will not work with [00:21] old versions of the Apollo [00:22] server. Here, we have a regular [00:24] GraphQL API that resolves data [00:26] about lifts. We need to make [00:28] this a federated API. We'll have [00:30] to import a function to build a [00:32] federated schema from Apollo [00:33] Federation. We'll still need the [00:37] typeDefs and resolvers. [00:39] I'm [00:39] just going to go ahead and cut [00:40] these two and put them on the [00:42] clipboard. Then, when we create [00:44] the Apollo server, we'll need to [00:46] add a schema key, where we will [00:48] use the buildFederatedSchema [00:50] function with our typedefs and [00:51] our resolvers to build a [00:53] federated schema. I can go ahead [00:55] and start this service. We have [00:57] a federated service that's [00:58] running on port 4001. [01:01] I can [01:02] test this service out by adding [01:02] a query for all lifts, we'll get [01:04] the name in the status, and we [01:06] can see that our service is [01:07] working. We can also run this [01:11] service query, which returns the [01:13] schema for the service. You can [01:15] see that we also have some [01:16] additional directives that were [01:17] added to the schema. These [01:19] directives will allow us to [01:20] extend and resolve other [01:21] federated services. [01:27] I'm going [01:28] to open a new terminal instance. [01:30] I'll navigate to the trails [01:31] folder and repeat the process to [01:33] make this service federated as [01:34] well. We'll install Apollo [01:37] Federation, Apollo Server, and [01:40] GraphQL. [01:49] I'll import the [01:51] buildFederatedSchema function. [01:55] We'll use this function to [01:56] create the schema for our second [01:58] federated service. [02:02] We'll start [02:03] the trail service. I can come [02:07] over here and test it. We can [02:09] see that this service is running [02:10] on 4002. It will give us the [02:12] trail count, and a list of all [02:14] of the trails. We have created [02:17] two federated services that can [02:19] work behind in Apollo Gateway.
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!