 So, Nisar is going to be talking about how to develop a GraphQL API in Django using Graphen. So yeah, that's super interesting. Okay, so the audience is yours. I can see your screen. That's perfect. So you are ready to go. Good luck. Thank you. Remember, so as a reminder, if anyone has a question during the talk, you can click in the Q&A, ask the question, and then I'm going to ask those questions to Nisar at the end of the talk. Okay. Good luck. Yeah, thank you. Yeah, so hi everyone. Today I'll be talking about, like, developing GraphQL APIs in Django using Graphen. So first of all, like, let me introduce myself. I am Nisarik Shah from India. I am currently an undergrad CS student. I'm also the software developer at TwitterZip product and I am the co-creator of courses around product. So first of all, let's talk about the main points of the today's talk, right? So, like, I'll be starting with the general views on the APIs. So, like, then I'll shift towards the restful APIs in the GraphQL APIs. Then I'll discuss a few concepts of the GraphQL and then we'll implement the GraphQL APIs in Django using Graphen. Now, basically, in today's age, most of the applications use APIs in the backend, right? And then they connect them with the frontend. Now, the core logic is written in a function which could be called when a particular API is called by a client and then the data is sent to the client from the server. So, basically, what happens that a client calls the API, backend function gets executed and the client gets the data, right? Now, some of the popular API protocols which are currently used are so pressed and there are a few more too. Now, let's talk about like this restful API. Restful APIs have particular methods such as get, put, post, get, put, post and delete it. So, the developer needs to build different endpoints according to the different requirements such as if a developer wants to fetch the data from the server, then a get method API needs to be developed. And if there needs some modification of the server, then put, post, or delete API needs to be developed, right? So, here are the some endpoints of the rest APIs, right? Now, like whenever this API is called, a piece of code gets executed and the response is sent to the client, right? And also in the rest, in some of the APIs, you need to pass some headers mentioning the type of the response such as JSON or like anything. And also, if you want to secure the API, then so like you need to pass some specific token for the authentication. Now, what can be the problems faced while building restful APIs? First thing is there are multiple number of endpoints which are developed in the rest for a single application. Now, this would require a very good amount of documentation or otherwise it may occur some problem at some time. The second is over fetching. Now, what over fetching means? It means the client gets the unnecessary data which is not required in that particular function or feature. Now, for example, just imagine if you want to display all speakers name and you have one endpoint name slash speakers which returns JSON objects which includes profiles of all the users, which means it includes all the data of a particular user. Now, the client only means the name of the speaker and the other data is not needed for that particular feature. So, this is known as over fetching and the under fetching. Now, the under fetching means a client is not receiving the complete required data for a particular function. And so, client needs to call multiple endpoints. Like the client first get the data from the first request and then a second request needs to be made for the required data. So, what could be the alternative of rest and what can be used? So, that's GraphQL. But yes, there are also some cases where rest is more preferred over GraphQL. Basically, GraphQL is open source and it's a query-based language. So, like you just need to write the queries for that and it uses this schema-based system and it also has a good community support. So, now why GraphQL basically? Now, as it's core, it's a language for querying the data this from the client-side applications. Which means the client needs to request whichever data is required at its side. And due to this, it improves the client-server interaction by its enabling the former to make precise data request and obtain no more, no less, but exactly the data which is needed to client for a particular functionality. And the other thing is just a single endpoint. Like, there's only a one post endpoint, no other endpoints, no over fetching, no under fetching as the client gets the data which is required only. And the other thing is autogenization of API documentation is done while you are building the GraphQL APIs. Which means if you write a code, then the documentation of that code is directly done in the GraphIQL like you. Now, let's learn about the GraphQL. We'll like learn some concepts about schema, mutations, queries and the subscriptions. So, first of all schema. Now, basically schema is the object type or like we can say that we can find that which data is available in the API. Which means we can say that the schema holds the complete definition of the API or the complete structure to be more precise. So here, my schema name is product and I have various fields such as product, ID, name, category, price and this. So these are the fields or we can also say it as variables. And this ID, string, float, these are the built-in scalar types which we can also say as the data type. Now, this exclamation mark, it means this field is... So basically this field is required or this field is a necessary field. And we can also build our own scalar types such as list of an object type or something like that. So basically, and also here, by using this schema, it's easy to find the usages of a particular type of data type that which data type is used more. And also we can track the usages of schema by using few tools such as which schema is used more and which query is used more. The next is mutational. So mutation are used for changing or like basically for updating or like changing the data to the server. So now here I have mentioned one mutation named its add product and the required variables which are needed to add a product into the server are passed into this add product mutation. Now here I have mentioned the fields which I am expecting back from the server on the successful operations. So these are the fields which I am expecting back from the server on the successful operation. Now here it is written the same thing. So here it is written the same thing which the server has stored, but you can also return some other objects according to your needs. For example, by adding a product, you want to send the total list of products to the client, then also it could be done by making some of the changes. So here I have shown this mutation using the query variables. So we can also declare these query variables and these query variables are passed to the mutation and then they are passed further to the add a product. Now query query is basically used for fetching data from the server. So here you can see that the query has exactly the same shape as the result. So this is the result and the query is the same shape as the result. So this is the benefit of GraphQL as we know that we will get back what we expected and the server knows exactly what fields the client is asking for. So here I have asked for only the five fields instead of the complete object. And that's why I'm getting the response from the server of only five fields from the query. Subscription. So a subscription is a real-time connection to the server. So it's a real-time connection between client and the server, which means a client subscribes to an event and then this server gives the data to the client when an event is occurred. Here I have subscription on this add a product mutation, which means here by hitting the subscription query to the server, the client is connected with the server in real-time and the connection is established. And now whenever the server performs this particular mutation of adding a product, then server sends this information about the added product to the client back at the same time. Now this can be used for real-time chat services or OTP and some few features too. Here actually I have mentioned the example of mutation, but like you can subscribe queries also and like few other events too. Now there are multiple like tools or libraries for building the GraphQL APIs in Python. I am particularly using Graphin library as it's more convenient to me. Now Graphin uses the code first approach. Now what is code first approach? The code first approach means we start by coding the resolvers or we start by coding the APIs and from the code we get this schema generated as a by-product. That means we'll still have a schema, but instead of manually created, it is created by running our API codes. While the schema first approach, which is used by area, then schema first approach indicates means like we need to first define the complete schema. And according to that schema, we need to develop the APIs and the API structures and the restriction should be matched to the schema. So it's not that schema first approach is better than code first or code first is better than schema first. It's just a matter of convenience. So now let's start building the GraphQL APIs in Django. So we'll be using Django, Graphin and the PostgreSQL as our database. So these are the few steps for building the environment. So I have set up a Django project named project. Then so like you have to install this Graphin. So like using this command and also Graphin Django. Now why I am using Graphin Django is because it provides Django object types. Now why Django object types are required, right? So if I want to build a schema, which is equivalent or which is similar to my Django model, right? So suppose I have 10 fields in my Django model and I want that all 10 fields to the GraphQL schema. Then then basically using this Django object type, I can map that Django object into a GraphQL object or into a GraphQL schema. So like directly and that's why Django object types are very useful. And then also you need to make some settings like some changes for setting up the PostgreSQL. You need to also add this Graphin underscore Django in your install app and this code snippet into a settings.py file. This project is your project name of the Django. Now let's understand a few concepts of Graphin. So first one is the object type. Now the object type is a block or we can say it as basically a mediator, which is used for defining the relations between my fields and my schema, which means here. So basically I'll be having a few number of fields in my product model, right? Now I need to define a relation between these fields and my GraphQL schema. And for that this object type is used for defining the relation between my schema and my fields and my fields which are required in that schema. Now how to build schema? So basically schema is created by supplying the object type of each operation, which means that the relation which we have defined between my fields and the schema. That needs to be passed for creating a schema. Now schema can be built in two ways in Django. First is using this Django object type and the second is we can explicitly mention. Now let's have a look on building schema using Django object type. So here what is happening that I like suppose I have 10 fields in my product model and I want to map all that 10 fields into my GraphQL schema. So for that you can use this code snippet like you just need to pass a Django object type into and so like build a class and then this prototype needs to be passed to the query class. And your GraphQL schema and your GraphQL schema is generated. And if you want to mention the explicit fields, suppose you want to only mention only the four fields in your GraphQL schema, then you can just manually add these fields and you can just assign the variables. And then the schema of only these four variables will be generated instead of a complete model. Resolvers. Now basically this resolver is a method which will resolve the query of a particular schema, which means here I have two schemas which is login and users. And I have two functions which is resolve underscore users and resolve underscore login. So whenever a request will be made for a login schema, let's say then the resolver function of the login schema will be called and this like business logic will be executed and the client will get the response like whichever is needed. So resolver method is used for resolving the queries of a particular schema and make sure that the name of the result function, it should be resolved underscore your schema name, which is your login and your login tool. Now there are three main parameters of the resolvers. One is parent, info and quarks. Now parent is basically, it's the parent of the current root, which means here I have a particular query, which is a joint query. And so now here the parent of repository is repo. So that repo will be passed as a parent. And in info, now the info variable contains some meta information about the user authentication and like something like that. And in quarks, quarks basically carries the graphical arguments such as suppose when you were, so suppose when you were executing a query, which uses the variables then that variables are passed in the quarks. Scalers, now basically scalers are nothing but just like the data types, right? So it's a string float integer ID. So like, so basically cracking towards many inbuilt scalar types, but also you can build your own scalar type such as like just create a schema of the data type and just reference that schema into a particular schema and pass it as an error. So you can build your own scalar such as a list of objects or areas or something like that. So it can be built according to the requirements. Now we'll be looking over the code snippets for queries mutations pagination and authentication pagination is basically sending a particular data or a particular bunch of data to the client instead of sending the complete data. And the authentication is for sickling the backend API's. So here is my file structure. I have made two gen words one is e-commerce and one is users, and I have defined a file name schema.py in which I'll be writing all the queries and all the mutations of e-commerce. So like read that app and here is schema underscore users.py file in user so that you can build a file with any user so like with any name, but in the last this all files needs to be mapped to a schema.py file of your main project. So that you need to take care of. So let's see for writing the queries. So what's the steps are for writing queries. First of all, you need to mention your schema, which is done here products schema right and I am building this by passing the product type and this product type is using Django object type, which means all the fields of the models will be mapped to a GraphQL schema. Right. So after mentioning our schema, like you know to build a resolver function which will resolve your query. So here is my resolver function. And in this I am passing a product name. So, like if you want to pass any variables from your query. Then you can pass like this year I have so like mentioned here and so like it's a string so I need to. So like also mentioned that it's a string. And then this variable is passed to the result. So like this resolver function and then this variable is used into your logic and then the appropriate output is written back to the client, which is required. So these are the concept used for building a particular query. Now writing mutations for mutations basically you need to variables right for a particular object which needs to build an object or which needs to build a class right. So for that I have so it's the same like here I have mentioned add a product schema by Django object type then I have one class name arguments. Now in this arguments class. I need to define all the fields which are required for their particular mutations, along with defining that particular data types, which is your string float and I need to also mention which is required by making record is equal to true. Now all these variables are passed to a muted function. And now inside the muted function, you need to write your business logic or saving the object which I've done here. This is production and I'm building my product model here. And then I'm just returning the product which is saved on the server. But you can even. Yeah, so like basically you can return whatever you want. It's not just only the sale product. You can also return a complete list of products. Or you can also like it on something else. So it's according to the requirements. It's not necessary that you just return the product which is saved. Now, by adding this patch into your URLs.py file. And by hitting this local host slash GraphQL. So this particular endpoint into a web browser, you'll be able to see this graph Iql view screen. Right. So basically here is a space where you can write your GraphQL queries or mutations. And here you'll be getting your server responses. And on the extreme right hand side, this is the API documentation with GraphQL provides. Which means and this documentation also has all the fields which are required for a particular mutation and the response which will which the client will get is mentioned here. So if a developer has developed a mutation, then the front end developers just need to pull the code and just need to run that and they'll be able to see all the mutations and the parameters which are required. As a response which will be so. So like the response which will be given to the client. Now for pagination for pagination, it seems as we are fetching the data from the server that means we need to build a query. So I will a query in a similar way only, but I introduced two variables. One is first and second is jump right and then I'm using the concept of Python slicing. Now what is Python slicing and how it can be used for pagination. Suppose I have 20 objects and I want to get the objects from 11 to 15 right. So I can just make first is equal to five and jump is equal to 10, which means 10 objects will be jump and then the first five objects will be taken right here jump then. So first 10 objects will be jump and then the first five objects will be taken as a response and then that only five objects will be returned to the client as required. So, like this is how the pagination can be incorporated using the same queries. Now for authentication for authentication, you need to add this code snippet into your settings.py file, which includes this graph in schema this and this authentication backends. So like this. So basically, these are the middle words which are required for building the authentication. Now, so, so basically in my previous slides, I have told that you need to map all this, all that queries and mutations into a single schema.py file, which I have shown here that I have built project schema.py file. And in this I have mapped the queries of e-commerce here and the mutations of e-commerce here. So you need to map all this or your queries and mutations into a single file and then you for authentication, you need to add these three lines for the authentication. Now by adding these three lines, you'll be able to see three more mutations. One is token auth, verified token and refresh token and then using these mutations, you can generate the JWT tokens. So let's see how that first of all, I have created a mutation for creating a user and then I have created a user with which username is Nisar, it can password is also Nisar. Now I have one mutation which is token auth, which is obtained by adding this line. Now by hitting this mutation, I'll get a token which is a JWT token and now this token can be saved on the front end side for the further API calls. And also this mutation can be used as a login mutation. So as soon as a user passes the credentials, these credentials can be used to generate the token and the token can be saved on the front end side. Here I have added this code in my queries and mutations. So if I'll not find a token from my API request, then I'll just declare it as the user is not logged in. So now basically if I'm passing a token in the headers, then it will be an HTTP request. So for HTTP request, I'm using Insomniac line and here I have added this token prefixed by a string JWT in my this field in my header. And now I'm just querying the post API on localhost 8000 slash GraphQL. And by hitting this mutation, I'm getting the output or the data from this like so like this data. So means from the server, right? And if I don't include this JWT token, then I'll be getting this error message not logged in. Which means the server won't pass the data to the client without this authorization token. Here are some helpful resources which I have used and these are also resources which could be learned for like which could be used for learning the GraphQL in more detail, right? So you can find all these code snippets in this particular GitHub repo nissar 1499 slash Django dash e-commerce dash GraphQL. We are building a type of so like basically we are preparing a boilerplate of an e-commerce, right? By implementing the GraphQL APIs in Django. So my goal is to provide a complete backend code on which a complete e-commerce website can be built using only a few modifications. So if you want to get updated with this project, then you can watch or start this on the GitHub, right? Also, I'll be happy to discuss few ideas which are implemented, which can be implemented in this project. As an ending note, now I would like to add one point here that there are a few cases where REST is also preferred over GraphQL. Like if you want to implement a caching system, then get a request in RESTful APIs can be cached while caching the GraphQL request. You need to probably build your own mechanism, right? The other complexity sometimes routing multiple endpoints are much easier than applying then just applying the join operations on your GraphQL schema and just building a very long schema as it would take much more time to get a result by the result function, right? So thank you. Now, like I would like to take the questions from the audience. Okay, thank you. We really need a way to show you that there is people listening to you. So thank you very much. We have a question. We have time maybe for one more. So if anyone has more questions, click in the Q&A and ask. So the first one is, what are the cons for Graph SQL? So GraphQL, sorry. So what do you think are the cons on using it? So basically, what I mentioned is that while using GraphQL, the caching system, so basically for caching your APIs, you need to build either your own caching system or in REST APIs, basically the GET requests can be cached very easily instead of GraphQL. Also in GraphQL, there is one optimization problem, which is N plus one problem, which means, suppose, so like basically in Graphin, if you have a lot of data and if a query is executed, then it would take much more time rather than REST. So there are a few cons of GraphQL, but also there are some benefits, which means only single API endpoint and also no over fetching or no under fetching. So like you should take as per your requirements. Okay. So last one, and you have like a less than one minute to answer is, which are your favorite tools for generating Python code? Generating Python code, okay. Yeah, no idea. Yeah. Ah, from, sorry, he's I'm from GraphQL schemas. So to generate Python code from a GraphQL schema. Okay, so like, sorry, I'm not able to get the question that do I need to build a schema from? I think the question is how you create Python code using the GraphQL schema? I think that was a question. Okay, okay. So basically, for creating the Python code using so from the GraphQL schema. So what first of all is done is we need to define that which fields are required in a schema, right? And then just that fields are required to be added in your resolvers or your mutations in which the class argument is defined, right? So from that only your fields in the queries and the mutations will be fetched from the schema. Also for building the schema in Python, or basically, more specifically in Django, there are two ways which I have shown one is like you can explicitly mention the fields and the other is using Django object type. But if you're building schemas in flask and all that, then you need to basically define the complete schema. And also for tracking the schema, there are a few tools such as GraphQL inspector and all that where you can track your complete schema. Okay, so sorry, I going to interrupt you because we are out of time. There is a channel in Discord. So the name of the channel is Stalk GraphQL API in Django. So you can, people can go there, you can continue the discussion in that channel. So thank you very much for your talk. Thank you for presenting in EuroPython. Thank you. Have a nice day. Thank you. Yeah.