NestJS GraphQL Complete Course | NestJS - GraphQL - Postgres - relational resolvers and many more.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to another tech video on my channel how you guys doing more than anything i hope you guys are healthy if you watch my draft will master course in youtube you may remember we have done like kind of a pet project over there so that uh course we there in in that course we discussed like um what is the graphql why we need it and for what we can use what are the resolvers what you mean by schema and bunch of concepts and theories with some some uh sample pet project but in this video i'm going to show you how you can create real production grade graphql service but one condition i'm not going to explain theoretical concepts and fundamentals behind the graphql in this video right i'm not going to explain resolvers i'm not going to explain schema i'm just going to create it because all those explanation very simple way step by step i have in that graphql master course right so i have a link here and also i have a link in description below you can go there and watch if you don't know about graphql and if you're understanding the graphql is cloudy but if you're good with graphql you can just jump into the course condition number two or rule number two i'm going to use a nest case for this uh example because ness js is right very structured language and framework so you can use your service very opinionated way so therefore i'm not i'm going to use an sds in this course for this one if you're not familiar with the nexus i have other course other video about nesters and explaining how exactly this js works and how we can use nexus step-by-step guide to create next days-based services right so you can watch that video also if you are not familiar with the next days right so therefore i'm not going to explain graphql or even nexus in this video but i'm going to teach you how you can create real production great service okay what we are going to do we are going to implement full service with all the operations and with um let's say for example uh employee and a project right one-to-many relationship right in both side right one too many and many one relationship and also i'm going to explain you the third operations all the current operation how we can implement how we can expose those uh through graphql and everything including a database because to discuss the mutations we need to use a database right so in my next year's video i use uh mongodb business yes so therefore in this video i'm going to use um else the sql database with the sds which is a typo r in that case i did teach you uh mongodb with mongoose and i'm going to teach you uh a sql database with the typo rf that means we covered every aspect of nesjs and also crafty well right one stone two bits right so let's jump into the project so here first i'm going to create a project uh nest new let's say employee graphql service right you don't have to name the project in this way but i'm going to you name this project this way because then it's easy to uh see right so now it will ask what is the package manager so i'm going to say it's npm right it will create a npm project right so i'm going to show you something else also today that is with the next days in my course also we implement everything step by step right today also i'm going to do that but once you know that fundamental once you know uh once your family about these concepts and the fundamental about the language of the framework this is providing kind of a single out-of-the-box command where it generates all the boilerplate codes to you right i'm going to show you that but i'm going to use in this straight away within this project right so i'm going to uh create a different project for that so next new sample right so i'm going to use this project to demonstrate that okay if it is easy why i'm not going to use that because i'm a strong believer every software engineer should know and about the fundamental right once you know that once you master your fundamental why you need a resolver how to create a resolver why need the service how to get a service then you can jump and use all those tools and techniques and the code generators and everything right otherwise you will do that project for now but with the long run you will go to stock all right so therefore i'm not going to put you into that pit hall so i'm going to teach you everything from the beginning but let me just show you this magic okay so okay so you can see here uh you have a uh default code right visually we see this right so now i'm going to use i'm going to go to this terminal and give a command next generate we know all those from my our previous course resources or else short form is rds and let's say employee right so you will see here if you use a http service you need to use a server controller service repository that's a good practice right but if you use a graphql you need resolvers you need schemas you need entities all those things right so what this does is what this ca light does is it create all those templates in the boilerplate code for you let me to show you so you can select whether it's a rest api or a graphql code first order schema first i'm going to explain what this is and whether it's a microservice or a web socket right so let's go and see is a graphql code first and now you will see it generate it will generate bunch of uh boilerplate codes for you right so you can see here it create the employee directory and it has employee entity and the the kind of template and also it has a create and update dtos and then it has a mod it created a new module and it create the resolver for you right in the resolver it create all the club operations find find all create update delete right and also in the service uh it created all those um the i mean a boilerplate template for your service right you just need to go and implement this right it's a very easy straightforward right so but i'm not going to use this today because i want you to know everything from the beginning so therefore i'm going to create these at least for one module then i'm going to show you um how to use this right so let's go back to our real project so employees graphql service and open this in the code right so this is our real project what we are going to work on today okay so [Music] here we have um this okay let's let's do the the basic thing what we usually do right we don't need this generated uh controller so i'm going to delete this and so you remember in our first project also we did this we don't need this service so i'm going to delete that as well and we go here in the uh module of modulo ts and now it of course it will give a few errors for you right because it's using app service and app controller so i'm going to remove those two right so also we don't need this and we don't need this right because we are going to build everything from the scratch right so now i want to make sure uh everything is working fine after this deletion so how we do that in p.m run 10 p.m run start there okay so if there is no error right no errors so we are good right great so let's get the dependencies installed we need few dependencies here because we are going to deal with the graphql so we need the graphql dependency as well as we are going to deal with the database so we need the database dependencies as well right so uh this easiest way is you can go to sds documentation and here it says graphql quick start right you go here and it tells you what are the dependencies you need right so i'm not going to explain these dependencies each and every because we already use these dependencies and i'm going i'm going to install this so nexus graphql is a kind of a wrapper for graphql and also we need apollo server experts as well so in last an sj's video i use mongoose uh dependencies in the driver but here i don't use that because i'm going to use sql database with the type of rn so we need these dependencies as well right but i do i'm not going to copy this mysql 2 because i don't need that because i'm going to use a pro stress okay so let's get the dependencies ready first okay so that is done so now let's install the uh database dependencies and instead of this i need pg other than this we need to be a postgresql as well so let's get that done okay good so now we have all the uh dependencies ready just need to implement right so first we are going to you implement the uh employee resolvers and all those things so you know what there is always so let me to generate those okay first thing is first let's generate our modules resolvers and services right um i'm going to use the cli for that next generate module and that is uh employee module right so it will generate the module and link it with the app module good and i um okay so let's generate um service nest generate service which is an employee service and do not generate spec file right because we are not focusing test here okay so and also generate resolvers was nest generate resolver employee resolver and also do not generate spec file okay good so now you can see here we have employee uh module right and also we have employees resolving the service and it's uh imported to the provider as well right so we are good there right let's create the employee entity first and i'm going to create a directory here called entities and there i'm going to add employee entity employee dot entity dot ts and here i'm going to export class employee and i'm going to quickly create this so you don't have to wait and see okay good so now we have um this entity right so let's configure graphql into our service how we can do that we need to go here and we need to import graphql um like module here so let's import graphql module graphql module dot forward right so here we need to do a very important thing we need to select whether we are going to schema first or we are going to code first right i mean you can skip either both but this will be very easy so what does that mean if you are really good with graphql you can create your schema and you can ask nest to generate its own all the entities for your schema that's one way of doing that right the second way of doing that you can create all entities right the first one is called schema first you can create all entities you can create employee entity you can create project entry whatever entities you want and then you can ask nes to generate graphql schema for that usually this is the most used one the reason is uh most of graphql servers we need database support right we need to mute it or we need to create these records and database when you want to create those records database you need to make sure you need entities for that right so whatever the award framework you're using you they need a schema so if you go with schema first you can create that schema and you can reuse this schema to graphql as well otherwise you need to create an employee schema separately employ entity separately for graphql and you don't have to do that and you can um sorry you need to um schema for a graphql and entity for database so you don't have to do that you can just design your uh you can reuse your graph your database uh entity in order to generate graphical schema for you okay so i'm going to take that approach right but if you want me feel free to uh take the schema first approach so it's explained the documentation how to do that is very straightforward and everything else remain unchanged doesn't matter what you generate and what strategy you use okay so um let me to get the code here so if you go in the graphql right and the quick start so you will see uh it i mean here it explained everything whether you go with the graphql schema first two um code first right so i'm going to go with the code first right so here therefore i'm going to copy this line and i'm going to paste it here right so you can uh make sure you import missing imports okay so here this is your graphql schema file right so let me to rename that called graphql schema dot gql right so graphql schema dot uh gql okay so what this does is it will read our entities and then it will generate our uh graphql schema file okay so now how graphql knows what are the entities it need to use or what are the models or what are the classes or whatever your language right i'm seeing a little different of vocabulary so whatever your class file it need to use to generate the schema okay so we go here the class we created and we mark this as object type right we mark this as object type so now what the nest does is when startup it go and tree what are all object types let's take those and generate its uh graphql schema for those okay so that's how it works okay so let's go and implement our service and take it from there so you go to employee service here and i'm going to create a first method um saying find all right so i'm not i'm i'm not required to say um it is find all employees because it's of course it's employee service and now um i don't have anything here so but it's fine for now so you go back here to um resolver right so in this file so here you're going to create again find all okay and in this final method you need to make sure you mark this as a query right so you mark this as a query so there make sure uh you do it right because usually it implement from next j is common you need to make sure that is not from the next day is common and that is from the graph keyword right we don't need uh uh common query we need the draft qr query okay so and also you need to tell here what is the output type here the output type is a employee okay and also this resolver is for employee right we need to make this a basic configuration here okay so now most important thing in employee entity we need to tell what are the fields we are going to include for this okay so here we can use field decorator right so field decorator here right so with this field decorator we'll tell what other fields you need to include into your schema right and also if you need you can set the properties here for example let's say city is not must it's optional you can set another bill is true and also like for example if you have a some different data type if you want to map the specific data type you can map the type as well so that is also you can do okay so let's run this from this stage and whenever error comes we can fix that so start there okay so you can see it executed properly and if you go here the graphql schema you can see it generated the type and the query by looking at our entity right so looking at our entity it generated its query and also type and if you go here now you will see the playground as well localhost 3000 [Music] crafty web right so you will get the playground right and if you go here schema you will see this as well okay so we are good so now what we need to do is we need to configure database for this okay so for that we go back here to app module and we need to import and we need to import um typo r okay so here we need to import typeorm module right and also it's for root and that's also need um configuration right this is the configuration i need i just i type because i don't want you to like looking at here while i'm typing so here you cannot do something wrong right don't worry about that so it says uh it is wrong and you can see here in the help text so what are the database type it support right this type of mysql merit ap and postgres um like cockroach sqlite mysql sap or so many databases right so and then my database is localhost this is the port and nairobi is my username and this is my password and employee is my database and this is very important right what this tells you um so here you need to i mean we tell type of ram go here look into these output files entity dot is going to do this and take those files and create the tables for us right so that way you don't have to go and create tables right tables for engage primary is everything it will generate and this is also important synchronize equal true right so synchronize true you only use dev environment do not ever use there's no production right what it does is every time when you run this service it will check your entities if something is mismatched it it's going to modify the database i do not use this on a production because if you use that it will i mean to disaster on some terrible things to your database maybe even drop tables right so don't do that so then in the production we are using something called migration right so i'll explain that later but for now just understand do not use this right so now i explain why we go with the code first because then you can share same entity uh between database as well as a graphql right so that is what we are going to do now and here we are going to mark this as an entity right here we are going to mark this as an entity so now so this same uh typescript entity we are going to use by database as well as um graphql right so here i'm going to mark this as identity column right so i'm going to use generated identity right primary generated column i'm i'm going to tell it's a uuid all right so here what happens is in this spring field every time when you create the record database will generate uuid for you as a primary key but you need to keep in mind so here they use uuid version 4 right which is likely to conflict so but if you use if you want um something like a uuid version one you can manually generate and set that you can then you need to make this as a just a primary column right so without saying you can use this decoration right so for this one without just saying generated right so in that case um it will mark as a primary key but you need to set the value manually all right so everything else i'm going to just mark as a column that since i'm i have set this as optional field then i'm going to set this nullabel equal true here as well and i'm just just for your demonstration purpose right so you don't have to i mean think about it much okay good so now we need to take these values from uh database right so for the service so for that i'm going to use constructor in the constructor i can use something called repository injection what the repository injection does is the type of rna comes with something called repository api so this repository api it comes with the standard inbuilt methods like a fine final update delete all those uh operations right so you don't have to implement your own repository you can use this repository api inject into your service and use that so it's the same as if you're coming with the spring or spring data is printed jpa so we have a repository um apis right repository um interfaces so you can use that it's the same technique same principle nothing different okay so here i'm going to use inject repository and my repository is type of employee and private employee repository it's a type of repository and employee okay so this is what it does okay so now here you can tell i want to return promise promise of employee array okay and from here you can directly use the in this import improve the return this dot employee repository dot fine okay this dot employee repository dot fine right so this is good if you want you can make this message with this async and that's everything it's good so now go back to our resolver here right in the resolver we can inject our employee service to the constructor okay here we can use this dot employee service dot find all and that's it okay so this is a like that so here we are saying we are returning employee so therefore you don't have to specifically specify what is the type of return from this but we need to do one thing here that's a good practice for example think like this so you have employee resolver here right and we are going to create a project resolver also right because we are going to learn about one too many and many to one relationships right so if you when you do that there will be two fine nulls and when you go to the client the schema it will override each other right you only see one fine note so therefore it is a good practice to name this right it's a good practice to name this uh with the specific name find all employees right so or else you can say get all employees because if it is a all there is nothing to find okay so this will be good okay so let's run this and see what the status and whether we did some mistake here and why this is in the right okay i miss mine here okay good okay giving me some error uh congresswoman dependencies is employee service uh okay so the problem here okay so we missed some something um small mistake so that is here we are using in the service um this employee repository as an inject repository right but to do that if you want to do that you need to make sure you come back here in the employee module you need to import that okay you need to import that so here you can mark imports right again uh type or a module dot for feature and we did this in a previous video as well so i'm not going to uh go in detail here and we need to pla pass what is our entity we are going to use with this one right so employee right because otherwise you cannot use that that's why it says i cannot resolve this okay now let's see what is the status okay cool now we are good so if you go back to let's go back and see our database first and if you go to the table if you refresh that so it has created the employee table right so now table creation done so if you see columns you will see all the columns you have id first name last name and city and everything that's good right so that's good so if you come back here you can see the type employee the query right but if you try to get this query let's try to get this query and my query is get all employees and i need id first name and last name but of course this won't work because um uh it's returning null value because it doesn't exist right so if you want really uh to see whether this is working so what you can do is in the service level here instead of doing this you can return something hard-coded right so employee okay let's do little modification and see whether we can see something okay so you come back here and run this still uh the same error so let's see maybe we missed something else let's go back to resolver yeah it's returning not the employee it's an employee array so it's time to return single employee okay these are this i mean silly mistake you can do and let's try to see okay now you can get that right so but this is not what we need so we don't want to uh do something like this so i'm going to uh delete this and then keep our method ready okay right so now in order to get something from a database right we need to create that in the database right so therefore i'm going to do the create method right so therefore i'm going to the create method so create and here that also return promise of the written single employee right okay good now this is very important this is somewhere a lot of developers do some mistake they still think graphql on a rest paradigm right because in a rest world you can use same entity to create and uh get delete update for anything but document doesn't work like that graphql doesn't work the same way but the rest work because in the graph it has its group input object and the output object separately so therefore you cannot right keep in mind you cannot share the same entity in between in a input and output in other words you cannot use the same entity to query or query and mutation both right so mutation you have to have a separate entity for query you have have a separate entity i mean you will think this is so stupid then this is like um this is something wrong but think like this so for when you create you may don't need ids because id is auto generating right and you don't need you may don't need certain calculation like let's say you have two values and calculate third believe in service layer you don't need that so therefore it makes sense using two different entities two different classes to create and get or else queries and the mutations in other words also you can put certain validations when you apply validations if you apply the validation onto the global one single class then for example when the user send that you may don't have that right so therefore the validation will fail or else sometime in come from the database you may don't have that so database the validation would fail so when you have two different entities two different classes for queries and the mutation then you can um easily handle this so therefore let's create a dto okay so let's create detail here for an employee create right so you can mark this as a create employee dot you can get markers if you want input dot ts to mark this is this is used to input because dto has two different uh ways because one is like kind of uh you hold the data but also d2 has a responsibility this one is an input so no one is come back and change this export class employee create tto okay so let's go back here employee entity copy everything and come back here and paste okay so um make sure you import these missing inputs but you don't need these database related stuff here and these you don't need because this is nothing to do with the database right so therefore um this is enough right so last one remember we mark as object type right but this one we mark as input type right so we mark as an input type we use input type decorator so now graphql knows okay this is going to use mutations right so this is going to use for mutation and this does not going to use for queries and if you really want to expose one model into your front end you can do that easily you can create a classic dto not the input dto and you can use a classic dto and you can let them to send that detail and you you can use a destruction and to get the value from there to here right i'll show you that as well so don't worry okay so let's get this ready okay so now you have this and now go back to a service here i'm going to use employee and this is type of employee create dtu okay and you can do something like this i'll show you two different ways right because let's say you want to expose single dto to input an output so that front end developers will feel good because they can use the same model to like here and there so in that case you can use something like this you can hear you can create a temporary variable and assign like this this dot employee repository dot create right so you can get this employee to create this right so you can use this variable i think i have a small mistake here right this one right so this will do what this will do is this will do like uh employee emp equal new employee and emp dot id is this emp dot first name is this everything whatever the ddos you coming from your front and right you can that way you can use share dto and then you can use this dot employee repository dot save and you can pass this created object here right you can pass this created object here and you can return this okay but i mean if you want you can do that that is perfectly cool but if you i mean if you use separate dtos you don't have to do that you can directly assign but otherwise you can do this okay right so let's go back to your resolver right so let's go back to the resolver and include this so here i'm going to use create and here i'm going to you mark this as a mutation right i'm going to mark this as a mutation right so there also i need to mention what is my input type so this typing input type is an array not an array right and also i need to tell the name it's a good practice to uh name your so you can say create employee so this is optional but it's good practice to do because otherwise it will confuse like if you have a two different method from the uh multiple resource resources you expose then schema will not reflect that properly right so it's always good to do this okay and now this dot employee service dot create and now you need to pass some sort of argument here which is coming from the front end okay so what you can do is you can use arg right so here as you need to tell what is you're going to do and then the variable for this and the type of the variable right so employee create dto okay and you can pass employee here and you can directly return from this okay so let me to explain this little bit so this employee i mean giving the sensible name here is important right if you i mean it's good if you name this as employee create dto itself okay so i'll show you why this is important so when you run this right we know like the nest will go through all our entities and object types and input types and it generate our graphql schema file right so when that happens ah if you go here you will see right it create input using employee create dto it create input using our um entity what we created right not the entity but the dto we created you can see here it named this as a variables employee that is the same name what you giving here right so let's say if you mark this as employee input right so you will see this name is different this name is changed to employee to employee input because that is the same name what we used there okay so you need to keep that in mind where that works okay cool so now let's go back to our schema and if you go to the schema file schema here you will see now it has a mutation and also it has a create and this employee right so let's go back and try to create employee so let let me do this little bit uh enlarge for you all right so mutation okay what is the mutation i'm going to use create employee mutation and i'm going to use the employee input right if you create an employee there so this will look like a employee right and then i mean that then it will it will make sense right i mean it's it's giving a sensible input right so let's say first name is nairobi and then last name is alphonso so uh this is my value so then i need to tell uh what what i need to see from output object i need to see the id and also the first name right so then i can get the id okay so let's run this and see what happens now it says employee dto id is a required type string was not provided okay so let's go back here right if you go employee create dtu right so here it says um employee in the reality we don't need id here right because id is something coming from uh database automatically right that is the important thing that's why i put the id over there right if you compare this is the employee uh create and this uh employee dtu right here you must have a id but here you don't need to have id right that is why i put the uh id there to make sure um you understand the difference why what is the importance of having input and output classes separately right so that is the reason behind that so let's uh run this so now it says okay it created uh this value with the nairobi so let's go and uh query the database right i mean this only this time i'm going to show this because otherwise i'm not going to waste time by querying database because we can use the uh get okay so now you can see you have an irobialpon so it's engineer and nc so with the id is generated okay cool so now even here if you use the get all employees now uh nairobi will come along with its value okay good so that's it for a creating object value but now we need to let's see let's create a something some different employee as well okay so john doe okay so let's put the same as a rest of the value separately and then you have a two different um employees right good so now we know how to create a one uh object and we we learn the mutation and we need the query as well right and it's clean and clear you don't have to worry about anything it's very straightforward and opinionated right now we need to focus how we can work with multiple uh entities like for example relationship one-to-many relationship right i'm going to show you one-to-many relationship then main domain relationship is nothing right so it's it's just one more mapping so i'm going to show you that so what we're going to do is employee will belongs to one project but the projects may have multiple employees so one-to-many relationship in the sense you have an employee and a project like one project uh will carry multiple employees so that mean employee table we can have a project id so then we can manage this relationship within the same two table still we can do that right so to save some time here in this case i'm going to use i'm going to use um that tool what i demonstrated before to create all the required artifacts for me right in that case i just need to go and uh update those artifacts right so how i can do that so um nest generate resources and project right i want to create project resources for me and now uh next we generate that and it is a graphql code first approach yes i need all the schemas i forgot to use uh skip spec files so it created that that's fine we can delete separately okay so you have a dto create and you can see create project and update project dtos and entity you have a project entity and also you create a project module and resolve expect yes we don't need this and also it has service spec and we need we don't need that either right so service spec we don't need that either okay good so now if you check here uh it has an employee sorry now let's close this employee for a moment right so it has all those um project module and it's uh in imported project resolve and the project service and also uh app module updated with uh project module as well right so you can see the project module is here right so everything is good we just need to uh implement this so let's go back to uh dtu project dto so let's create the entity first right so i'm going to quickly create that so you don't have to waste time to looking into this which i'm going to fast forward because the same thing what we have done over there okay so you can force here if you need to understand this separately but the same thing so we have id name and a code for a project here i use the number as purposely to show you that if you need a number you can switch into scala type like this because number is not the graph you will type so you need to map whatever you feel with the existing scala type so um so that's it from here and now you go back to um so let's create project entity project create project dtu also in the same time and so this one i'm going to delete this and again here you don't need id field at all and you don't need the columns okay so that's it i'm not going to use update for now but i'm going to use the same dtu for create and update both okay so you go back to uh service in the service you need to inject the constructor right how we can do that so inject repository and the repository type base project okay so for do this we need to go back in here update the project module with imports all right so we are good come back to service and like let's let's first implement the finder okay so the return type is promise of project array and find one also i'm going to implement i'm going to quickly implement this and fast forward so because this is a um i mean a similar thing what we have done and you can post the video and read if you want later so in this case i'm going to i'm going to make it a little familiar i'm going to make this as a project and here i i'm going to use the same syntax that we used before so you won't get confused but you can directly use the same keep that in mind if you want and i have that comment because then if someone is don't watch the video but uh refer the code in the github it may make me confuse why i am doing this so and let me to tell this explained in video okay so otherwise they will see why i'm doing this okay okay i'll come back and update changes update and remove methods so let's go back to project resolver quickly and here also i'm going to quickly update this because everything here what we have already discussed so i'm going to quickly update and fast forward and then you can just see this right the intention is to show you how to do the one too many and many to one relationship okay good so now go back to um update project because you understand the create project don't need the id but update update project always need the id because we need to tell what project we're going to update so here i'm going to copy this and also [Music] um it is here but i'm going to i mean this is i'm not going to explain this i'm going to delete this part and i'm going to add the id which is a string and then it is a field right so i'm going to add the field here okay good so now everything is good um we'll see if we made anything mistake here so this has to be [Music] so update one this has to be a string okay that's what it complains me right so now everything green so now we are good shape to go so let's um let's do this and see whether it's working in p.m run okay good so now come back here and let's see the schema right so you can see so this is what i was telling you in the query you can see get all employees and here the project right so i mean if you use find all for both methods and you if you didn't put the name so this will have a one find all method find no query right so then i mean you cannot use you don't know whether it's it's a final employees or find out projects so that's why uh the dedicated name is important right so here what you're getting is the name you you added in the in the resolver right here all projects right so if you say um like get all project here right so it will come back here and update right get all employees and get all projects right click on project right if you if you had only the find one without specifying the name then only one find one method will be here okay so let's try to uh create project first right so mutation and so what i'm going to do is create project and so i want to pass the project and so i'm not going to pass the id but i need to send the name right i'm going to name send the name as a rapid and then i want to pass the code the code is let's say 100 and from the output field i want to use id because i want to see what is the id and i want to see what is the name and just to just to say this right so just create this and you've got the project created so here on the get all employees so let's uh create other one um let's say create a new separate one so then you won't get confused as you will see the project is created right so now we know now we have a service graphql server where it can create employee find all employees and everything and also it can create a project and uh get the project query the project as well right so now let's build the relationship one to one and one to many relationship in between these employees and project and let's see how graph we will handle that so let's stop the project and see how this works so let me to close everything and open two files one is my project entity and other one is my employee entity okay employee entity right so cool so uh let's get this side by side so you can easily understand the difference between these two so let me to close this as well okay and let's close this and get more space okay how we can build this relationship this is very important you understand how this relationship works right so i have seen people uh even with the hibernate and even with the jpa and they really struggle to put this annotation to write wiring up to understand where one one too many goes where the minute one goes right this where to put this decorator is very important and also if it is a java we have to put this annotation is very important right so i mean it's same principle applies everywhere you just need to understand that properly okay let's try to figure it out so now we have employee and project so that means one employee only belongs to one project but one project will have many employees right so if you come and sit on employee side you will see many of us will go into one project so that mean the first start with the u so that means many to one many of employees go to one project so that mean the the decorator should goes as many to one because in the employees there's a mini if you sit on a project site you will see one project will cover many employees so the project itself is a come first so that when you on a project site is a one one two many okay so that's how it work and let's go and create a variable here employees is type of employee array right so now let's go and put the annotation here the decorator here i always say annotation right so decorator is one too many right because why we are on the project side and we cover one project for many employees so now you need to tell the data type right so that is employee data type and then you need to tell employee how we can recognize this employee would be employee dot project right so this it out because we didn't create that yet so that's fine okay and you come back here in the employee side for employee side you need to create the project right you need to create the project right so this is a type of project right so in this side many of us we have an employee side so that means many of us will go to one project so that means it's a many to one relationship right always you go to the particular side if you're decorating employee class so go to the employee class and see okay what is the relationship perspective to me many of us go to one project so that mean many to one relationship and then here it's a type is a project right and then a project would be the map to project dot employee employees okay right so also you can add the field here because this has to include right and the type is project okay the type is project and also here it has to be [Music] so now when you save this side so this will go away so this should be simple p so now this will go away right code so now here this also need to mention with the field right so the type is employee okay good so now we need one more thing because in the in this type of relationship either you can use a third table or you can use the same table right in that case we don't need a third table because we can keep a project id within the employee table so in that case we can perform the one domain relationship right so now what we do here we create a new column for project id right project id it's a type of a string right and that need a column because that is go to the database right and if you want you can include that even even within the field as well if you don't need you can skip that so let me to include that okay so here you need to understand this right so when you um i mean it doesn't import here when you create this right so the this has to go to in the create as well so the your dto so when you go to create you need to make sure this is you make as a nullable because when you create a project you don't have a employees okay good so now um this part is done right so let me to uh close these two and now if we get the window back right so you can see it's it's here now still you won't get any data because we didn't wired up but let me let's we'll see what happened to our project and i could say unable to create database okay because we already have a bunch of data um in the database so therefore uh it will give errors because the relationship still does not exist so therefore it will it cannot create a relationship because um the existing data will not comply with the relationship so therefore it create a problem so let's come back and try again okay so now everything is good uh but let's say let's go back and create our records so first we're going to create a project right so when you create the project uh let's create this one that you got rapid project created so now when you create employee you need to put this id here right so let's create employee and now you need to kill the project id here okay so because the create dto we didn't add that so let's go back and fix that quickly so let's go quickly to employee create dtu right so here and uh you need to add new uh property called project id it's a type of string and mark that as a field right so we don't have a update uh dto so therefore we can avoid that for now and if you go back here your schema will update to get the the employee with the project id right so now you can send the project id here so go back and copy this value and [Music] so that value would be this right so now let's get project id right and you will see the project id but if you put the project here right so if you put the project here you will get error because the project is not null and um it's not there right okay fine so now you need to solve this problem right ideally when you query the employees that you should get the project and the project information as well there are two different ways to do this right but see what happened see i mean let let me to explain the the difference between these two right so first i'll i'll go here and i'm going to fit the project right so when i put the project i get the project but if i ask employees right if i ask employees and their first name and the last name right so that will give me an error because it is not there right if it's not there okay so now what we need to do is we need to fix this right this is how we can fix we can come back here right in the project service right so here you have find method right find all method okay in the find all method you can tell what is the relationship right relations in the relations you need to specify go back to the project um entity right what was the name you added here the name is employees right so you need to resolve employees here right that is the relationship mean right so that the relationship mean ideally that has to come in array right the relationships can be an array okay cool so now the one side we fixed right because we got an error so now when you fetch this it says cannot return null for non-nullable field employed first name what did we did wrong so here we use the employee that's fine i mean if this is wrong it will give us a proper error saying this is wrong so let's go back to project entity um yeah okay uh because here we are saying employee array but in the field we referred as just employee so that might be the case so and there's one other thing we can fix here because uh there can be projects where you don't have employees so you need to make sure this is not able to as well right because i mean there can be project you just created the project but you don't have employees here so let's see what happened so now go back here okay cool so now if you have a project and it give you uh employees as well right so let's create one more employee again nairobi avocado engineer nc and i'm going to put the nairobi onto the same project right oops now this time it give error create employee so i don't need this for now because it just created okay so now i got i put the nairobi in the same project so let's get the all projects so now you can see it's one project there are two employees working right so this is work perfectly fine jonah and nairobi on this rapid project so now here is the problem right so we can do the same way to implement this uh this side right when you get the employee so still if you put the project it will give you an error we can use the same technique to fix that but here is the problem right if you have find and find all right so for example if you go to this project service you have a fine and you have a fine one right so if you use the fine one still the employees won't come right so for example if you go back here and if you let's say get projects so i'm going to use other query and i'm going to copy this and use other query to get one project right so we have something like this here in a query get a project right so [Music] here in the project you need to pass the project id right the project id i am going to pass this one right project id i am going to pass this one right so if i execute this it says employee is null because we didn't put this relationship over there so how to fix this in the fine one as well you need to tell the relations is employees right so relation is employee so that means everywhere you use this you need to fix this then only it will work right but you i mean in the project side you don't have other option you need to do that right so now it will give you the project uh all the employees right employee side yeah you don't have any option uh but you have to do this but for the employee side sorry project site you don't have options for employee site employee carrying the project id why does that mean we can resolve from there right so let's see how we can do that so if you go to this project resolver you can see we have a method to find one right so that means the find one method is expecting a project id right when you get the project id it can give the project back so now employee employee carrying this project id right in the employee it is a required field as a project id you cannot create employee without a project so that mean so we can use this method to resolve the project right what is the project employee belongs to so but the problem is this particular method in the project resolver and um employee uh will fetch from the employer is always two different resolvers it's in two different modules right so in order to get that done so we need to access this project resolver from right it's a two different resolvers so project resolver from your um employee resolve employee module right so how we can get this so you go to the project module right and we need to export this right so we can export this so you can export project service okay you export the project service now you go to employee module and you can import and you can import project module right you can import the project module so now what you can do is you can come back to employee service and you can inject this in here right so private project service is type of project service okay so now you can create a method here okay project and here it should need a string id and it will return promise of type project okay so now we can say this dot project service dot find one and we need to pass this id right it's simple okay it's simple and we need to return here okay so that's good right so now we come back here to employee resolve right so this method name is very important right so this method name is very important why because if you go back to here employee entity which we used to get so this is the what we are going to resolve right this field is the one we are going to resolve right so therefore it's important so you need to make sure the method name is also same as this variable name okay otherwise it won't work okay so you come back to employee resolver okay so the my method is this right and that is also um that the type of this is field resolve oh resolve field yeah resolve field right so resolve field what is the output of this is project right so what we do here this dot employee service dot get project right so it require id in here getting id is a little bit tricky okay so why so if you go here we are going to fetch employees and employees we are going to fit the project right we don't need this project id we are going to fit the project right so now i mean if you do this now you will get an error okay because this project is not running that's fine even if it's running you will get error right so now in the project project is a sub sub uh data set i mean a sub uh entity to the parent employee right so here this has id and this has a name and this is a code okay so now we are going to resolve this to resolve this we need id right so we need to get id id getting from this employee objects project id right this one this is the one we are referring right so therefore what we need to do is we need to tell here hey you need to resolve this from parent right you need to resolve this from the parent right and then you will get project id right so how you get this let's say directly we get here parentheses employee like the employees type of employee right and you can mark here employee dot project id and that way you will get project id from employee okay so now we are good let's see npm run start there okay good so let's go back here and execute this now it says cannot return now for none another welfare employee dot project okay why so we get the project oh very simple mistake we don't return here okay i'm doing one hour long tutorial is um just make me tired so like forgetting things easily sorry about it right so now you can see right so this id uh this uh m you you have a project right this john jones project is rapid and this project is this now so advantage of this method is like doesn't matter how many finds you're implementing as long as it has the object has a project field and these know how to resolve the project field right i mean this example you can use this for any field you want right if you want to resolve i mean if you want to tell nest to how to resolve this particular field you can write it here you can put the project here and you can say resolve field and then whenever it sees the object uh that field within that object it will going to resolve right for example if you need like um quickly implement the find one here you can implement the fine one and you can see uh this works right so now we miss a few things to do here um so let me stop this and if you go to um service project service we have two things to do one is this update and then um this one right so update and delete is nothing but it's very simple to do right since you now you know how to do that so what we can do is this dot project repository dot um you can take save itself right directly it doesn't matter so how you can do this let [Music] project this type of project it's equal this dot project repository dot create and we give the update input and then we can set the id here right so project dot id equals the id we are getting from here and in the save method you can pass this project right so that will work and then you can return this okay and delete also same right so you can have a string here um i mean okay i'm not going to implement the delete right here but when i push the code i'll implement the delete because uh i'm in the sake of saving some time because this already is a long tutorial and also if you come to this employee service right so you have a find all so you can quickly implement find one okay so i need the id is type of string okay so this dot employee repository dot find one and you pass the id and you return this i'm not going to set the return types and anything here again to save the time and now you go to employee resolver right so you can have a query here for type of employee right and okay so the method name is fine one and here i'm going to return this dot employee service dot find one and i need the id all right so here i'm going to use ads and on this id i'm going to take to id it's a type of string and i'm going to pass this id here and so now i have sorry i have um one employ implement also implemented right so if you go to run this and now you can see okay so okay remove id uh giving a problem because we here in the employer the project resolver um so let me go here we change the value over there but we didn't change the value over here so we donate this okay and we can tell this is a string and i mean we didn't implement that but i'll make sure i implemented before push the code because i don't think i have a timing between this feature to do that okay so let's um right so it's working so let's go back to here and quickly create i mean you can implement multiple queries within the same thing right so but i'm i'm just having multiple windows here right so query i'm going to query uh find one and because i didn't name that i think right so here i need to pass the id right and then i'm going to get my uh first name and last name as well as projects right as well as project so now technically this should resolve automatically so let's go back here and get employee one employee id so let's get this one right and get it past it here right so now okay so i didn't mention the path so let's say project name okay so now when i run this now it automatically resolve the project why because uh we don't have to put the relationship because everywhere anywhere if you're picking the employee to the resolver and it sees a project field and you know how to resolve it okay so this being a long tutorial and but i covered everything you need to create a production ready of google server so keep in mind these practices are there for reasons right these are built bunch of engineers these practices are established by a bunch of architects so there everything has a reason right so you can violate this with your own cost with on risk right but this is the right way to implement i mean this is there is no straightforward right way because this is opinion way so this is one opinion how you can implement a straightforward graphql server and we are going to talk about security as well in a future video but for now this is really good to go for production okay then thank you for watching for this long video and i'll push this code to github so you can refer the code always and if you have any question you can comment and make sure you subscribe and follow my facebook and instagram so you can stay in touch with me and shoot your question if i have time i will answer stay safe take care
Info
Channel: CodeLabs
Views: 3,961
Rating: 4.8956523 out of 5
Keywords: nestjs graphql code first, nestjs graphql typeorm, nestjs graphql, graphql nestjs, nest graphql, nest graphql typorm, graphql tutorial, graphql vs rest, graphql apollo, graphql apollo server, graphql api, nestjs tutorial, nestjs graphql code first example, graphql nestjs tutorial, graphql nestjs example, nestjs graphql schema first, nestjs graphql federation, nestjs graphql mysql, nestjs graphql sequelize, krish, krishantha, codelabs, krish dinesh, gql, nest gql, graphql node
Id: _PVA98-ooWA
Channel Id: undefined
Length: 82min 18sec (4938 seconds)
Published: Sun May 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.