Master Graphql with Spring Boot 🔥🔥

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up everyone and welcome to daily codee buffer in this video we are going to learn about graphql and how we can create apis using graphql so without any further Ado let's get started so what we are going to do is we are going to understand what is graphql layer how we can create graphql layer we'll go through the basics of that and then we will create the spring board application with that we are going to create different apis using graphql so first let's understand what is graphql so generally when we create the application we create the application into different layers so we create the application into representation layer your business or service layer and your data layer okay generally these are the layers that we create to build our application to make it cohesive okay so generally your entire business logic comes here at the business or service layer and your data layer your database your whatever the database that you're using everything comes at the data layer and for representation layer what generally we use is we create the rest apis right this is what generally we create create and based on those rest apis we create the uis and ux for those apis but at the same representation layer we can create graphql so graphql comes at the representation layer your interaction layer how you're going to interact with your data how you're going to interact with your business logic that's where the graphql will come into picture so rather than rest apis rather than creating rest apis that works on HTTP you can create graphql layer you can create graphql which will help us to understand or which will help us to fetch the data or modify the data based on the requirements okay so that's the layer that we are going to create now how it is going to benefit us suppose let's take the simple example of fetching the data okay so you have three applications one is mobile application let me just draw it here you have Android application iOS application and your web application okay these three applications are there and those three applications connect to the rest apis okay and this rest API is having one API called post okay which will return all the post for the different uh devices that we have and this post is going to return the list and list has few attributes suppose ID name title image and other things okay so these are the data that it has now suppose Android iOS and web are the three clients for this rest API so whenever they are calling this post post API okay everyone is getting the same data that's good right because one API getting the same data but what if Android wants to call the same post API but doesn't want the image okay what if iOS needs the data for the same post but doesn't need the ID okay that is not possible it will get that data and it has to filter it out and not display it that's what generally it's been happening right but with graphql what we Define is we Define the schema and with this schema we Define the clients like whatever you want you can fetch we Define that okay we are going to provide all these details okay with post query so now whatever you need you can fet data suppose Android wants that I want to fet the data but I just want to fetch ID name and title then it can pass those query to our schema to our graphql layer and we are going to send those data only similar thing we will do for iOS we will Define iOS client will Define that I want only this data I want only name and title I do not want ID and image then graphql layer will understand that par that and it will only send those data so you can see that how this will help us and how it will help the different client to F data based on their requirement so we just created one layer and any client can come here and get the data based on their requirement so we just created a layer schema and multiple clients can f the data based on that requirement so it will be very fast for processing as well so this was enough Theory let's go let's understand how we can build this so what we are going to do is we are going to build a springboard application and with that springboard application we are going to create this entire schema graph qu layer and everything okay so for that what we are going to do is we are going to go to our browser and we are going to go to start. spring.io okay so here we are going to create the project so what I'll do I'll use the M1 Java spring boot 3. 2 and let me just Define my group as daily code buffer and the demo I'm going to give as graphql demo okay so this is my artifact uh packaging is jar Java version is 17 okay so now let's add the dependency so as we are going to create a spring web project we are going to add the spring web dependency okay with that you can add the spring data jpa dependency myql dependency and everything if you're working with your database okay I am not current going to work with the database I'm just going to show you how you can create your graphql layer now to have the graph layer we need to have the graphql dependency you can see this is the spring for graphql build graphql application with spring for graphql and graphql Java so this is what we are going to add here so adding this now let's generate the project and open an intellig idea okay I'll go to new project not new project open project and and here you can see that we have open the project that is the graphql demo okay and if you open May one main Java resources here you can see that we are getting all the different folders and here you can see that we are also getting one extra folder that is graphql this is the place where we are going to create our schema okay so in of J jabber let's build the application so what we are going to do today is rather not using the database we are just going to use the service layer and within that service layer I'm just going to maintain one list which will give me the data okay so let me just build that one because service layer we have built the service layer enough time just to have the current operations like create update delete find and all those operations okay so let's build that very quickly and then we'll focus more on the graph layer so let me just create the different packages here so I'll Define controller package service package and model package okay you can also call it as domain but we are going to call it model because that's what we are familiar with okay so these are the different packages that we create now we are going to create the models and services now we are going to work with our models so what we are going to build the application is we are going to build the application for suppose Cricket fever is on right so IPL is coming on so we are going to create for the IPL so what I'm trying to do here is I'm going to maintain the players and those players belongs to which team okay so that that two things we are going to maintain here so let me just create the two models for that so I'm going to create player Java class and this is the player record that I'm going to create and I'm going to Define integer ID string name and it belongs to which team so team team okay now team is not there so let's create the team as well so I'm going to create new Java class team and this is going to be your enum okay so here I can Define CSK Super Kings Mumbai Indians Del capitals Gujarat Titans so on okay so this is what we have created and now if you see in the players this is also resol so this is the player record that we have created now within the service package we are going to create a player service okay this is what we are going to Define this as a service here so this is just your business layer where it will help you to get the data F the data all those gr operations okay this is going to be very basic which we are going to create quickly so let's build this so this is still not part of your graph Q this is just a business layer that we are creating okay so what I want to do is I want to store the records okay so for that I'm going to have the list of players here okay let me import AR list and list okay so this is the list of players that I'm going to have let me just add one Atomic integer as well okay this will help me to get the IDS okay to generate the IDS sequentially now we need to have different methods uh method to create a player a method to update a player delete a player get all players and find a particular player so these are the different methods that I need so with the help of this list I'm going to do everything so let me just add one method you can see this is the method to find all players which is returning the players list here okay simple then I'm going to create a new method okay this is going to find a player here okay so find one player based on the ID that is provided okay so what it's doing is it is streaming through the list of players here and filtering out filtering out based on the ID so let me just Define ID okay so player. ID equals to ID and find first so you can see this is just a simple method find one which is taking ID and fetching the data from this list so you can see that we are not doing any rocket signs we are just building simple methods to save over time as well because these are the simple methods that we can build now let's create a method to create a player so here you can see that I am defining a create method which is taking name and team here okay and it is creating a new player player player equals to new player and we are using Atomic integer do increment and get to get the value passing name and team and that object is created and we are adding that object to our list and returning that player simple now let's create a method to delete a player so you can see that for deleting a player it is also very simple I am creating a delete method which is taking ID and that particular ID we need to delete from the list that we have so I am taking the player out from that particular players list okay you can see that with this we'll get the player and if there is no players available then we will throw the exception and what we are doing is we are getting the player and we are removing that player from the list that we have and returning that same player now let's do for the update as well so you can see that for update also it's a simple method okay so we created a method that is update method which is taking integer ID name that we want to update and the team which we want to update so these are the parameters that we took now based on the ID provided we will create a new player so you can see that we created a new player updated player with all the details that we passed now we need to find the player which is already available in the list and we need to replace with a new player okay that's what we going to do so as we did in the delete player to find the player available in the list we did the same thing here to find that player so we did this find first okay we found the optional player available now we checked if player is available then we need to find the index of that particular player which is there in that list once we get the index we will set the new player to that particular index if not we will throw the exception and we will return the updated player you can see it's simple method now what I want to do is whenever I start my application whenever this particular player service is injected to my spring bean factory I need some data to be already already present in this players list okay so for that we will hook into the spring Bean life cycle in the post construct phase and once that particular object is created we will add the data here so let's do that as well okay so I'm just creating a method in it in that method I am just adding few players here okay you can see players I'm adding and which team they belongs to and I am hooking into the post construct phase okay so once the object is created all this particular data will be loaded to the players cool you can see your service layer is created now if you want you can create your jpa layer database layer anything you want that would be same okay we are just concentrating on our representation layer that is how we are going to create our graph service so let's create One controller as well so I'm going to create a player controller okay now whenever we are working with the graphql we need to create a service we need to Define like what everything belongs to and what everything is like what is your apis and everything okay how the client will be able to fet data okay in rest apis we create a controller and we Define different methods and on different methods we Define what it's going to be what is going to be get method post method and everything so here also we need to define a schema here okay so let me create a new file that's going to be schema do graph qls okay this is where we are going to Define our schema so here you can see that your schema file is created now let's understand one more thing Let me just clear this out okay so now you can see that in the schema you need to do similar things what you have defined here so you have defined few models here right so that models also you need to Define in the schema like what is a data that you're going to get back okay so here within the schema you need to Define your types okay like what it is so you have player type right so we'll Define player type and within that you will Define What fields you you have and then you have one team as well right so that's the enum so you'll Define that as well that you have one enum of name team and you'll Define what team you have so CSK Mi all those things you will Define here okay then you have another type that is called query query type is nothing but to F the data okay so whenever you want to F the data you will Define the type query and here you will Define what is the name of the query suppose you are name meing as find all okay if this is the name of the query then when we are calling or when the client is calling it will Define that that particular client is calling this find all query and within this find all query you need to Define this find all query will return which type of fields okay if there are multiple you can Define find one similar thing you will Define here okay so bunch of different queries that is helpful for fing the data you will Define everything in type query the other one is type mutation okay mutation is useful when you want to mutate the data when you want to update or create the data or delete the data so whenever there is an modification in the data you will Define type mutation and within that you will Define the different queries different mutation methods so suppose you you define create you define update delete and all okay so this way you need to Define your entire schema so this is what we are going to do in this schema here okay so let's go to our graphql let's go to the schema and let's do this so first thing was let's create this schema model here okay so we'll Define type and what was the name of the model player and we'll Define within the player we'll Define there was one ID which was of type ID okay then one was name which was of type string and one was of team which was of type team okay so this things we have defined this is the same thing that we did here one ID name and string okay now if I go back to schema what I need is I am placing exclamation that means I always need ID ID is mandatory okay now we have defined team as well so we need to Define team as well what was team it's an enam so we Define enum team and this is what we need to Define values so here the values were what let me just go here and copy this values we'll go to schema and we'll paste those values we'll just remove comma here okay so here you can see that you define two things whatever your model was there you define player and team okay now you need to build the different queries like you need to fetch all the data right so when you go to player service you define different methods right that I want one to find all the data find one data all those different things okay similarly we need to Define here so within this we will Define type query within this query we need to define the name okay so suppose I'm defining name as find all okay so this is the name of the query when I call this find all what is going to do it's going to return the list of player okay so this is the list of player okay so this is what we have defined now we need to build this as well so when we go to controller layer here is something that we need to build so let's go that we'll go to controller layer and here rather than defining rest apis we need to Define graphql layer so first as this is the controller we need to Define as a controller we are not going to Define this as a rest controller because this is not the rest API so basically this is a controller now we need the object of our service as well okay so we'll Define that as well private f final player service okay and it is asking us to create a Constructor so let's do that okay so we created a Constructor and with that Constructor this player service will be injected so we are good here now we are going to create one method that is public list of player okay and we going to name it as find all okay which is going to return player service do find all method okay so here you can see that you created a method okay which is going to return the list of players generally what we do is now at here we Define at theate get mapping to Define this is a get API but here as this is going to be your query in your graph schema right this is going to be query so what is the name of the query find all so this name should be exactly the same find all and here you need to Define query mapping you can see that this is the part of graphql so once you define this is a query mapping this particular two things the here the schema what you defined here and whatever you define in this controller will map together with the name here and with this query mapping so now whenever the data will whenever the request will come here for the query find all it will find here and same thing it is going to return the list of player and this also is going to return the list of players okay so that's one thing so now what we'll do is let's build this let's run this application let's see that we are able to find the data or not okay so let's go here and run the application so here you can see that you are able to run your application and you can see that you have started your application now how you're going to run it okay so similar thing we will go to postman so make sure that you have updated your Postman your Postman is capable to run your graph queries as well and here what I'm going to do is I have created a new request and my request is Local Host 880 graphql this is what I'm going to do why it came to graph is because that's a default path okay if we go to our here okay you can see see in the logs that graph endpoint HTTP post / graph is been exposed because that the default uh URL so that's why we are able to get this so you can see that once I am doing this it it was able to find that there is one find all query already available okay and once you open this you can see that with find all these are the three fields that we can fetch the data for okay so I'll just select this and you can see that the query is created so what is the query query find all this is optional okay you can give any name or you can skip the name name as well okay so what I'm doing here that I want to do the query and what is the name of the query find all is the name of the query and now within the find all query what data you need I need ID name and team this is what I have defined this is my query okay now this graphical query will hit this URL that is my graphical schema graphical service that I'm running and it will tell that okay I need to get data from this query and these are the data that I'm required okay once I hit this you can see that I'm getting this data okay that this is the find all query and within this this are the three data available now Suppose there is a different client available okay and that client says okay similarly I want to find all the uh players available but I do not need ID so you can see that I have removed the ID now so once I query this you can see that I'm getting all the data but I'm not getting any ID I'm just getting name and team okay okay similarly if I tell that I do not want team I just want name and ID okay and I query it you can see that I'm am just getting name and ID okay so you can see that how flexible it is when we create the graph layer so we are able to find data based on the client's requirement okay if client needs two records and only two types of fields they can get it similarly okay now let's add another queries as well another layers so let's go to the intellig idea again let's stop the server now I want to add the API to find a particular record so let's build that so I'll go to the schema and here I'll Define one I'll Define that find one this is the new query that I want to create and this is going to return single player now if I want to find a single player I need to pass some parameters as well okay so to pass the parameters we will Define this way what parameters we need to pass I need to pass ID right and what is the type of the ID it's ID that's it we have defined that there is a find one method find one query which is taking the input of an ID which is going to return the player okay so this is what we are going to build here so we'll create public optional of player its name is find one okay this is a query mapping now what it's going to take integer ID okay this is the input now here you can see that this is the input now how I will tell that this is the input so in normal rests what we were doing is we were defining with path variables annotation or request par annotation if it's a request parameter it's path variable we are going to we were defining with adate path variable so here we will Define at argument that this is this is the argument okay and then here we'll Define return player service. find one method which is taking ID so here you can see that your another query is also created so now if you run this again okay you can see that your application started and graph qu has been exposed here and we are going to postman and let me just refresh it okay the same URI you can see it's only showing one query right if I refresh it now you can see that it is showing one more query so let me just remove this and let me just open find one here you can see that there is one argument of type ID and it's going to return these three values so let me select this and I'll pass on the value as one so you can see that this is giving me all the data and here can see that this is the way we are going to pass that five find one and within the brackets pass the ID and it's going to return me first record okay and similarly you can see that six is not available okay so you can see that this two thing we have created now what if I do not want to use this Postman client you do not have Postman client and you do not want to install Okay so for that there is a graphql dashboard already available so we can enable that graphical dashboard and we can do the query directly there so let me show you that as well so if I go to intelligy idea and if I go to Resource application. properties here I can enable the graphql dashboard so these are the two properties that we need to add that is spring. graphql dogra iql do enabled so it will enable it and what is the path for that path is/ graph iql now you can see that now we are enabling it the graphql dashboard so we do not need to query using the post P we can directly use the graphql dashboard to query it so let me just run this and the server is started so what I'll do is I'll go to the browser and let me just do local host 8080 SL graph iql okay so here you can see that this is your graph quel dashboard you can see that how you can use it okay everything so let me just show graph Explorer here and here you can see that I am having find all query and find one query available okay there is no mutations available so if I want to do find all and if I hit let me just remove this let me just select find all and I want these three records and if I hit run you can see that I'm getting all the records okay so this is your graphical dashboard if I remove ID I don't want ID then I can run it again and you can see that I'm not getting ID so this way also you can enable your dashboard and you can query your graphql from here cool right now let's go ahead and let's build the mutations okay so let's go back to intelligy idea again and now let's work on the mutation so now for the mutation what we have we have to do create update and delete that's the services that we have created right uh that is the create delete and update okay so let's go to the schema here and rather than query now we are going to create mutation so we'll Define type mutation okay now within the mutation I'll Define that I want to create one okay which is going to return a player now within this create what I'm going to take as input I'm going to take name that's the string and I'm going to take team that is of type team okay this two thing I'm going to take as the input and I will save the data so this create I'm going to create here so I'll go to the player controller and I'm going to create a method so I'm going to Define public player create okay and now rather than query mapping I'm going to do mutation mapping okay and what are the input string name and team team okay these are the two inputs that we are taking so both these are arguments so we are going to Define both as argument argument one and argument okay and here I'm going to call return player service do create and I'm going going to pass name here and team here okay so here you can see that your create mutation is also ready so let's restart it and let's see so you can see that we have restarted let's go to postman you can also go to your uh graphical dashboard and do the same queries so I'll refresh it and this is the query and you can see that now we have mutation also available so within mutation you can see that it is taking two arguments that is name and team and it will return the entire data ID name and string so let me just pass name and string here so name I'm passing as abbd Villers okay team is not available so let me just put it to CSK as of now okay so once I put it it should create in and it should send the data back okay so let me just query okay so we need to Define what we need in return okay so here you can see that it created six and it is returning the data so you can see that this is how the mutation is created mutation what is the name of the mutation create this is not mandatory okay you can remove it this is what it's mandatory So within create you are passing two Fields what is the name and what is the team and what data you need in return suppose you don't need ID you need another player suppose surina query it you can see that you getting this now if I do query and find all query you can see that I should get all the records right you can six seven everything is been added here okay so here you can see that your create mutation is also working similarly we can create for delete and update so let's do that so we'll stop the server we'll go to our schema. graphql and now let's create update and delete so for update let me just copy this and paste it one thing I'll do is I need I ID of type ID okay one thing extra I need because which ID I want to update that is something that I want to take this is going to be your update and I will create one for delete so let me just rename to delete and delete will take only ID okay and it will return the player so you can see that this three mutation also I have created now we'll go to player controller and we'll create the mutation so let me just copy this okay and this this is going to be your update one more argument I'll be taking is at theate argument it's going to be integer ID right and it's going to return update it's going to take ID and name and team so you can see that your update is done one more thing delete okay delete is just going to take ID it's not going to take team here and this is going to be your integer okay I'm going to pass ID here and the method should be delete so you can see that three mutation method also I have created now I will restart my server okay you can see that my server is started again I'll go to postman I'll refresh it and now you can see that now I have three mutations available update is also available and delete is also available I'll pass pass the delete one here and I'm going to delete suppose five I'll query it you can see that f is deleted it was created as well because two mutations I called together so let me just delete this and let me just call one thing first let me just call find all so I'm getting six records okay so what I do let me just remove it and I want to delete the six record sixth value okay and I'm quering it I'm getting six back here so which is deleted now if I do find all I'm going to get only four records okay six is gone now similarly the same thing will be available in your browser as well if you go to browser okay and let me just refresh it so if I go here you can see that in the mutation you can see the three mutations are available create update and delete right all the three mutations are there and within the query two queries were available so you can see that we were able to create a complete Crow application here and we were also able to create or enable the graphical dashboard and we also saw how we can query those apis as well so now you should be able to create the graph cual schema and create a representation your graph layers on top of your business layer okay so you can see that how easy it was to create your graphql layer and build application on top of it so if you're already working with rest API try creating graphql because there are many applications many organizations using graphql as of now as of today so this was a quick introduction and quick application that we created using graphql if you like this video give us a thumbs up and subscribe to my channel for the upcoming videos you can also click on join button to join my channel and support me if you have any doubts regarding any of the things that we covered then do let me know in the comment section below I will link this code in the description below for you to check out I will see you in the next video till then Happy coding bye-bye
Info
Channel: Daily Code Buffer
Views: 20,038
Rating: undefined out of 5
Keywords: graphql, spring boot, graphql spring boot, spring boot graphql tutorial, graphql tutorial, graphql vs rest, what is graphql, graphql in one video, graphql java, graphql crash course, graphql react, praphql full tutorial, daily code buffer, graphql queries, graphql mutation, graphql mutation query example, graphql microservices, spring boot 3
Id: eD-1KTK7fGc
Channel Id: undefined
Length: 33min 56sec (2036 seconds)
Published: Sun Jan 07 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.