Best way to create GraphQL API ?? | NestJS GraphQL Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going i got another nested yes tutorial for you today this time we're at graphql so this tutorial is going to be split up into a couple core parts first we're going to talk about how to generate a project from scratch and how to set it up so that it works with a graphql schema how to write your initial resolver the second part is we're going to talk about how to integrate this with a database using type orm and then finally the last part is all the things that we can do to make it all even better so for example validation how can you auto generate a bunch of this stuff as well as how do you work with database relationships and how to do that with typo ram and graphql and that's just kind of working together so what we're going to build today is a pet store api so imagine if you had a client who had a pet store website of some sort and they need to be able to query who are all of the pets who are the owners owners of those pets as well as having the ability to add a new pet a new owner and so on by the way i do kind of assume that you have a little bit of background in terms of what is an sjs um if you don't i have a an older video where i compare express and sjs if you check out my channel it's also good to have a decent background on graphql itself you know i don't really talk too much into detail of what it is and what are all the different parts of it the main thing that we'll focus on today is queries and mutations so if you have a basic understanding of that as well as arguments and input types so that would be a really useful background to have the documentation on graphql.org learn is is very good place to start reading about this even if you haven't looked at it before and then type orm which i mentioned is what we're going to use for our database integration this one if you don't know as much about it that's fine i think i will have that covered in the tutorial but one thing that would be really useful to know about is the repository api and how that all works so that's the main thing that we're going to be using with nesjs all right finally let's jump right in and let's get started so just like with any sjs project you're going to want to have the end cli installed so you can do npm install dash g at sjs cli i already have this installed so i'm not going to show that but there's a script if you need to copy it once you have the cli installed you can then just do nest new and then the name of your project in our case i think i'm going to call this nest graphql demo hit enter and then it's going to ask you what packager you would like to use i usually pick npm here you can use yarn if that's something you're more familiar with and when that's finished installing we're going to go right into that directory and then we're going to install a couple of other things so our core graphql dependencies npm install and i'm going to paste a bunch of things in here so feel free to pause the video and if you're following along just copy that but as you can see what we're really installing here is graphical itself as well as the sjs sort of integration package between the two and then behind the scenes we're actually just using apollo server which is one of the nice things with with nesjs is behind the scenes at least by default it's still using express so in theory anything that you can use with express you can still use with sjs so let's install that and it shouldn't take too long okay now that that's finished installing i'm just going to open up a visual studio code editor from here and you'll notice what this uh what our cli next cli generated is just a bunch of kind of starter files for us a basic controller a root app module that we'll be working with a lot so the first thing we're going to do is in the terminal here in vs code we're going to again utilize the cli to generate our initial pets module so you can do nest g g is short for generate sg module paths and you'll notice as i run these generate commands it automatically updates my files to wire it up correctly so notice that the app module now has a pet's module import and then if i open up that pest module as i add more things to this model you'll also see that this is going to be automatically updated so for example we're going to do another nsg service pets and then one more thing we're going to do next g resolver pads all right now we got our basic pets module generated with a service and a resolver we're actually going to go back to the app module so what we're gonna add here is the uh the import for the graphql module this is the thing that's gonna kind of make it all work and make sure to add any missing imports so before we move forward let me talk about this uh line 11 really quick if you go to the next js documentation for this graphql integration you'll notice that they talk about uh the option that you can either go code first or you can go schemer first what this effectively means is if you go code first what that means is you first write your typescript classes and from those typescript classes the say yes we'll try to figure out all right what is the schema and i'm going to auto generate that for you so hence that's why there is an auto schema property that says it's going to generate our graphql schema into this file so you're writing the code and the schema the graphical schema is auto generated from that hands code first the schema first is kind of the inverse of that is if you're someone who already knows graphql really well and you you know how to write the schema yourself you can instead take the inverse approach of you write the graphql schema first and that schema is going to auto generate the typescript definitions that nest will need to kind of run throughout the application in this tutorial we're going to stick with the code first approach and that's for a specific reason since we're also going to be using type orm in type rm usually you create classes to represent your database entities so for example if you had a user database table you're also going to have a user entity class that means we already have these classes that represent the schema so we can use this this class to also generate our object type for for graphql so what that effectively means is we can write one class that represents both our database schema as well as our graphql object types so in our case it's not really worth it to go schema first however you can go that route if that's something you're more familiar with all right so back in the code again make sure you have this graphql module for root with the auto schema file so since we just talked about entity classes let's go ahead and create a new file in the paths directory we're going to call this one pet dot entity dot ts and we're going to start with a basic class export class pet and within this class we're going to define the properties that we're going to have again both in our database as well as our graphql schema so usually when you save things in a database you're going to want to have it now an id um we're going to keep it simple we're just going to have number ids and then pets usually have a name make that a string and then maybe there's an optional type property you know for example maybe the pet is a dog a cat a lizard right so right now it's just a basic class the way you kind of tell nesting graphql that this is an object type is by adding the object type decorator okay and you'll see that that got imported from this package and then you also have to tell graphql which properties of this class is going to be part of the schema so you need to add field here another decorator and then when it within this decorator you can specify what type it will be uh in this case numbers will be represented as the int scalar type from graphql you know like i said i'm not going to go over the uh documentation too much but there is a section here where it talks about scalar types so that int that we just imported is this representation of a 32-bit signed integer now with strings it's a little bit easier uh it seems to automatically be able to know that this kind of string is just you know your regular string and graphql so nothing to do there and then same thing for this guy except we need to add a configuration object here to say that this is nullable i think that's a typescript limitation where it's not able to communicate that just because it has this question mark over here it doesn't know that it's nullable so you still have to tell graphql hey this is this is optional so now that we've got our pet object type which you'll see this generated in the schema in a little bit here let's go ahead and try to create a new query to perhaps query all of the existing pets in our system so within pet service let's maybe create a mock find all method which returns a promise of pets pet okay and we didn't set up our database integration yet we'll do that in a few minutes here so we're just gonna kind of mock it out so maybe we have a new pad that has an id of one and a name of it's called mambo all right and we said we're gonna return an array of these so our mock implementation could just be returning an array with that one pet inside and we said it's a promise so let's make this async all right we're gonna enhance this a little bit later to make that more real to actually talk with a database but first we just want to make sure that we can kind of show how to wire all things up and be able to generate that graphql schema so so now let's switch over to our path resolver where i'm going to add a constructor so that we can inject our service in here private pet service has a type of pet service right and again if you're not super familiar with an sjs what's what nasa is going to do kind of behind the scenes is this automatically going to create an instance of that pet service and then inject it into our our resolver here so let's go ahead and write our first query you know maybe this was just called pets and again returns a returns a collection of pets and this will just do our this type of service dot the thing we just made earlier make sure to import our entity and then the thing we need to add to actually tell the resolver that this is a an actual query is the query the creator you'll notice with with nsgs and all this stuff everything is kind of based on adding the right decorators to things one thing to call out here is make sure you're importing query from the correct place so for example vs code actually imported it from the wrong place from me i don't want this query from that's just common i want that query from sjs graphql so that's a very easy mistake to make so make sure you're aware that with this the graphql query what you want to provide in here is the return type so it's expecting a function that returns the type that you're expecting in this case it's going to be an array of pat and notice that uh the syntax here might be a little bit weird right you or maybe you're expecting to write it this way um just know that you got to write it like this because it will you'll so you'll notice later when we generate the graphql schema that's how graphql represents arrays or lists as they have kind of the the bracket and then the the type within that and then one last thing before we get this to work is on the resolver you also almost always want to add a type here as well of what what is this resolver for right it's resolving for pets so i'm going to add that and that's going to also be useful for later on when you're having um you know nested fields nested queries all right now that we have all that set up that kind of concludes our the first core part that i mentioned for this tutorial let's go ahead and run mpm run start dev to finally run our application for the first time so if you did everything correctly you should be able to see that it'll say application successfully started you shouldn't see any um errors or anything in here all right now when you see that message you can go ahead and switch over to your browser and go to localhost 3000 slash graphql and you should see a playground there and you'll notice that if we go to the schema here you'll notice that it's seeing a pet schema our object type as well as the query we just made actually really quick back in the code you should also see that new schema gql file which has that schema right so our auto generation of the schema is now working and let's go ahead and run a test query here of our pets and within there we just want to grab back maybe the name and we should over we should be able to see mambo because we hard-coded that into the response all right so there you go that's kind of the first basic part that i wanted to cover is how to kind of set it all up get your basic schema working and get a resolver working the next part we're going to cover here is our database integration with type orm so we're going to do npm install at sjs slash type orm and also the type 4 and package itself and then our database driver in this case i'm just going to use sqlite sqlite this got just kind of really easy to set up and get up and running for basic development you can of course use your own preference here like if you have a mysql or a postgres database you can do that as well with type of ram type forum even has support for basic support for mongodb if you want to do that so i'm going to hit enter here and wait for this to install all right so that just completed for me i'm gonna go over to our root app module dot ts and i'm gonna add a new import here of this so i just pasted this in but basically you need a type or a module for root at your root module and then within that you just provide the configuration object that you pass on to your database driver so if for example if you were using my sql here you would pass in your you know your mysql host and password and so on so over here we have the type of sqlite like i said and then we also have memory here uh what what that means is it's gonna run our database in memory so as it restarts it kind of starts from a fresh database and this field just says where is it gonna find our entities basically it's going to look for any file that has dot entity in the file name so that's why our pet entity has that in its name and then synchronize through just means that it's going to automatically synchronize our database schema so for example if we were to create a new users entity it's going to automatically add that as a new database table so it's going to make it so that our database always matches our schema on the fly it is worth noting that in production you should not be using synchronize you should instead use migrations which typeom also supports so again this is really just for it's an easy tool to have for development or demos like this that we're doing all right now that we have our typeware module configuration in place we can go back into our entity and we said that we can reuse this class to represent both our object type in a graphql schema as well as their entity in a database so this is the primary reason we're doing the code first approach so i'm going to add an entity decorator here from typeorm and then similarly i'm also going to add decorators for the fields so that we can tell type or m and the database what are these fields so for example i want this to be a primary generated column which will just communicate to sql like that this field needs to be an auto incrementing integer and similarly with this one for the name we just need to have a column here nothing really to configure there again it's kind of the same where if it's just a string it knows to turn that into a varchar in the database same thing here again similarly we also need to tell it that it's nullable so in a database representation this means that this column in the database to type is is nullable this one the name is not nullable so in the database representation it's gonna be not null so it's gonna require that you provide a name on every insert all right so again now our entity is set up to represent both our database table for pets as well as our object type for the graphql schema so that's really useful in my opinion so let's switch back to the pets module and we're going to add a new imports here which is the other type rm module again but this time we're going to do for feature which takes in an array of entities in this case we're going to do pet and if you're not familiar with this what this import allows us to do is then within our pet service you can add a constructor here which has the inject repository inject repository of type pet and we're going to call that the pets repository which also has a type of repository that okay so the only way that you're able to do this inject repository is if you had this type or a module for feature so you have to make sure that whatever your whatever repository for a entity that you're trying to import that needs to be in this array and again i kind of briefly mentioned it in the beginning but it would be really useful for you to know the repository api and type orm so take a look at that in the uh documentation but let's go ahead and just show you some example usage so um now that we have that maybe let's update this find all to be you know the real thing so we're going to change this to this.repository.find so by default what find does is effectively the same as like a select star um from your database table all right so now that we've got this basic find all if we go ahead and rerun our np and run start dev back in our browser if we were if we run our pets query here now we get back an empty array and that's because it's actually going into our sqlite database and saying select my pets and it's not getting anything so again to make that a little bit more real let's go ahead and implement a way for us to perhaps add a new pad to our database so we're gonna do [Music] createpat as a new method and i'm gonna provide an argument here that i'm gonna explain a little bit more in a sec so i'm gonna call this grade pad input and this method returns a promise of a pat and basically so imagine that createpad input is our our dto our data transfer object which has the fields that we need to create a new pet so in this case probably going to need a name and an optional type so just imagine that's going to be what that is and we'll define the shape of that in a second here but first we're going to do cons new pad equals this dot create this dot pat's repository dot create and then we pass in that input and what this is is effectively kind of the same as doing um new path equals new path and then you know you go in into your you pet dot name equals you know your input.name so it that's kind of the shorthand for that is instantiating a new class and setting the different properties and then once we have that we're just going to return this.repository.save that new pad so again you can look at the documentation if you want on what save does but effectively this is the insert into the database and save can also be used for updating so if you have an existing entity that you queried and you make changes to it you can save those changes using save so right now it's doing a squiggly line here with the red and that's because it's not able to tell what the shape of this input is so it doesn't know if it's correct so what we're going to do is within our pets directory here we're going to create a new folder i'm going to call that dto and within that folder we're going to create a new class called create dash input create pet dot input that yes and in here we're going to create a another class called great input create pet input and this class is going to contain those fields that we're looking for so again like we said we want a name here and an optional type now kind of similar to our entity file what we're going to do here is we're going to decorate it with an input type okay and input types and graphql again i'm gonna point you to the documentation here if you wanna check it out if you're not familiar with it but input type it is is an actual thing in graphql that basically just represents your dto so that's basically what they call data transfer objects in graphql is input types so it's a representation of a of the shape of an object argument or input hence the name input again we need to add a field decorator here and another one here and this is kind of the same as like the field that we provide in the entity right so same thing if it's uh if it's a number make sure to do this the scalar int um if it's another class you need to specify that in our case again we need to make this nullable all right so now that we have that defined we need to fix our petservice.createpad to have the right type here make sure to import this all right so now all the red squigglies are gone our application started up again and that's because it now recognizes that oh yes this is a valid shape for a new pad all right now that we have all that out of the way we can then create our first mutation which is creating a new pad createpat and that takes in a great input i'm sorry create pad input keep messing that up and similarly this returns a promise of your newly created pet okay and this is really just calling pet service dot create pet and we're just kind of forwarding that argument now if you're kind of following the pattern here we got a couple of things that are missing to notify graphql that this is actually a mutation so kind of similar to how we have a query up here in this case we're going to add mutation which returns a type of pet and then one last thing is we're going to add args here and this takes a string that we're just going to also call createpet input so let's take a look at the schema on what that generated if i open this to the right here you'll notice that we've got our new mutation that has createpat same name here and then the name of the argument here is actually the name that you pass it to the string so that's why we kind of just keep it the same you know so for example if i call that test you'll notice that our schema gets updated to have test here so that's why you generally want to keep that to be the same okay now we can go back into our browser into our playground and let's go ahead and test our new mutation do mutation and then create and you might need to refresh by the way so it can pull in the schema create path and notice that the argument here is createpad input and we want to provide that with an object that has a name at least mumbo and then the return schema we're also gonna do name nid right so when i ran that now it has mambo created and just to kind of verify this is working it's saving in our database if we go back to our path query if i run that we get mumbo right if i do another one here um dumbo and run our query again now you got two things you got mumbo dumbo and again notice that our id is incrementing right because we have that primary generated column annotation in our entity all right so that's the basics of how to do mutations you might be wondering why did we have to go through kind of the effort of making these input files and there's another benefit to doing it this way because you can kind of just inline define your your input shape in here but having it into a specific input type like this then allows you to do things like auto validation in the future [Music] so for example an sjs has really good integration with class validator to do to do validation so actually why don't we go ahead and and show you a simple example of that you know what if we for example wanted to add some validation on what the the name should and shouldn't be so in the terminal here i'm going to do npm install class validator and class transformer all right install that and once that's installed switch over to our main.ts file here and this is where you can define global pipes and pipes are one of the concepts in nest that you can use to do things like um validation or object transformation input transformation so we're going to add app dot use global pipes new validation pipe now what that allows us to do is we can then go back to our pet entity and we can do something like or i'm sorry go back to our great pet input and then we can do something like maybe we want to make sure that the name is als is only alpha so in in class validator you can add a annotation called is alpha right you see it's coming from class validator and this sets it up so that you can just provide non-alpha names so let's go ahead and test this back into our playground if i go to createpad and if i were just to provide a you know set of numbers here it technically is still correct from the schema's perspective because it is still a string right because we have it wrapped in a quotes but it's not it's not alpha it's not letters so when i run this it's going to come back with a bad request exception and you'll notice because message name must contain only letters right so now we've got validation working just from a simple annotation on our input class so that's why having the input class in its own definition like this is very nice because then you can add you know specific validations in it and you can go into the class validator documentation and you can look in here in the validation is there a bunch of stuff in here like for example um you want to have like a date string is it positive is it negative for numbers min and max you can specify your lengths you can say is it alphanumeric is it just numbers is it a number so basically there's a lot of really useful kind of predefined validation that you can take advantage or advantage of here and all you have to do is specify it as as a decorator on your classes so that's really nice and really quick side note this is one of the things that i really like about sjs is everything just feels cohesive like everything that you're doing is sort of just writing classes and adding decorators to it um so there's like there's one cohesive way of defining and creating things you know to me it's it's really nice you know for comparison if you're doing this in um express you know if you weren't doing it in typescript right then you won't have access to decorators so in those cases you're probably going to be representing your your database classes a certain way you're probably going to be doing a different schema language for your validation like if you've ever used joy or something similar again in this it's just really nice and cohesive in my opinion alright so that's a quick example of validation and just to add a couple more examples of writing queries here maybe let's write a query for being able to select just one path you know maybe by id so let's maybe add in our pet service a find one which returns a promise of a single pet and in here we're going to have an id which is a number and this is simply return.this.repository.find1 or fail um you want to use find one or fail here so that it actually assuming you want it to fail if it didn't find um the pet that it's looking for if you just do find one it's gonna return an empty array if you didn't find anything so most cases that i found you usually want to use find one or fail and then you just pass in the id here all right and then within our pass resolver we can add a you know we can add a new query here maybe let's call that getpat and similarly this returns a promise of a pet and we're just going to do this.petservice.find1 and passing an id in this case um you can like i said you can also define your arguments kind of in line here you can do args and then provide a string for the name of that arg i'm just going to call it id and then you're going to have to provide a config here so that it knows what type that is again we're gonna do our scalar and i'm gonna call that id and it's a number and then pass that down here anyways same thing here query returns pet so let's go ahead and go back to our playground and test that out remember that i said our database is running in memory so every time i'm changing the code it's actually emptying out my database so um if you run pets right now it's going to be empty so we're going to have to do a create pet first and again we got to have an alpha name here maybe you're smart and you just you know did the string version of the number so let's do 11 which has an idea of one so if we now run our new get again let me refresh this so it gets the new schema get pad and that takes in an input of id and i'm going to provide one and i want to select the name if i run that i should get back 11. all right so there you go there's another example of a query if you're still with me here the next thing i want to cover is what are some of the shortcuts that you can do to generate a lot of this stuff for you in as little time as possible so i walked you through kind of doing a lot of this from scratch because i think it's important to know the fundamentals right you should know how to write this from scratch if you if you had to but once you kind of understand um how to set up your entities and your and your input types and your annotations for queries and mutations once you know all of that and you're pretty familiar with then you can in the future just use the nscli to auto-generate a lot of the boilerplate for you so let's go ahead and take a look at an example for that so as an example let's imagine that we're going to add another thing to our schema of type owner right every pet needs an owner with an scli you can do something like nsg resource and then provide it with a name of your resource in this case we're going to do owners and when you hit enter there you'll notice it's going to give you a couple options here since we're doing graphql code first we're going to select that and it's going to ask you would you like to generate crud and entry points so this is basically a boilerplate generator for crud stuff we're gonna go ahead and select y here so you can see what this generates and again notice that it generated a lot of those things that we kind of did from scratch right it's got a new module a resolver a service and a couple dtos as well as the entity so let's take a look at those so notice we now have a new owner's directory here and it's got a resolver that has basically all of the crud stuff there by default like it's got create it's got find all find one and then update delete write your basic crud and it's got everything wired up for you so again this is really nice if you kind of understand how to write these things from scratch already um it's going to save you a lot of time the one missing piece here is if you go into the owner service you'll notice that they all just return strings right because it doesn't know how to hook it up to typo rm so that's the only thing that's missing so we'll fix that in a little bit here but let's take a look at the entity that got generated so again it creates your object type for you and similarly there is an input type for creating and updating all right so back in the owner entity why don't we go ahead and turn this into kind of the real thing so let me just delete this what we're going to do here is kind of similar to our pet entity we're going to turn this into a database entity and we're gonna add an id field here same thing with the number and actually just to save some time let's open up our pet entity and just copy this part sure let's also give this owner a name import these missing decorators all right so now we got a new owner entity that has an id and a name let's go ahead and set up some database relationships so owners should have you know pets um so let's define this real quick from a database perspective this is going to be a one to many right an owner can have one too many paths and within here we need to provide a little bit of configuration first kind of similar to you know these field decorators we have to tell a type or mr database what is the type of this field we're going to do pat and then we also need to say in the pet where is the kind of matching field right where in this case it needs to be pet that owner and this is going to be red squiggly because we haven't implemented that yet so we'll we'll fix that in a second here but don't forget your graphql annotation and a similar thing also requires a function here to specify what the type is so in our case it's an array of pad so it needs to match this type and we're going to say it's nullable because maybe initially you don't have any pets on the other side of that we're going to do in the pet entity we're going to add an owner field of type owner so this is the inverse so this is going to be a many to one so similarly we need to provide the type as well as within that object what's the property that it kind of maps to so in this case the thing we just defined was owner bad pets right so this is kind of saying in this entity owner that pets maps to that relationship and pet that owner maps to this right so it's kind of a lot it's like uh how do i explain this um like the field has to exist kind of as a bridge between the two entities that's a terrible explanation but i can't come up with anything better right now and again we'll add a field here and it should have a type of owner so with these many to one and one to many annotations the way it works is when the schema of the database updates it's actually gonna behind the scenes add an owner id to the pet table and that's how it's gonna be able to do the relationships so if you were from a database perspective if you're trying to find the owner of a pet you know you can do the join from you know select pet and then inner join on owner id equals owner.id so behind the scenes there is a new owner id column that's being added to our pet table you can also specify that here if you want to have access to it which we do later as you'll see so i'm going to do column and field here and it's going to have a type of it scalar int alright and i think we need to also update our owner dtos here uh we're gonna copy this name so the create really this should be a name um just a field all right so now that we kind of have those relationships in place what i want to be able to do is in our query i want to be able to do something like get all the pets along with their owner so if i were able ideally i want to be able to do something like this like who is the pets and who is the owner of of each of those pets so that's what we're gonna implement next so before we can do that we need to fix some stuff in our owner's module um you know again to kind of make it real we need to add our type rm module for feature here and it needs to have access to the owner repository imports that's another easy thing to mistake all right and then within owner service we can add a constructor where we can do our inject repository again add any missing imports all right and then let's go ahead and implement some of these missing implementations so for example i'm going to leave update and removing uh maybe that's something that you as a viewer maybe if you if you're following along you can implement that yourself as a as a practice challenge okay so if we go back to our resolver um that means that we can now do a create owner mutation and we can also do a find all owners and find one owner and these two does not work yet one thing i want to call out real quick is again notice that we have a find one implemented in an owner's service we actually want to be able to share this method with our pets module so that again we want to be able to find the owner of each pet so to do that in sjs you can have providers be shared if you add it to the exports array here i'm going to do owners service what that allows us to do is we can then go into our pets module and add this as a new import here we can do owners module and what that allows us to do is if we go into our pet service we can add a new import here of owner service and that means that now we have access to those methods in the owner's service so maybe we want to implement a get owner here which takes in a owner id and returns a promise of an owner so we can do return this dot owner service dot find one and we're gonna forward that owner id and don't forget to import your owner entity all right now that we have that in the pet service we can then go to back to our pets resolver and we can add a new query here for owner which returns a promise of an owner and we're gonna do return this dot pet service dot get owner and we need to pass in some kind of id here and one thing that we can add here is a parent decorator for pet and then you can do pet dot owner id don't forget to add your query actually this one should be called resolve field returns a type of owner all right so let's talk about this a little bit because i just typed a bunch of stuff there the parent here is something that you can use for if you think about it from a nested query perspective right for for example what we're trying to do is here the owner of kind of this sub if you think about it as like a sub query the owner of that the parent of that is this pet object up here so we want to be able to pass this id down into that query so that's kind of what that um parent is doing is it's grabbing the current pet that you're on let's let's pass down the owner id of that and again we have access to that owner id because we added it to our owner id here and our pet entity and then resolve field works again because uh that's kind of the we're saying we're resolving this kind of nested field so it's kind of like a think of i could think of it as like a way to define like nested queries that's what result fields are for and this is also really important this is why we needed this of pet this is what enables us to do this resolve field with a parent of type pet alright now that we have all that in place um there's actually one last thing that i want to make sure we don't forget in the createpad input we need to add the new owner id number field here which is not nullable and this is so that when you at the point that you create a new pad it's going to require that there's an owner right and in a real case scenario you don't want to be able to add pets and not know who their owners are all right so with that in place we're going to go ahead and run npm start dev again and once that starts back into our playground again remember that our database is getting wiped each time because we're doing it in memory but in my case i need to do a create owner first right because now you can't create a pet without the owner and this requires an input of at least a name i'm just gonna provide my own name there and we're going to select id and name all right so there's me as an owner with id1 now in our create path we can enhance this to pass in the owner id which is one so this is me saying i have a pet whose name is 11 so let's run that so now we've got a new pet created and if we go into our past query right we were saying that we want to be able to query pratt's pets along with their owner so if we run this now you've got kind of that nested relationship happening in our query or it would acquire your paths along with their owners all right so that's pretty much it really that's what i wanted to show in this tutorial you of course can also implement the inverse of that of being able to query from the owner's side be able to query their pets like maybe you wanted to do something like owners and for each owner you want to pull the name and their pets name right so we didn't implement that but that's something if you're following along again i would challenge you to try and implement that yourself and then we also didn't implement update and deleting to kind of complete the crud again i'll leave that up to you as a challenge alright so that's it guys that was kind of a long tutorial hope you found some value in that i would definitely appreciate any feedback that you might have let me know in the comments and a like or a subscribe or both is also highly appreciated it helps um it helps youtube tell that this this content might be useful to be found by other people and with that said i'll see you in the next video thanks [Music] you
Info
Channel: Marius Espejo
Views: 10,734
Rating: 4.9685864 out of 5
Keywords: nestjs graphql code first, nestjs, graphql, nestjs graphql typeorm, nestjs graphql, graphql nestjs, nest graphql, nest graphql typorm, graphql tutorial, graphql vs rest, graphql react, graphql react tutorial, graphql apollo, graphql apollo server, graphql api, javascript tutorial, nestjs tutorial, nestjs graphql code first example, expressjs nodejs, express js api, typescript, typescript tutorial, nest typescript
Id: geYvdbpo3cA
Channel Id: undefined
Length: 53min 34sec (3214 seconds)
Published: Sun Feb 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.