All right.
So right now in our system, the only way for incoming requests to enter our application is through
our reservations microservice and specifically in the reservations controller.
We of course have all of our Crud methods which are exposing all of our reservation Crud functionality.
We also of course have our auth controller, which is going to be exposing basic login and the user's
controller to create users.
And this is all being exposed through Http, Http and this is all being exposed through rest APIs.
However, I want to show you how we can build an API gateway using GraphQL and Apollo Federation so
that we can stitch together all of our different microservices and expose any endpoint we want regardless
of the microservice or the transport layer that's being used.
So we'll be able to expose endpoints in our reservations, auth and payments, microservice using one
microservice and to the end user, there's only going to be one endpoint that they need to use to be
able to access all of these different microservices and they're all going to be combined together in
our API gateway, which is really going to simplify things and make it very easy for us to expose external
facing functionality in our app.
So let's go ahead and set up GraphQL Apollo Federation so that we can expose all of our microservices
over a common GraphQL microservice.
So before we get started, as usual, I'm going to keep all of the code for this GraphQL API gateway
implementation on a separate branch called GraphQL.
So if you would like to see the completed version of this GraphQL implementation, please check out
to the Branch GraphQL.
However, I'm going to switch back to the main branch and continue working.
So our first step is going to be using the CLI to generate a new app that we will call Gateway.
So let's go ahead and generate this gateway app.
And you can see here that we have generated this new microservice with the basic gateway controller
and of course our nest CLI Json has been updated to generate this gateway application.
Now that we have the gateway created, let's go ahead and install the dependencies.
We're going to need to set up our GraphQL Apollo Federation server.
Now I'll leave some useful links if you want to learn more about GraphQL or Apollo Federation.
Essentially, Apollo Federation is going to allow us to stitch together multiple different GraphQL microservices
into one common GraphQL microservice so that we only need to expose that one endpoint to the user,
even though the underlying microservices will be completely separate.
So Apollo Federation is going to make this very easy to do.
And like I said, I'll leave some links if you want to learn more about this.
However, let's go ahead and get started with setting this up.
So we're going to go ahead and NPM install dash dash, save a number of dependencies.
So let's go ahead and install Apollo slash gateway, Apollo slash server.
Apollo slash subgraph.
Nestjs, Apollo Nestjs GraphQL, and finally GraphQL itself.
Let's go ahead and let these dependencies finish installing.
All right.
So there's some dependencies that you can see here that we actually need to update since we're using
these versions of Apollo and GraphQL.
So you can reference my Package.json to see the current versions I'm using.
And on these versions that we're using, we're going to actually have to upgrade our current version
of Nestjs.
So let's go ahead and do that for Nestjs Common.
I am going to upgrade to the latest version available as of this video.
In my case, this is going to be ten .1.3.
We'll go ahead and also update Nestjs core to ten .1.3 and the same thing for Platform Express so that
we're all on the same version.
And to stay consistent, we'll go ahead and upgrade our dev dependency of nestjs CLI to the latest of
ten 1.11.
We'll upgrade nestjs schematics to the latest of ten .0.2 and nestjs testing.
I will go ahead and upgrade to ten .1.3.
So now that we've upgraded to the latest version of Nestjs, let's go ahead and run a NPM install dash
r to go ahead and install all of the dependencies recursively in our project.
Okay, so if all the dependencies we need set up, let's go into our docker, compose where we will
expose our new gateway microservice and set everything up so that we can run it properly using Docker.
So we want to add a new service here called Gateway to represent our GraphQL Gateway microservice.
So let's go ahead and just copy the existing notification service and paste it in.
We'll simply change the name to Gateway and let's change the Docker file path to app slash gateway slash
Docker file.
And then we can change the Start command to be gateway.
Change the file path to be in the gateway folder.
And now we have everything we need to run the gateway application.
So let's go ahead and set up the Gateway app by firstly creating a Docker file inside of the gateway
folder so that we can actually build and run this container.
Now, finally, we need to make sure that we add a port section to the gateway so that we will be exposing
the GraphQL server that will be running on our port to our local machine.
So let's expose Port 3004 to our local machine 3004, and I'll go ahead and apply quotes to this ports
definition.
I'll simply go ahead and copy the Docker file from the auth microservice and paste it in and we can
simply change out a few things in here.
So we will change the copy command on line 12 to copy app slash gateway into app slash gateway.
We'll change the run build command to run build Gateway.
And finally, for the actual command to start the microservice, we will change the path to be in dist
apps gateway slash main.
And we can check to see here that we have no more references to auth and everything has been refactored
to work with our gateway microservice.
Finally, let's go ahead and create a dot m file in the gateway folder.
And now let's go ahead and run Docker, compose up and specify.
We want to up the Gateway service.
If it's your first time starting the Gateway Service, you should see the Docker file, build your container
and then we will start up with our next command here.
All right.
So now we can see that the Gateway application has successfully started.
Let's go ahead and start refactoring our gateway.
So firstly, in our main.ts, we want to make sure we have our config module set up as usual and listen
on the port environment variable that we will set in the env.
So let's open up the gateway module and in our imports array, let's make sure we import the config
module from nestjs config call for route and we'll set the is global to true so that the config module
will be globally available.
Next we'll add the logger module from.
Apps common to make sure we have our common logger in place and now we can go to the main.ts.
So let's firstly go ahead and set up our logger as we normally have done.
So we'll call app dot, use logger and then we'll call app dot get and then get the logger from Nestjs
piano.
Next up, let's get the config service from the app dot get and we'll pass in the config service.
So now we have the config service from nestjs config and then we can make sure we're listening on the
port from config service dot get or throw and get the port from our m file.
So now in our dot env I'll add a new port and set it equal to 3004.
So now if we go ahead and restart our gateway, we should see that it starts back up on port 3004.
Okay, so our application has started up.
Now I'll go ahead and remove all of our unused files from the gateway.
We don't need the gateway controller or the Gateway service.
We're actually only going to be utilizing the Gateway module because this is where we're going to be
setting up our GraphQL Apollo Server and exposing all of our other GraphQL microservices so we can remove
these imports and the references to these files in our Gateway module.
All right, so let's go ahead and set up our GraphQL server in the Gateway to do this.
We'll go ahead and call GraphQL module dot for root async.
And then we're going to pass in the current driver configuration that we're going to be utilizing.
So in our case, we're going to be using the Apollo Gateway driver config and now we can open up.
Parentheses and provide an options object where we firstly need to specify the driver that we're going
to be utilizing.
So let's specify the driver we're using and our case we're using the Apollo gateway driver from Nestjs
Apollo.
Now we can set up the rest of our GraphQL module using our asynchronous use factory where we can inject
our config service as we would normally do.
So let's go ahead and inject the config service and expose the options that the rest of the GraphQL
module expects.
And then we'll make sure we inject the config service so that it's available in the use factory.
So to configure the rest of our GraphQL microservices that this gateway will be exposing, we're going
to use the gateway property.
And in here we have another property called Supergraph SDL.
So this is going to be the map of all of the other subgraphs or other GraphQL microservices that the
gateway will be proxying off requests to.
So we need to tell the Gateway GraphQL module where these other microservices are listening on so that
when a user visits our GraphQL server on the Gateway, they have access to all of the underlying GraphQL
servers that we will be exposing here.
So let's go ahead and do that.
To do this, we'll set this property equal to new Introspect and compose from Apollo slash Gateway.
And in here we'll pass in an options object where we now specify the subgraphs that I mentioned.
So this is going to be an array of subgraphs or other GraphQL microservices that we want to stitch together
and form a master graph that will be exposed to the user.
So let's go ahead and do that by setting up our first GraphQL microservice that we want to expose.
Now of course we don't have any yet, but we'll start off with the reservations.
Microservice So let's go ahead and firstly provide the name of the service that we want to expose or
the graph.
In this case we'll call it reservations, and then we simply specify the URL of where that GraphQL server
is going to be running on.
So in our case, we can actually pull this from the config service, we'll call it config service dot
get or throw and get the reservations GraphQL URL.
So now this is all we have to do to set up our GraphQL server and stitch together our reservations.
GraphQL Microservice, which like I said, doesn't exist yet, but we'll handle that next.
So let's go ahead and specify this in the dot env before we forget.
So we need to set up the reservations.
GraphQL URL and this is going to be equal to Http because it's going to be an Http server of the reservation
service and we give it the host name, which of course we know since we're running in Docker, it's
going to be the name we specify in our Docker compose file.
So in our case it's the reservation service and then it's simply the Http server will be running on.
So we know if we go to reservations.
That we are listening on Port 3000.
So we're simply just targeting the Http server that's already exposed and reservations on Port 3000.
And when we set up our GraphQL server on reservations, it's going to be listening by default on slash
GraphQL.
So let's go ahead and save this file.
And now we have everything we need to expose our reservations.
GraphQL Microservice.
Let's go ahead and actually set this up in the reservations, Microservice.