Build a GraphQL Server with NestJS and MongoDB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so just in a video about how to build a graph fuel server that uses MongoDB and a lot of people have been asking me to go try out nest Jas so I thought this would be a good time to try recreating that exact same server in SGS and see what the experience is like so that's what we're going to be doing so to start off I went ahead and just did MPM I - G - globally install the nest JSC Li then after that I ran the configuration to generate a project and that is where I'm currently act so right off the bat you'll notice with this nest GS uses typescript so you don't need to use for example babel and they have pretty much a nice little workspace set up for you out of the box I'd recommend just kind of like looking around see what they give you specifically the package.json file so we can see they install a couple dependencies for us and pretty much the entire dev set up for typescript you notice we have TS lint typescript itself TS node no demon all that fun stuff and then a whole bunch of scripts in here the one I'm gonna be using in this video is start dev so I already have that up and running right here and again this is using no demon so as we make changes stuff is gonna automatically reload so I went ahead and just ran the default project that it gives you and when you do that if we go to localhost 3000 we should see hello world and everything is working so the first thing that I wanted to do was see how we can add graph QL to this project tune SJS so there's this app that module thing over here that is kind of like the main module or introduction to our application I guess really main is but in main all we're really saying is neck nest factory create on this app module so this kind of the beginning or the central place so it recommends and we're just going to be going following the QuickStart for a graph QL so we're going to install these dependencies graph QL from nest Jia Apolo Server Express because it uses that underneath the hood graphical tools and graph kill itself so say you are an ad install those things and then after that we're going to import to the graph QL module and this is going to be inside of app not module and we're just going to introduce that new root module so imports graph QL module dot for route and we'll just going to do empty object for the options right now and I don't know if there's anything else looks like we can pass some arguments into for route if we want to pass anything to apollo server in our case we don't care to now there's two ways that you can use graphic QL with Ness GS you can use the schema first approach or the type first or Ansari not type first code first approach personally I'm going to be going with the code first approach it's the one I prefer and really would recommend you using so we're going to install type graph QL and here we can specify up to create a graph QL schema this is basically a set in there this is basically just the location of the graphical file that we want generated for our schema that way we can see the schema and the SDL standard design language after we take all the type graph type graphic you all resolvers and mash them into a schema alright so there's really we don't need to get into async configurations and all that fun stuff for this basic example we're just gonna hop over to creating our first resolver in graph QL after the setup and again I'm doing the code first approach under resolvers so I'm gonna skip to the part where they have a authors resolver and I'm gonna copy that and start with this so I'm gonna create a new folder here which I'm gonna call cats and this is basically my new I guess domain or you'd say module so here I'm going to create a cats dot resolver TS file I'm gonna paste in my resolver here so I'm gonna say hey cats not resolver we're an import resolver from nest graph QL I'm not gonna have any type associated with the resolver yet I'm gonna comment out the constructor we're going to come back to that and I'm just gonna have a single simple query so get rid of the arguments and we're just gonna say hello here we're just going to do a hello world and our return type here is going to be a string and we can import query from nest graphical also just a reminder for those of you that have not watched my vs code video I'm Otto I'm pouring this with command and then period okay so this is the most basic type graft keel resolver we just have a hello query now I want to use this inside of the app module over here we can create a new cats module dot TS and we can pretty much copy what we have over here well I even we don't really technically have to copy all that stuff I only need a portion of it we're gonna have a single provider cats resolver and we don't need any imports all right we don't need that and then we can import this cats module inside our app module over here we're going to say cats module and now we can we have I guess imported in that cat module with the single resolver that has a Hello query so now I'm going to start up the application and make sure everything works and if this works if we go to slash graph QL now we should see a graphical playground you'll also notice when I started this up if we click on schema that gql that just got generated we can see our graphical schema and again we specified what the name of that is here alright so let's head over here and we'll close 3000 slash graphic you all alright so we have our playground and I'm going to say hello and we get hello back awesome so that is how we can integrate graph qln now let's see how we can integrate MongoDB and we're going to be using the Mongoose package now an SJ s has a special integration with Mongoose that we're going to be using so we're gonna do with the nest GS way all right so let's copy this I'm under the techniques section and we're installing nests GS mongoose package and mongoose alright and then we're just going to be following this so you'll notice we're adding a new import to our application module and oh it's not completing for me so we'll just get this import directly and this is the connection string to the MongoDB database this will work just fine for me but you can change this if you want to point to something else besides just localhost and if you want a different database besides they called nest alright so I guess that sets up our connection next up we can create a schema I'm just going to use the schema that they're using this cat schema and we're gonna go inside of cats here and I'm gonna say cats that's schema ts pace SN and so we have a name age and breed and this is just regular Mongoose we're not doing any nice jazz stuff here all right so next up is setting up our cats module so we already have this set up but we're gonna add this Mongoose dot for feature thing honestly not exactly sure exactly what this does I'm assuming it has something to do with being able to access oops I'm assuming this allows us to access this cat schema inside the service class which we're about to create so import that and import our cat schema all right so we want to basically be able to access our cat schema inside of our resolver and be able to create cats and so the way you would abstract that or I guess set it up in next nest jeaious not next yes is to create what's called a service class which will access this so that is what we're going to create next if we scroll down they have a cat service we can copy come over here cats that's service TS now this is I guess a injectable class and we can access the cat model I'm guessing or schema because we added it to our import here and we need to create a cat interface and this create cat DTO basically these are just typescript interfaces that describe what types the cat has and we'll see we're creating two functions create and find all and basically we could put this logic inside of our resolver if we wanted to this is just a layer of abstraction that I believe makes it easier to test and this seems to be the nest jeaious way of doing things and you can see this is just a wrapper around taking the arguments for creating a cat and creating a cat model okay so let's go ahead and create these two folders and the stuff inside of it so let's start with interfaces so interfaces inside of interfaces we're gonna create a cat interface and we'll call over here and I went ahead and just went to their github sample to be able to copy this and paste this over here and then we're gonna create that d-o-t thing as well so that was whether you need to come back here but it's this path and this has the dot TS extension do you notice everything they kind of they have cats and then what it is and then dot TS at the end so schema resolver module interface DTO I actually not sure what DTO stands for to be honest but if we go to cats and grab that and we're gonna add some stuff to this later but this is what we're going to start with so now our service should be happy with us it is and we can now access this inside of our cat's resolver well we actually need to add an import or a provider for first so cats service and now we can access an our resolver over here so nest es can handle injecting this for us so all we have to say here is cats service and we're going to say cats service and now we can access it here so let's create a new query which we're going to call cats and just going to return all our cats so here we're gonna say this dot cat service dot find all and so what this is going to do is it's going to return an array of cats so I'm going to use the create cat DTO as my return type now we are using type graphic UL here so we can turn this into a input type or sorry you're just an object type oops my vs colliding object type and this is coming from type graph QL it looks like it's important from the wrong place there we go and we can make these fields and now I'm gonna get rid of the Rideau actually I don't know if really is gonna mess anything up I'll keep it for now I don't think it will now strings can be automatically inferred number cannot because it does not know whether the graphical type of this should be an integer or a double or a float so in this case it's going to be an int and it gets imported from type graph QL all right so that is how we do cats that query so we're just calling find all and if we look at our service we can see what find L does it's calling our cat model or schema it's calling dot find it's just executing that all right let's do what the create one will be next so we'll copy this this is going to be a mutation and something I noticed is I think these are just x squared from type graph QL so I don't think it matters if we import it from the nest graph QL or type graphical but I'm gonna import from up here all right so here we're gonna say create cat now we're gonna have to take some arguments because we're gonna be calling the create cat and this requires an age of breed and a name so let me create a new arcs and again I'm just going to import this from nest and we're gonna say I'm gonna call it input and we're gonna create a new type for this so I guess I'm gonna call it cat input and here we're gonna say new folder I'm gonna say new file and say inputs slash cat dot input TS export class cat input and this is just going to basically have all the same fields but instead of an object type it's going to be an input type and again everything's coming from type graphic you'll hear same as we did inside our DTO alright so let's import the cat input and we're just going past that to create nice alright so now we have our create cat and our cats and you'll notice how this works is basically we're just calling our cat's service and passing in any arguments that we need so let's go test this out so I'm going to do start my server and we can come back to our playground now I already wrote out the query but this is well looks like we have our create cat here I'm going to create a Bob too and he's gonna be six years old one thing I forgot to add that I wanted to add was an ID field so let's add that that's gonna be on the return type or the object type create cat DTO so field this is gonna be an ID it's gonna be of type string and the graph QL type is going to be an ID and so we want to explicitly say that all right so now go ahead and run this expect it iterable I think I just messed up the type yeah so we don't actually want to return an array here I want to turn that and input ID is missing oh yeah I messed that up didn't I because I don't want my create over here so my create cat DTO is not really the right type anymore so I'm going to name this create input or sorry cat input and use that instead of the DTO that i used over here this is really just the return type that we're using this is what the cat looks like can even rename this if I want to just cat type so I I did f2 to do that f2 and then you just retype it and then hit enter and I'll rename it everywhere so now we have a cat type our array of cat types and then a single cat type so we want to turn from creating cat alright so let's create a cat our bob - cat and now we can create all our cat or sari view our cats by running this and you can see I created a cat earlier and looks like I create Bob - twice nice alright so that is basically the same server that we built yesterday or a few days ago but now this is the nest GS version the main difference I see with it is seems to be a lot more boilerplate and this is immediately obvious for a small application like we're building here I think this is probably something that is has a very nice structure as your application grows and as you have like a very large project I feel like this is probably a really nice way to structure it but it feels very very kind of complex and kind of boilerplate ish just for a very small project like this just first just like everything is kind of split up into its own classes kind of reminds me of Java esque but overall I think it looks pretty nice and I'm excited to try more stuff with Ness GS and you guys all this goes up on github if you want to check it out and yeah expect some more Ness GS videos in the future you
Info
Channel: Ben Awad
Views: 49,475
Rating: 4.8362832 out of 5
Keywords: NestJS, MongoDB, GraphQL, GraphQL Server, Build a GraphQL Server, NestJS and MongoDB
Id: eHn64NxMwJY
Channel Id: undefined
Length: 19min 5sec (1145 seconds)
Published: Sun Apr 14 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.