Coded Factory Talks - Django Graphene

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
am i recording yes i am all right uh thank you all for coming uh today uh we're hoping to make these talks uh more frequent and also more public factory should probably keep this english so so it's not just the people who are working on factory projects that benefit from these talks it will be our entire alumni network um so these talks are basically going to revolve around technologies that we're actually using in the factory so every time we have a new technology every time we need to onboard engineers this is the way we're going to do it we're going to do it in the form of a talk we're going to try and keep the examples more generic so that any we don't give away client information but essentially we have a we have a pretty large project coming up and we've decided that the best architecture to use or the best kind of api for this specific project would be a graphql backend so we're going to be using graphene or django graphene to be specific now this talk specifically is just going to focus on querying in djangography we're not going to look at querying is the equivalent of get requests if you're talking like in a restful context um we're not going to be looking at mutations which are the equivalent of like post and put requests we're not going to be doing that today we're going to focus on the bare basics which is just querying the other thing we're not going to do today is we're not going to look at the front end we're going to we're going to use a like a something similar to postman that's kind of built into django graphene that allows you to test uh test the api but i'm not going to show you today how to query from the front end that's going to be a separate talk when we've actually built a front end for our engineers to start working with um is that all clear yes okay cool so uh let's do a quick comparison before i jump into the code this is this presentation is mostly just a couple of diagrams and then i'm gonna jump straight into the code base that we have so most of you all of you have uh like when you were in boot camp you learned django rest framework correct yes um and the way drf works is that you receive an http request that request gets routed to one of several potentially nested multiple times urls okay and then that url that matches whatever it may be is going to call a view and then that view is going to connect to a serializer and that serializer will define how your data comes back from the database to be connected to a model or some some set of models and then it will manipulate those and create like uh essentially an object that's going to be sent back as the response you all remember this okay remember how infuriating it can be if like the front end suddenly decides that it needs an extra piece of information to like change the serializer or suddenly realize that okay i did this but maybe there's a better view that i could write then that would combine these serializers together i'm thinking specifically of and false session app and shutter which was probably rewritten a few hundred times um but yeah this this process needs a lot of planning and a lot of like uh structure you kind of have to know in advance what your urls are going to be know in advance what your views are going to be and you you end up doing a lot of work just combining serializers rewriting them adjusting them refactoring them and cleaning them up yes so we move on to graphene all right so the way that graphene works and django graphene specifically is that your http request comes to url singular okay one url you don't have to think slash cookies slash cookie id slash whatever no it's one url okay that url calls one view which you don't write you just import it and use it okay and that view will connect to something called a schema now a schema for those of you from compsci background you know this schema is basically just a description of the structure of data okay so this schema all it is is a giant document which tells you what fields and types and i'm going to clarify all of this as we as we go through the example what fields and what types the user is allowed to query and access okay and each of those fields or types has something called a resolver which is a function that just defines what is in that field or what is in that type okay this might seem like a longer process but it is much easier to write fields and fields types and resolvers than it is to constantly be managing url's views and serializers okay obviously less talking more doing this started saying that in all of her classes that's talking more doing so i'm borrowing it um okay so let's go to the application that i have right now all right uh it's running right now let me just close this okay so i will and fiesta double are there any questions before i move on before i jump into the code about this less talking mode okay by the way you guys can can unmute it thank you all right you guys can unmute yourself at any point and just interrupt me okay all right so we have a uh a pretty simple application we've got an instructor model which has names in english and arabic a bio in english in arabic and an image we have a topic model which has a name name in arabic and an image we have a boot camp which has a name and an image and then this is where things start to get a little bit more complex we have a cohort which has a start date and name and a foreign key to a boot camp and it also has a many to many fields to instructors through a model called role okay and this role model that was not intentional this uh this role model has foreign keys to an instructor a cohort and an and a many-to-many field to a topic so it's not 100 straightforward but it's nothing too uh too out there and the way this looks in the in the django admin is that in a boot camp let's say coded live you have cohorts okay and if you go to edit cohort let's say real which is our upcoming coded live cohort you can add instructors and you can put the topics that they're going to be teaching okay this is a one of our instructors right here okay um you can also if you wanted to just look at the instructors directly all right okay so we're going to be building an api on top of this set of data now if this was drf this would maybe take um if you wanted to do it like very cleanly then it would take maybe a couple of hours to build and then if the front end needs something very specific then every single time they do something you need to rewrite your backend but let's see what it looks like in graphql okay the first thing i'm going to do is just install graphql or make sure that it's installed so fit install graphene dash django okay it's already installed um let's go to our settings.pi and put it into our installed apps it's already there because i already did this earlier okay and the next thing we want to do is we want to add our url singular okay so what i'm gonna do i'm gonna import a couple of things okay from django.views dot decorators.csrf csrf i'm going to import csrf exempt and this is so that we can make requests to this url that i'm about to write from any uh from any client um basically your apis don't need um cisrf tokens uh because they're kind of open by default you need to protect them in other ways the other thing i want to import is from graphene django import crap why is this not uh as you're sorry i just have a quick question if you don't mind i don't know importing the csrf exempt for this project specifically because you're not going to be connecting to a front end or are you speaking in general it's because i will be connecting to a front end basically if you don't use this then your front end has to send back the csrf token in a cookie with every single request which means you first have to make a request to the backend to get the csrf token store it in a cookie and then attach that cookie to every single request but that's unnecessary if you're building an api because csrf tokens are to stop static pages like injected scripts and static pages from making requests to a to a regular django backend um correct me if i'm wrongful as i might be completely wrong about that it's just something that i was reading up on today csrf is a way to protect your uh website from automated attacks so you don't make a bot that will request the same thing over and over and over again okay um let's see is this why is it nothing it is import graphql view sorry my autocomplete stopped working which i don't like but that's fine and we're going to add a url this url is going to be slash graphql and the view i'm going to add is graphql view dot as uh view i'm going to pass in something over here graph e with an i ql equals true and all this does is it'll give us a little tool um at this url if you go to it directly in the browser that allows you to test the api when you go into production you can switch this off uh if you want okay i'll show you guys that tool it's pretty amazing what it can do okay uh so that's our url oh this is the problem that views okay graphene django.views that's why it wasn't autocompleting all right so i have a url let's try it out okay i've already ready over here it was 404 a second ago and if i go now i get an error okay because that is not enough to connect an endpoint schema settings schema settings yes that is the next step that i'm going to do thank you the next step that i'm going to do is i'm going to add something to my settings which is a graphene settings variable this will be a dictionary with a schema and this schema needs to point to a file somewhere in your project that contains the main schema for your application ours is going to be in this folder over here in a file called schema in a variable called scheme so it's going to be graphene underscore demo dot schema the name of the file dot schema the name of the variable okay so let's create that file schema.pie pies and here we're going to import a couple of things graphene okay and [Music] i'm going to create a schema which is graphing dot schema and this schema class or this schema constructor if you can call it that in python i can't remember it's supposed to take a query class which we're going to create here so class query inherits from graphene dot object type and for now we're just gonna pass okay and we're gonna send this as the query over here okay so this object right here is going to be a document that has a description of everything that a user is allowed to make a request for everything that a user is allowed to make a request for okay and this is connected to our project through this so graphene demo dot schema dot schema is this thing right here and this will include everything that we are allowed to make a request to so is this enough that my backend stops crashing it is not although i'm getting a different error now which is fields must be a mapping okay so everything is now set up the issue that i have right now is that my schema is empty so my back end is telling me like you're making a request but there's nothing to there's nothing to ask for all right um so oops let's change that right now uh or let me go back to my diagram okay we added our url singular we added our view singular we created our schema singular okay we did all of this now we just need to work on these parts fields and types and resolvers all right before i jump into that as is our view our view is right here we don't even write it we just import it all right all right yeah okay um any any questions about this so far i know just kind of threw everything out there all right okay so let's add a field we add our fields well i'm going to add my fields right over here this is the main query object i can add fields right over here in a few minutes we're going to be bringing in fields from somewhere else but right now i'm going to add a field called hello okay and this field is going to be a graphene dot string okay all right so let me save that let's see if this changed anything for me oh okay this is the interface i was telling you about that graph eql equals true creates this interface okay this is kind of like postman for graphql okay you make requests through this and it'll hit that url in the back end okay the other amazing thing about this is that now that i have now that i have a field i can look over here click on query this capital q query is this one right here and it will tell me what fields i have okay so now over here i can say query hello okay and it auto completes as well it kind it knows what i have access to okay so query hello we can run it and i get back data hello null all right this no means that the backhand doesn't know what to put in hello it knows it's a string it's telling me that it's a string right here but it doesn't know what to put in that string so now we have schema and one field and there's one thing missing which is a resolver okay so let's go back to our code and let's go over here i said that the resolver is just a function that defines what the value of this field is going to be so i'm going to say dev resolve underscore hello self info self of course is the query itself info here is an object that has the request information so if you were using this and you needed something from request like request.data then it would actually be info.context dot data i believe i'm not going to do that today i'm going to do something more basic but that's what's in here for now what we're going to do is we're going to return uh hello world okay let's go back to our graphyql interface and let me make a request okay so now i can make a query for hello i can ask for the hello field and i'm gonna get back this string okay so let's add another one just to send this point home graphene dot string okay we're going to add another resolver oops i need to do that okay resolve goodbye these names have to match by the way and goodbye is going to be it's a very melodramatic api it's going to be goodbye cool world okay so let's i'm going to refresh this so we can get our schema update and now in my schema i can ask for hello and i can also ask for goodbye okay so if i query hello goodbye i'm gonna get both strings okay i just want to say something notice that there is no comma guys that is not comma although you can optionally add a comma and it won't break you don't need it you don't need it um so yeah now that we have two fields hello and goodbye and i can query for both fields i can also query for only one field i only want goodbye i only get goodbye so if i'm in the front end and i don't need hello i don't have to get it i don't have to ask for it i can only ask for what i need okay um i will i will block the two of you if you don't stop sorry in the chat i am so really diet [Music] okay so at this point we have two fields and two resolvers okay every time you create a field you need to create a resolver for that field basically what the schema needs to know what is going to be sent back if this is requested okay so let's try something a little bit more complex i'm going to give this hello field some parameters okay i'm going to define them over here i'm going to say name equals and this is going to be a graphene string with a default value equal to world and over here in my resolver i can get access to this to this argument to this name as part of my courts so i can say name over here okay and in that case i can turn this into an f string oops okay and instead of world this can be main okay so if i do that now and if i go back to my explorer over here i go into query i can see that now my hello field has a parameter so if i query for hello with no parameter i've given it a default value so it'll come back with hello world but if i want to i have the option of saying name is hani and it'll say hello honey um sarah can you can you clarify your question please as far as i'm aware you do need a resolver for every uh for every field that you have okay maybe there's a better way to do this but i learned graphene two days ago so my code might not be the most distinct yeah all right so up until this point everything we've done is actually has nothing to do with django okay everything that we've done till now is not actually connected to our database or to our back end in any way this is like a this is a very these these fields are are they they're pure python up until this point we're using graphene itself so hamed i think you're asking uh about uh like python graphene versus django graphene this everything we've done up until this point uh is pure python graphene with the exception of course of like connecting to the urls i don't know how you would use the schema in uh in pure graphene but the docs are pretty well written all right so let's move into like the django specific stuff but before i do that are there any questions about this you can wait everyone's clear all right um actually i don't know if you're gonna go into this this uh like if you have a lot of fields it's gonna be a lot of resolvers is there a better way of organizing this like splitting it up into different files or something like that absolutely it's not really into different files but it's in into different types so if you see over here i said that you can have fields but you can also have types so it'll become clear with the django stuff because creating a type requires a class for that kind of object as well and we have those as our models are classes so we can use our our models to make this example so let me jump into the django specific stuff first i'm just going to show you that you can have multiple schemas connected to each other or multiple query objects connected to each other okay i'm going to go into our boot camps application and create a schema over here okay scheme it up higher and in this one i'm going to [Music] say from graphene import uh i don't know what i need if i need anything yet um actually i don't need anything from graphene just yet but let's create a class class query and this one is not going to be like uh it's not going to be like the previous one it's not going to be graphing.object type it's actually just going to be just going to inherit from object it's going to be what's known in python as a mixin okay we're going to combine it with this query in a few seconds um and for now let's just pass and that's all you need in a schema like in an application level schema this is the this is everything that you need over here what we're going to do is we're going to say from bootcamps.schema or sorry import bootcamps.schema and then here this query is not just going to inherit from object type it's also going to have a mix in from bootcamp.schema that's query okay so anything that we define any fields that we define over here will also be available in this query okay i don't know if you guys have seen this kind of python before but um this is outside of the scope of this of this class um so let me refresh this let me try it nothing has changed so far but let's add a field to this schema right here i'm going to add a let's say a date field and this is going to be now and can import from graphene imports date time okay so this is going to be a date time field okay and then i need resolver so def resolve underscore date click self takes info and this resolver forgot to actually add this to my instructions so let's make this up as we go along let's return from date time import time turn date time dot now okay so this is one more field we just added one more field with one more resolver okay and because this query is being combined with this query as a mix in right over here okay it means it will show up in our schema so if i refresh our schema now i open up my query i have date which is a date time object okay so if i request the date okay i will get today's date and time again every single time i request it it's a little bit different okay thank you honey um everything clear about what i just did i could have done i could have done all of this in here but what we're going to do with this is we're going to start creating types for our objects and we're going to start turning our objects into fields that we can query okay our models sorry when i say objects emit our models all right so let's actually start to do that i'm gonna get something from let's import our models first so from dot models import instructor because that's the first query i'm gonna do where we can get a list of instructors okay and i'm also gonna need from graphene django imports django object type okay you see that this was a graphene object type i'm going to make a class here or i'm going to make something here that is a django object type the difference is that a django object type knows what a django model is okay um so let's create a class this class is going to be instructor type okay and it will inherit from django object type okay and this will have a meta a meta class and this match class will define the model this model is the instructor all right now what i'm going to do next i'm going to use this type as a field okay i'm going to create a field i'm going to create a it's actually it's a subquery and let's import from date time we're also going to import list i'm going to make this a list type okay and this is going to be a instructor type okay and of course every time we add a field we also need to add what resolver a resolver right so let's say def resolve underscore instructors takes self takes info and what i want to return here is my query set okay so instructor the model dot objects okay this query set will be returned and it will be passed to the instructor type and then this instructor type django object type from graphene django will take all of the fields in these objects and make them queryable doesn't make sense let's try it out okay i'm going to refresh this okay and if i check my query i've got my date my hello and my goodbye but i've also got instructors and instructors are an instructor type and this instructor type already contains all the fields of the instructor okay is that clear let's try it out so why aren't there explanation points as these types small instructors so these exclamation points mean that this is something that's required uh but it doesn't really matter when we're querying it matters more when we're mutating when we're making the equivalent of post input requests which we're not going to be doing i think it depends on if the field is nullable or not or blank exactly if it's required but in graphql syntax the exclamation point means required so the way that maps to django is that if something in django is not nullable and can't be blank then it becomes required in graphql but again this doesn't make a difference for us right now because we are just requesting information but let's see i can ask for instructors now okay but instructors is itself a type the same way that the the main query was a type which means instructors has fields which means i need to open another level okay so what do you guys want you want tell me what you want in terms of data you want the list of instructors and what do you want to know about each instructor they're image only okay so if i do this i will get a list of all the instructors images okay do i want their names i can get their names okay do i want their names in arabic instead of their names in english i can do that and found like having a revelation right now sorry for as i ran out of time what what other information do i have here i want their bio okay i can get their bio let's say i want their name in their bio but i don't care about their image i can request that okay it's being it's becoming clear what the power of this uh this type of api is here oh yeah of course most of the time i need the id in the front event for other reasons maybe i needed to query like an individual instructor i can request that as well okay so you so the the kind of graphql does make things easier for you as a back-end developer but it makes things a thousand times easier for the front-end developers which also makes it easier for the backend developers because they don't have to keep coming back to the backend developer and saying can you fix this serializer and add this feel to it no if something is in the model i can query it okay and if i don't need it i don't ask for it it also ridiculously reduces the amounts of data that you're sending back and forth uh or you're sending from the server to the front end um but yeah this is just the tip of the iceberg let's let's move on to the next part okay the next thing i'm gonna do i'm going to create an instructor uh field and this will be a instead of a list this will just be a field okay so this will be a field of instructor type okay and it will take a parameter this parameter will be an id and this id will be a an integer type an int okay id int okay so let's add a resolver so resolve instructor this is very similar to what we did with the hello which is i can pull the id over here okay and here what i'm going to do is i'm going to have a little statement and i'm going to say if id is not none then return instructor dot objects dot get pk equals id okay else return return none okay and if we go back here we refresh we can see now i have two things i can query for i can either query in query for all instructors or i can query for a single instructor with an id say equal to three okay in which case i will get hemsa okay id equals to four i'll get myself value ar okay all right clean code or clean code id equals two oops gives him shotty okay is that clear of course i can make it a bit smarter as well let's say i add id equals int and name equals string or not string sorry string um i don't know if this is actually going to work well let's let's try it okay let's get name over here and i can say if name is not none then return inspector.objects that gets name equals name okay which means now i have the option here to query not just by id but also by name so maybe i don't know the instructor's ids because why would i i can say kale salad and i can get interesting argument id is not a chord any python experts that can fix this for me i think i could just give them some default values actually maybe maybe i don't know let's try or what did you what were you suggesting uh same thing but i meant in the resolver but i think your method is better let's try i don't know if it'll actually work nope it still needs to be in the resolver uh so let's remove this so you're saying over here i need to give them default values right i'm guessing yeah okay let's try this again okay there we go now it works um yeah so now i have the option of querying by name or querying by id id 5. oh i don't think i can do that i don't think i can have the same query twice if i give it a name so instructor [Music] um foss do you remember how to give a query a name can you send the code okay thank you very useful let me just comment this out okay there we go so i can query by id or i can query by name right here instructor method okay let's try that number five okay you can also name your your uh your queries so this is this name over here is oops has to match the name of the field in camelcase because i'm going to show you guys that let's let's say that i call this by the way all oops all underscore instructors and this would be resolve all underscore instructors okay in the front end this would be the front end in that case it actually becomes camelcase so this would be so if i wanted this it would be all instructors like this okay um and the reason it converts to camel case is because the people doing the querying are most likely coding in javascript so that they don't go crazy it converts everything to camel case but yeah you can rename these however you want but all right so let's remove this let's keep this specify and run there we go all right any questions about this uh mazzies can you any instructor can you give it both id and name or in lesson argument with um the way we've written the code it's kind of one or the other if you give it an id it's going to ignore any name that you pass and it'll just use the id but if you wanted it to be both you could uh you could have like uh and it depends conditional resolver but you could you could realistically do something like this it doesn't make sense in this case but you could do something like this instructor instructor.objects.getpk equals um why not i don't remember i haven't done django in a while but i don't think you can do a get request with multiple parameters with id uh no okay this would skip everything else but let's let's give that a shot let's say carol salad if i do this instructor matching query does not exist actually throws an error which is nice and the id is none um well let's say the id is i forgot what scale's id was six okay yeah but then it has to match both but you could use it for other things like maybe you want to find i don't know filter filter yeah you could use it for filtering maybe you want the first 10 items right so so there's a lot that you can do with just these simple tools that i've defined here okay again we are just touching we're just looking at the tip of the iceberg okay all right uh there's one more thing i want to do and just go somewhere here interesting i don't seem to have actually pushed my my notes great can you give me one second i just need to check something in the read me file oh the phone was away yep i did not commit my code great um all right i'll gonna have to make this up for the rest of the uh for the rest of the fawaz right now okay okay so uh the next thing that i want to do is that uh this this kind of field is so common uh where you you have a list field and then it's a django object type so you just resolve the entire query set it's so common that it actually has a shortcut um so from graphene django i'm to get django list field okay and instead of making this a list i'm going to make it a django list field which means i can remove the resolver okay i also don't like calling it all instructive because the resolver is not there by default okay so now if i come back here right here there's my instructors and i can query all my instructors and get everything okay now there is something that is missing from this which is probably the like everything i've done so far it's not that hard to do like building a serializer that does this apart from like the ability to pick and choose what you want all of this is doable with a serializer right so where does this really start to shine i'm going to bring in one of my other models okay and i'm going to create a class for this and let's call this cohort type django object type okay i'm going to give this a metaclass oops meta model equals cohort and that's all i'm going to do i'm not going to do anything else okay let's go over here let's refresh query instructors cohorts okay so now for every instructor i can get their chords okay and because cohorts is a cohort type it means i need to define what i want from the cohort i want the start date and the name in arabic or let's say the name of every cohort that this instructor is teaching okay make the query and here you can see that layls is teaching summer 2020 which starts on this date and shari is teaching amman which started on the state i don't know if that's actually a start date so you guys who are from that man cohort please don't correct this um and he's teaching summer and fall hamza is teaching at manor i can get this nested structure and i can reshape it any way that i want i can pick and choose what information i want what else can i get from i can even do crazy things like instructors okay by name which will give me that layla is teaching at cohort summer 2020 with amshari shari is teaching this amman cohort with myself and hamza uh he's teaching this cohort with leila and he's teaching this cohort with khalid okay and because this is instructors i can get their cohorts okay i don't know why you would want to do this but you can if you want okay let's say here i want just the id okay these are all the cohorts that shadow is teaching okay but yeah you can you can nest endlessly okay the reason this is called graphql is because it represents the data as a directional graph so this object has a field that points to this other object and if that object has a field that points back to this object that's fine that's not a problem it doesn't matter try doing this with a serializer you will jump out of the window okay so let's go a little bit further okay i have more data types let's import bootcamp of course if i wanted to i could create a cohorts field over here with a cohorts actually why do we do that it's pretty straightforward oops okay cohorts color type refresh query and i can get all the cohorts by name okay and for each one i can get the instructors by name in arabic is this beginning to feel very useful by the way just so you all know the only person in the camera on is in fun so when i ask a question i'm just waiting for her to nod all right so let's create our bootcamp type and instead of the word cohort i want the word bootcamp and let's create a bootcamp's list okay this is bootcamps right here bootcamp type okay so let's come over here let's refresh this i can query for bootcamps so i can come here i can say boot camps and by name coded live in full stack and for each one i want the cohorts by name and monday of summer 20 fall 20 and for each one for each cohort i want the instructors okay uh by name okay this is our very famous uh snack bar behind me by the way it's controlled by liz as well as teaching she also is a snack coordinator i could um what else can i do um i think i can also if i wanted to no i don't have that relationship i don't think i have a relationship to instructors from boot camps okay so the the foreign key relationships are being filled in automatically i think if you wanted the reverse relationship so from a model to to the set of objects that belong to it i think you'd need to define that as a field i'm gonna i'm gonna give it a try but let me just finish what i'm doing here what did we have left we had rolls right so let's say roll um this is role type okay so if i come here and i refresh this for every instructor in every cohort no i wouldn't be like this what i would need to do is i would get need to get the roles in this and each role has an instructor singular and then i would need their name and the role or the interesting oh i can't get topics because i didn't create the topic type yet so let me do that quickly it's not that big of a deal because i can just import topic and then oops create a topic type and press this for every role i want the list of topics by name okay so here you go and the amman coded live in the command cohort of the coded live boot camp mushadi was teaching python and javascript okay hamza was teaching django and i was teaching react and react native in the real cohorts of coded live hamza will be teaching django b will be teaching javascript and python i'll be teaching reactant react native in the summer 2020 cohort of full stack and shady is teaching javascript leila is teaching react express and react native i can ask whatever i can ask some pretty complex questions uh over here as a front-end developer this is extremely useful oh snail that was a very happy coincidence i did not mean for that to happen but it happened okay because i saw you like you you were gonna copy boot camp or something and then he copied instructor i was like why did he do that and then i noticed it was alphabetically ordered yeah that was a happy accident okay so the change the changes that you need to make as a back-end developer now if the front-end requires something is if they require an entirely new kind of data so for example with everything that we have right now um something that's missing is like maybe i need to know the different types or the different topics that we teach it coded that's not reflected here right now i can't get to that information easily i'd have to do what get all the instructors and get their roles and for each role get the topics and for each topic at the main structures and then like i'd have to do some kind of map and reduce on this that would be insane so at this point as a front-end developer i would go to the backend developer and i wouldn't tell them by the way you need to write a whole new set of urls and a whole new set of views a whole new set of serializers no all they need to do is add something like now now it has to be alphabetical uh topics topic type and instead of doing this they can do this oops what did i do got an area somewhere you have two topics and you're i'm very confused no i think i think i can have two queries like this without them being uh i wonder if i can do this nope that was not a good idea um i think i have to name them if i want to do that no expected name found um i think you have to name both of them you named only the second one no that's the case no it's not the instructors not the rules oh right right i see what you're saying uh the other problem is that i put this over here oh instructors i still don't think that's the main issue interesting let's see i can tell you why you want why i think it's the topics some of them don't have a name maybe that's why no they all have names the name is required can you instead of name have id let me just comment this out and see if this works okay this works let me comment this back in no i i guess i guess the actual topics need to have a separate name okay either way you're very unlikely to make a request like this so i was just putting them side by side this is much more useful as a request okay of course if i wanted to well that wouldn't make sense i was going to say if i wanted to return this as just a list of strings i can do that but that would be like a custom resolver and it kind of defeats the purpose which is that you want to give the front-end developer the ability to choose what information about the topics they want so image right there okay there was something i said i was going to try and i can't remember what it was accessing information from the other direction right i said i was going to try that let me go to the graphene docs and see if i can find an example um okay just give me one second right this should work by default let me try it again so let me for a uh boot camp get the wait i can get the cohorts what was i trying to do that didn't work earlier does anybody remember what it was that i was trying to do earlier that didn't work i can get naughty little topics um name instructor name no i think this is fine okay i think with this you should be able to query in any direction yes you didn't actually try it you just said you couldn't i was getting like uh i wasn't getting an autocomplete but it might have been that i just didn't refresh the browser by the way if you make changes over here so for example if i uh if i were to add a field like hello again close string um damn it now i have to create a resolver for this let's say i change the way this resolver works uh trying to think of a good example all right here i'm just gonna return to time today um so if i were to query for this date time is an argument can you try uh dot now dot today you know it's a method or a i think it's just a property it's not a method okay so there we go um and you'll you'll notice that the date time field for uh for graphene can can have doesn't care whether this is a date and time or if it's just a date it'll figure it out um but yeah this works but if i check my well that was actually a bad example because nothing changed in the schema let me comment this out i can't resolve boot camps anymore okay okay this right here will still allow me to make requests for bootcamps okay it'll crash but it'll still allow me to do that so if you want your schema and your autocompletion here to be up to date you need to refresh the page it'll make a request that will hit the correct schema but it won't have the correct schema over here okay all right um i don't think i had anything else does anybody have any questions about this stuff so far do people think it's useful like how useful do you think graphql is in comparison to restful apis yeah i've read a lot about it for us personally i still haven't seen an advantage of rest over uh graphic i haven't really dug deep into it with why is rest i think more popular than graphic yeah it came first okay that's actually pretty much the answer um no because a lot of them i i have read some cancer arguments to graphql um but i cannot remember what they are off the top of my head um as is the case with everything all technologies have their their place um just because this is really cool and it's the trendy thing doesn't mean that it makes sense everywhere uh i just i'm struggling to think of places that it wouldn't make sense initial setup you want to do it now restful apis are easier to i'll send out some articles on the uh on the talks channel to maybe see if i can find some answers to that question then uh any questions from any anybody else before we stop recording yes no all good all right let's stop
Info
Channel: CODED
Views: 2,381
Rating: undefined out of 5
Keywords: coded, academy, kuwait, الكويت, coding, code, django, python, graphql, graphene, backend, server, api, query
Id: MNHc0j8PDnE
Channel Id: undefined
Length: 69min 41sec (4181 seconds)
Published: Thu Jul 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.