A technical tutorial demonstrating how to integrate GraphQL into a NestJS microservice. The process involves configuring the GraphQL module, creating resolvers for CRUD operations, and applying decorators to models and DTOs to automatically generate the GraphQL schema.
Length 12:17
Captions English (Generated), Spanish (Auto-translated), French (Auto-translated), Korean (Auto-translated), Portuguese (Auto-translated), Vietnamese (Auto-translated) #graphql #nestjs #microservices #typescript #coding #programming #backend #resolver #schema #api
0:00 Okay, so our first step to add GraphQL to our 0:03 reservations 0:04 microservice will be to open up the 0:07 reservations module. And in our imports array, 0:11 let's go ahead and add a new import to the 0:15 GraphQL module from 0:17 Nestjs/graphql, 0:20 and we're gonna call for root on it. 0:23 We can then pass in the options or the config that we'll be f- 0:26 supplying. In this case, it's gonna be the Apollo 0:30 Federation driver config. 0:34 And then let's open up an options object 0:37 where we're gonna specify two properties. 0:40 So the first will be the driver that we're using. 0:43 So this will be the Apollo Federation driver from 0:47 Nestjs/apollo. 0:49 And then we're gonna specify auto schema file 0:53 equal to an object. We're gonna set federation 0:57 equal to two, because we're gonna be using 1:00 Fed-Federation version two. 1:02 And what this auto schema file is gonna do is it's gonna automatically 1:06 generate the GraphQL schema from our 1:09 code. So when we set up the GraphQL types in 1:13 our code, they're automatically gonna be converted 1:17 into a 1:17 GraphQL schema that GraphQL understands, and there's nothing else we're gonna 1:21 have to do. This is automatically going to be 1:24 happening for 1:25 us 1:26 since we set this property. So next up, we're ready to add our first 1:30 resolver to our reservation service, which is gonna be the 1:34 equivalent of an HTTP controller in the GraphQL 1:38 world. So the resolver is going to be the list of 1:42 mutations and queries that we're gonna expose in our schema 1:46 and that users can call using GraphQL into our 1:50 system. So let's go ahead and create this. 1:52 In the source folder, we'll create a 1:55 reservations.resolver.ts. 1:59 Now let's add the @resolver decorator from 2:02 Nestjs/graphql. And now we need to specify 2:06 the entity that we're returning from this resolver, 2:10 and this is going to be added to our GraphQL schema automatically
The rest of the transcript is for members.
Sign in