GraphQL CRUD with Django - Introducing CRUD operations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to the graphql with django series this is part three so in the previous tutorial we did focus on returning data from the database so in this tutorial i just want to finish off the crud and have a look at how to simply create some data with graphql or django and then update that data and then lastly delete the data so this tutorial just takes you through the very kind of introductory basics of doing that we're just going to be utilizing the same databases we created in the previous tutorial so if you haven't create seen the previous tutorial and you want to have a look first how to kind of return data then please have a look at the previous tutorials before moving on to completing this one so we finished the previous tutorial ending up just taking some data returning data from multiple kind of tables in the database so we returned all questions and all answers and we returned something like this to the front end so let's go ahead now and just add some data to our database so far the only thing that we've been dealing with was queries so now we're going to move on to mutations and this is going to allow us to actually create some new data in our database so a similar type of pattern from the query first of all we need to bring in the fields that we want to deal with so what we're going to do here is we're just going to i'm just going to provide you a simple example of updating and adding um data to the category table so in my model i have a category table uh here so that only includes name so this just gives us a good opportunity just to start um with something really basic updating and adding so data to the category model here so first thing that we need to do like our class query we now need to bring in the class mutation and then like before because we want to grab some data we're going to use a graphene object type and now we just need to like we did with the query just uh select the fields that we want to use so we create this new variable here called update category because we want to update a new item in the category for example add you can change that to add for example or i'm just going to keep it to update for now and then we then are going to set out a new class in a minute and just pass over the fields to that class so then we can actually perform the update so that's pretty much it how we're going to start off by defining our mutation and then we're going to then grab our fields and then send it across to another class where we then going to perform some actions on it in this case we're going to add a new entry into our model so before we make that new class and perform some of those actions let's just update our schema here because we want to tell the front end not only that we have a query that we're running but we also want to tell it now that we also have mutations so mutation equals uh in this case it's going to be mutation okay so that just allows us then to hook the front end with our mutation class which is now going to be connected to another class which we're going to call category mutation so let's go ahead and create our new class which we're going to call category mutation uh there we go and then so this is going to kind of extend this class and kind of allow us not in a normal sense but it's going to allow us then just to define the parameters to allow us to save data so here we're going to define um the parameters and the functionality to actually then save the data to a database so we're going to be expected to work with the name um so this is uh if we look up here we're going to get the from the category type we're going to get the name field from the category type so we're going to bring that in so that's what we expect in terms of the arguments that are going to be passed in and then what we do now is we're just going to define the category type fields here so category equals the fields from category type so we now know we now have kind of a connection between our um a function that we're going to build to save the data and what model we're working with so we're going to collect the fields from there and going to put them into category and then we're going to now add a class method which is going to allow us to kind of build a function so we pass in root and info if you remember from the previous tutorial what that is for the necessary z but also what we need to pass into this function is the name so that obviously is what we're going to include on the front end and pass in so that we can save that new name um or that new category and that's obviously the the name of the category that we want to save so let's put that in and now we just go ahead and it just now starts to feel a little bit more like django because here what we're going to say is set up this new variable called category and you can see we're getting the category model this and the name equals name okay so let me go ahead and just do a category save so literally what we've done here is we're gonna from the front end uh send across a name and that's then going to then be captured and we're going to capture that pass it into this function here we're going to utilize that uh to then actually then just save it and that's then going to save it as a new category in our table so once we've saved it we'll go ahead and just return and then you can see what we're doing is we're just returning the category equals category just to confirm that we've saved the new category okay so that's pretty much the flow of this um we start off with this class mutation and then we're going to pass in some fields so as long as our field is recognized in this case it's going to be name it can then be passed into this function here and then we can utilize it against the database to then actually then save the data so it does help having a little bit of knowledge of django queries etc here because you can imagine if you do you could easily then extend this and you now know how to potentially now add some more data so if we did want id to bring in id we'd need to bring in the argument up here id etc so we can bring that in there and then we can bring that data into our function here so we can then perform some actions so that's how you're going to kind of extend it to add more fields here so i am just showing a really kind of basic version at the moment so with the server turned on we can now go into our front end here if you head over to graph ql in the url and this is what we previously had this is what we were previously returning so previously we're using query so as you might imagine now we now need to define our mutation so just make sure you refresh this so you can pick up your mutation you can see that now it's available so we can now start kind of configuring our mutation so like in the previous tutorial we can go ahead and just name this mutation it makes it a lot easier for us to identify it on the front end so let's just call this first mutation okay call it whatever you like of course and now we need to go ahead and you can see that some items already starting to pop up here so identifies our update category that we've just created in our mutation so we're able to hook into that now at this point what we want to do is now to define like i previously showed you how to kind of pass these uh variables or arguments across so in this case we've got the name so let's just define the name and then let's give it a name so let's call this a a new category for example so that allows us to create a new category now we want to now go ahead and define category and finally within category we have the name there we go so basically what we're doing here we're just defining in the category with declaring that's what we want to work on and obviously what we're doing is we're passing over this argument here name equals new cat so that's the name of the new category that we're going to build so if we do that there will be an issue here i'm expecting a problem so if i execute that you can see that we've got this error here so in addition to what we previously mentioned in regards to root and info in the previous tutorial we now need to add another item here so here we're going to need to bring in another argument here cls and that's going to allow us then to perform the actions that we want to perform so let's go ahead and now add a new category here so just refresh and then we go and you can see that that seems to have added so let's go back into our database refresh and you can now see we've got a new category name so that was a really simple example of course you'll want to expand upon that and you'll simply just need to think about the arguments just expanding the arguments here of all the different parameters obviously change the model that you're using so for example if you wanted to make some quizzes obviously you'd maybe include all these items here and then just follow the same type of procedure down here obviously you would need to chain that across and then just save the data as per normal so at this point i really i'd um ask you to explore those options and in future tutorials we put this together in much larger and in context of course we're going to continue with this quiz theme and uh build this up utilizing graphql etc so we'll definitely focus and have a look at that a later date but for now if you can go ahead and start thinking about possibly changing this mutation to include quizzes i think that's a good task to do so next up an example whereby let's just uh get rid of this one here we're going to provide you a simple example to actually update so i'm just going to line that so you can see this in the code provided as a link in the tutorial description so let's think about um for example updating the data so let's go ahead it's the same type of procedure here so we can leave this exactly the same and we're just going to pass in some fields so that's okay we're going to call this category mutation again now this does say update this time and so we are going to update so how we're going to update this if you're not familiar with the database here if i just browse over some of these items here you can see that down the bottom left here there's an id um so we're going to use the id this is number two this number one these are the ids of these categories so we're going to use the id to actually select the category and then update it with some new data so this time we're going to add in a new class argument it's going to be exactly the same as before and this time we've got names so let's just copy this across so we're going to utilize the name as per normal um but this time we're just going to add some more here so we're going to also bring in the id so let's do that so id equals graphene dot id so notice here we had a string for the name so this is going to identify an id right this is obviously the primary key now if we move up again if we go into category you can see that we've got the id field and name available so we are definitely utilizing both of them this time so we go ahead like before and access the the category the fields that we're going to need and now we just need to again just define our class method so let's go ahead and add class method now this time whereas before we were just utilizing the name we can now to add in for example the id so we also want to pass in the id into this function so the first thing we're going to do here is we're going to we're going to get the data from the database so let's just select category equals category and object so very kind of django here and we're going to get one item from the database and that's where the the id equals the id that we pass in so that's going to return one item from the database obviously once we've got this data we can then go ahead and override it so uh this is going to be category and then we're passing in category we're then going to pass in the name for example uh so dot name so that's going to access the name that we've passed in across and there we go so the category name so we're going to basically override the category name we're going to get the data from the database we're going to up update or override the data name to the new name that we pass in and then once we've done that obviously we just need to save so let's just go ahead and save that category.save and then we can just go ahead and return as we did previously okay so that's kind of the functionality there we're going to pass in the name and the id we're going to get the id that we passed in that's going to identify an item or category within our database we're then going to basically override the name with the new name and then save it and that's going to create the update so this is what we've got so far new cat and that is id number 15. you can see down here for example when i hover over category 15. so let's go back into our query and update this so we want to also now pass in our id uh so that was a 15 in this case so obviously we're doing this manually um we can also additionally add in the name or keep the name there because that's not a problem but obviously we want to change the name so let's change this to another category so that's going to be the new name for this so it says it if you hover over it says unknown because i haven't refreshed yet so let's just refresh it doesn't look like the server is working okay so it was just stuck i've turned the server back on there's no changes that we made so refresh again and you can now see if i move that move in a bit uh so everything seems to be picked up nicely now so update category id 50 name another cat okay so let's see if that works let's do that so we've got another cat let's go back in and refresh and there we go so we've made an update in our category name so we can just test that out again so it's still id number 15 so let's try that instead and you can now see when i refresh it's going to change the name again so you can see how that's progressing on slightly from the previous example we're now taking in two arguments here we're using an argument to uh on the object so here obviously we can run multiple kind of django this is very django here isn't it objects get so we can form multiple operations as we would do as if we were using django of course so again that was a very simple example so i guess now it's up to you to think about if you are going to if you did follow the previous suggestion of creating quizzes utilizing this bigger example now is a challenge to try and kind of update a much larger set of fields so give that a go let's move on to the last example which is delete so this is really simple because we've been using save previously so let's just change save to for example delete so this time uh we're going to get the id uh we don't necessarily need to um know the name we just need to know the id and what we can do from there is well once we've selected it we could just obviously go ahead and delete it so let's just give this a go so just checking this is a still number 15 in the bottom left hand corner here number 15. so we don't necessarily need to pass in the name here so from category we can just identify the id so let's just refresh this there we go update category argument name of type string is required so there's just a few things that looks like i need to kind of change here so let's just uh remove that argument there from our mutate um we don't need to name in in this case anymore um let's just give that a go refresh okay so it looks like it's okay this time so let's go ahead and do that so kind of obviously um the problem is we're trying to return something here but it looks like it is working so refresh and you can see that it has disappeared so let's just remove the return in this case and let's just try that again just to make it a little bit cleaner so i just add a new category that category now is number 16 so i just need to change this to 16 and then press go so it's a little bit cleaner this time and then a refresh and what we just added there is now disappeared and there we go so that was another a simple example a simple change to the arguments removing the items you don't need obviously removing and including now the delete so the django rm can select the item from the database we collect it and then we simply delete it so really all we're doing here is we're utilizing graphql to specify on the front end the particular id that we want to select and delete all we're doing really is passing it across from here this is just python django now where we're collecting the data from the database and we're just deleting it as per normal so again i appreciate this is a very small example but i guess i'm challenging you now to think about how you can expand on the deletion include multiple fields uh one question that you're going to have or one of the many questions that you might have is for example how to control return to show that um i've actually updated something and that's something that really needs to be left for a later tutorial one of the perceived weaknesses of graphql is the uh reporting and error control how kind of managing and handling that over for example rest which is considered a little bit more uh nicer in terms of controlling or error controlling uh or capturing errors sorry on the front end okay so hopefully that was useful too um i think we've covered everything we wanted to obviously the return was done in the previous tutorial so that was a simple example introduction if you like to create update and delete with graphql
Info
Channel: Very Academy
Views: 6,555
Rating: undefined out of 5
Keywords: graphql, django, django graphql, graphql schema, django objects, django generic views, django tutorial, djangotutorial, django tut, django beginners, djangoproject, django examples, learn django, beginners django, django framework, django 2020, django example, graphql tutorial, graphql queries, DjangoListField, graphql arguments, graphql crud
Id: 3819x3b43Ok
Channel Id: undefined
Length: 20min 35sec (1235 seconds)
Published: Mon Nov 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.