Spring Boot with GraphQL Query Example | Tech Primers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
are you guys bored off with a rest implementation you would have recently heard about the graph QL and how Facebook has used graph given in implementing that mobile application if you don't know about the graph given you can check out my video about graph QL in this particular video we are going to create a spring board application with graph here and let's see what kind of magic graph QL provides let's get started [Music] press the bell icon on the YouTube app and never miss any update from tech primers just to set the context graph QL is a query language similar to rest it is not any database the names sounds something different because it is named as graph QL however it is just graph query language it is just another language for your API similar to how we create a rest implementation or the rest platform or the architecture graph QL is another architecture or the specification which you can use let's see how we can use graph QL in the spring Buddha plication and what it provides this particular example which I am going to create was provided by buzzient I have used his particular code which he gave me I'm using his implementation I'm not going to use exactly his project but I am going to use a different implementation he had provided me with the movie's implementation so he has a movie database and we can query the movie database with the graph key one in a sprin good application what I am going to create is I'm going to create a book repository so we can query that book store and we can see how we can use it in spring Boop Thank You button so he saved most of my time in exploring about crop cool he had created and given me a repository Apollo that it into github so I have been using that so this is the repository which he had created so I will give the link for this in the description as well and also I'll give the link in the description for the project which I am going to create so let's start off by as usual we are going to go to spring initializer I'm going to use the spring boot 1.5 dot nine the group ID for our project is comm tech primers which I'll just say comm tech timers crafty one the artifact I'm going to name it as spring boot graph cue one example the dependencies which we require are I'm gonna use the rest controller right so I'm gonna use a spring MVC so I'm just adding them so there will include spring MVC in that if you model web has the spring MVC along that you don't get so this is going to have an embedded Tomcat because it's just being good application I'm going to use the hsql database for storing the book repository I'm gonna use Lombok Laubach is the compiled I'm annotation processor I'm going to create a model called book for that I'm going to use the Lombok annotation that's it from the spring initializer we need to add the spring boat graph QL dependency that I will add it directly in the project let me unzip and open this in the intelligent meanwhile the project is unzip let me open it in IntelliJ the project is getting importer meanwhile let's see what is there in the format some of the palm has the web I think I also need the JPA right so let me include this bring boot JP as well so I need the JPA because I'm going to use JP a to access the data from the database the hsql database so I'm going to add the JP as well so I'll not include the version so let springboard figure out what version it means so we need the JPA we are going to use the web spring boot start of it we are using hsql database and we are using log book and there is a dependency for test that's okay now we need to add the dependency for the graph QL so I already have the graph QL dependencies injected into my life template so what I'm going to do is I'm going to select my life template and this is going to add the dependency so these two are the dependencies which we need to add for supporting graph q1 in spring booth so this is created by graph QL graph QL is a query language created by Facebook and this jars are created by graph QL themselves so let's go and create the project so this is the project structure as we already know I'm going to use some other port so I I don't have the default port as free so I'm going to use the port let's say 8 0 9 1 I'm going to use support eight zero nine one eight zero eight zero it's already used by my Jenkins which is running in my machine so I'm not going to disturb that let's go ahead and create the packages like what packages do we need Abby need a repository obviously for storing the data what else we need we need a model package we need a resource basically the controller and I'm going to call it as a resource so I will just say resource because it's chalice resource and we need services right I'll create service so let's start off with creating the schema for the graph you will database so I'm going to create something called box dot graph QL so this is going to have the schema which we need for exposing the rest endpoint so what is a schema right so there are different concepts inside the schema of a graph QL so we have queries and we have mutations we are going to see queries in this particular video let's create some schema we need to define them as types so we need a book type so we are going to create a book repository right so we need a book time so I'm going to create a type called book and I'm going to have some variables or basically these are data for the particular book or the metadata of the book so iesson let's say Ison is the metadata for which I wanted and this is going to be a string so I'll just define it as a string we also need the title I'll just say try tool also a string we need the publisher and we need the author so author can be more number of people right so I'm going to define it as an array so I can define the array like this I have authors and also I need the published deep I'll just make it a string for me so I have I need the isand title publisher authors and the publish date so I have these as mine so now I need to expose my final query right I need to expose my query so I need some type of message which the user needs to send me so what type of query the user needs to send me is the user can select either all books right I'm going to say the user can send me all books and this should return me list of books and also the user should be able to send me a book which can be expecting an ID and this could be of type book so we will see this in detail once we create that query so what we are going to do is this gets converted into something like this in the graph QL if see this right so this is the final graph QL language for the query which the user will be providing and in order to create this we need a schema and this is nothing but a scheme so we have done the query now we need the schema so we need to define the schema saying what type of scheme is and I'm going to provide the schema as query Dondre so we have created a schema which is going to have query so we need to provide the query to this particular application and what type of queries we can provide we can provide either all books or we can provide a specific book and what type of data the book has these are the different type of objects the book can accept so let's now create the resource right we'll let let's now start off with the resource I'm going to name the resource a book resource like how we do this is going to be a rest controller as usual using the spring MVC and we are going to map this as request mapping as let's say books so I'm just going to say as breast lash books like I'll just say red slash books that's it I'm not going to add anything else here I'm going to add some post mapping and this post mapping needs to return something let's see what is that it is going to return and never need the books right I'll just create a book so we need to get the body so it's going to be the question body and this is going to be the query which the user is going to provide right and let's fill this later on let's come back to the model we need a model we have the book model I'm going to create the book model and I'm going to use the Lombok before that I'm going to mark this as stable and entity because we need to use the same model to persist into the database I'm going to use spring JPA so I'm writing it as table and entity let's add some fields the ID field is going to be is n so we also have other fields we have the title we have the publisher we also have the author's you we also finally heard they published it now I'm going to use Lombok for annotation processing I'm not going to create that getters and setters I'll just going to say I need in our constructor I need the all arcs constructor I need some getters and setters so I'll just say getters and setters that's it right I don't need anything else I've added all my guess to create a repository or the JPA repository I'm going to create it as book repository and there's got to be an interface and this is going to implement JPA a repository wit book as my model and what is the primary key the primary key is going to be a string because it is ID and be a market a string that's why repository is done our model is done our resource is also almost done now we need to create different services so there is a concept of data pictures in the graph queue so we can write a graph QL service so I'm going to write this as a service this graph QL service needs to load the data all right so we need to initialize the graph QL service or the graph QL with water whatever we added here so whatever schema be created we need to inject that into the graph QL so we need to have a resource here I'm going to create a resource I'm going to add the you you though clasper bookstore draft keyword yep the name is correct so this would be loaded as a resource now what else do we need so we are going to add a post construct which is going to be called when we have the graph QL service initialized so I'm just going to name it as a load schema what I have done here is I have already injected that into my life template and I'm going to just say load graph t1 this is going to add me the code which I need for loading the data into the graph tool so this code is going to be the same for any graph QL data load so let's see what this code does so I'm going to use the resource I need to get that file which is nothing buddy graph QL resource of the schema so this is the graph QL schema we need to know the schema into the graph QL service once the file is retreat we need to use the file to parse it so what we do here is we have it named a schema file that is a schema parser provided by graph q1 so if you see here this is provided by graph q1 API and we are passing that particular file and loading it into a type definition register which can be used by graph QL so graph QL will refer to this registry which has the data of this particular file and it will keep track of what all objects are there and what all objects you are requesting so based on that it will provide I keep a track of it so that is what happening that is what is happening here the next time next one is the runtime wiring so we need to wire these schemas right what needs to be mapped to what so that they will be happening in the runtime wiring which we need to implement now so I'll just create a runtime wiring method let's implement that in a while meanwhile once the wiring is done we need to provide the wiring and the registry to the graph QL schema and make it executable the same schema is should be built finally and we need to build that as a graph q1 so let me create that growl so graph QL is a public API this is the public API fuel go inside this is a public API provided by the graph QL team for our usage so we need to execute any query on this particular API so this is the API which is exposed to everybody we will have to use this from our core source so let's see how to provide now the runtime wearing it so runtime wiring is having a API called new runtime wiring dot build so this is going to build our runtime wiring however we need to provide what type of wiring we need to add so we have provided two types of objects right so we notice the bookstore craft you will be have two types of queries right all books and books so we need to add some data fetches so how does the API or the graph you'll know which table to get what data from right that is what we need to map in the runtime wedding so now we need to add the schema type and we need to add these all books and books to that particular schema type right so we are going to add that into the runtime wearing here I'm going to say it type and we are going to use this particular method where we can pass the type name and what type of cooperation we want to perform and it is basically a builder so we need to have query that's the schema which we are going to use right schema type and we need to provide what type of wiring we need so I am going to say type wiring and this type wiring we need to provide different data features so in our case we have two types of data features one is the all books so I'm going to say data features we need to provide a data feature here so data picture is another class which we need to provide here along with what type of name being had so all books is the name right so if we go here all books is the name which we have here and we are going to provide a data feature here so I'm going to say all books data feature we need to write this particular implementation let's do that later and I also need the book that's what we have right yep the book 1 so I have a book and I'll just say book data-feature finally I need to build this so that it can be returned let's say returned but I think I can replace this as like this I need to create these data features what I'll do is I'll just create these data features I'm going to call this as all data fetch on and this I'm going to call it as book data fits on and these we need to inject it here these can be created as a service so let's create that I'll just do one out of wire and then we can go ahead and create them you so once I implement it you will know why you rate the data feature so for now understand that we need to provide the mapping for the schema to fetch the data if somebody calls all books if somebody is providing all books in the query how does it fit the data that is what we are going to do here I'm going to create a new package inside service call the data feature let's create this class since idols you and these data pictures need to be component they need to implement ready to implement the data feature interface so this is the book one so I just need to provide the book here I'm going to provide the book and let's implement this data feature let's go to the all books same way this needs to implement say in the latest data feature but here we need to provide the list of books if you notice here we need to provide the list of books right so we will provide the list of books here so list of books because this can return a list let's implement these methods so first let's go to the all book data feature so what do we need in the all book data features basically it needs everything isn't it we need to inject the I'll make it as a component and we need to inject the repository here so that we can retrieve the data from the book repository so we will use the book repository directly so this will act like an abstraction to the repository so the data fetches are like service component which is going to interact with the repository and it is going to get the data for us so using the book depository I am going to do a find off so in respective of what type of data fetching your enrollment you are setting it is going to retrieve all the data from the database and how graph QL does that is based on the parameters passed from the schema it automatically filters up so let's go to the book data center now we need to do the same we need to author by the book repository because we have the single repository right we have a single repository using which we are going to get the data notice here in the book repository we are going to find only the one which is based on the ice in number right which is based on iesson so how will we get that so from the data fetching environment we can retrieve it using gate argument we can get it using the gate argument and this ID is nothing but the value which we have ported here so we had mentioned that provide a book heidi and we will provide it and that is what is here and there is going to be the isin this will be a string and using that Ison we are going to core the book repository and we are getting it because the primary key is a string which we have defined in the book here right now we have created the all book feature we have to look at that book Facebook data feature now we have to okay we what we did okay we don't need to build it here alright okay so I have built it already so that is it me have created the runtime wiring in this service now we need to provide this service for the resource so I like just expose our gate method get' okay so I'll just expose this graph q1 so I'll just make this private so that nobody knows that it is there so this particular graph QL service can be exposed to the resource mouse over the books resource now we'll be using the graph q1 service so from the graph keyword service what we need to do is we need to provide thee we need to get the graph QL and we need to execute that so we can directly execute it and we need to provide what query this that's it so we are using the graph QL service to get the graph QL public API and using the execute method to provide the query the query will be nothing but the query which we will be providing from the postman so let's see that I think we are almost done right so we have okay we didn't push the response back isn't it so we get the response in an execution result from this execution toussant we can directly push it so either we can extract it out so we can do a get we can do a get data and extract it out or we can directly give that so if let there are errors right we won't be able to track if there is a get data what we are going to provide is we are going to provide the whole the whole execution result we are going to wrap that into the response object we are going to use their response entity let's it has an object of type we need to provide the execution result and also what type of response it is so we are going to say his shady piece a status okay I'll just say okay and let me change this to response type of object that's it so we need to you can remove this now so what we have done in the resources we are using the graph QL service to get that particular public API and execute the query which we received from the res slash books using that we get the execution result and that execution result we are wrapping up into a response entity so that we can get that back in the UI or the client side so I'll just summarize what we have done here we have treated a model called book we character JPA for storing this we have created a book resource there we are using the graph QL service inside the graph QL service we are loading the schema into the graph QL API this is the graph QL public API where we have to load the schema in order to load that we need to wire it in the runtime using the runtime wiring provided by the graph QL API we need to provide these schemas and inject the data features which fetches the data we have to get to data features one counter broke data picture which will query based on a particular ice in the other one is the all books data fetcher it is going to query all the books from the database and also we need to unload the data back into the database side they have not implemented that let me write one more method here so what I will do is I will do load of load of low data into it just cure let me load the data into the hsql into a word we need to get the I'll just auto wire the book repository because we need to load that a tie-in to the Book Depository isn't it let's load some book information so I'll just use this stream API I'm going to use team dot off and we need to provide different books from the books I am going to stream I'm going to do a for each for every book I'll just save it into the book repository so that is what we are going to do but we need to provide the book right what is this book what is this we need to provide the book and just create multiple books let's create some books right I don't know the name of some books let's go on to worry some books here let's do some cloud books let's get some name of the cloud books so that we can use it okay let's use this book okay book of clouds pour out this we need to provide a nice in so the Ison number we can provide let's say as one two three for simplicity I'm just provided we need to provide the title didn't try query it okay then the publisher it is Kindle let's do it as Kindle edition now we need to provide the author's I'll just add the author name what is author author is let's add water so there is only one author so I am going to leave that as is the publish to date we need to give the published date where is the published date of this table let's make it simple right I'll just going to I'm just going to add it as a November 2017 similarly I'm going to add some few more books for simplicity I just create some more I'll just say ice NS so on to floor let's say I'll just say cloud architecture and engineering I'll just say oh really for simplicity I'll just add some names which we can relate it let's add one more book let's add three books okay so Bihar create we are creating three different books and injecting that into the repository here so before loading the before auto wiring the graph q1 we are going to load the data into the hsql database notebook books into 0 positive so that's all I guess apart from that we have created the books schema for the graph q1 which can be loaded into the graph QL API and also I have chained the port number here server at port as 8 0 9 1 so that the Springwood application runs on this particular port let me start the spring boot apllication ok this is showing an error because I have Java 9 configure to let me change it to Java 8 let me do a clean install so that I don't have anything the bill to fail due to some test case but my clean install got succeeded let me run this particular build so I'm going to run this spring port main class so that it can start the Tomcat so this is using the embedded Tomcat as you guys know it is a spring good application so let's check if you notice here there is an error here let's check what is the error the error says there is a schema problem which is 1 which is 1 let's check what is happening the schema I think I know what the problem is here we have put books instead of book so let's try this once again now yep the spelling buta plication is up now and it is running at our age 0 9 1 and you can see hardware standpoint is registered let's go to postman so I am going to use postman to query the post so the URL which we are going to access is localhost colon 8080 puts our book let me go and check its called books I'm just saying books and we need to provide a body and it's going to be a wrong message and it is going to be text we need to provide text and we need to create this graph QL now so based on how the schema is defined by how to create something similar so we need to provide a query and we need to provide something similar so let's add the schema here initially let's go and get all most we need all books right for the first time inside the books we need to provide what all fields we need so inside the book we have ice and title publisher authors like let's do only iesson and let's say a title if we query this this should return all the books there are three books now it should return all the three books which the corresponding iesson and the title let's do the send now and if you notice here we got the response and this response things all books whatever we said we wanted and it is providing Ison and Titan I also need the author's right let's provide authors here notice here I am not changing my rest in point however I am changing whatever I am posting here based on whatever I am posting my response is going to change notice here the author's is now present same way if I need the publisher I can provide the publisher and also the published date when I say all these all those information are going to get populated if I don't want any information I can't remove that so let's say if I don't want anything else I just want is an unpublished it it can do that as well right now we got the all books they tested the all books and all books is working fine so this is what graph QL can provide so graph key word is powerful because you have a single restraint we did have only one dressed in point but he controlled the responses based on that now we can't Lloyd the book as well if we need to query based on a particular is and do you know what is an is n we know and I sell of a book we can't do that as well so we can provide the book we can provide it like this we can't Reuter iesson what is the iesson let's say one two key that's it if we provide this we can still provide what I want from that eysan so I want it title of the book if let's say I want only the title like in this eighth item and let's query this and if you notice here it provided the title of this particular book which is one two three I also need the author's I also need the publisher let's see what happens notice here the book information came as we saw let's change this to 1 to 4 there is going to return the book information of is and 1 to 4 notice here we have not changed the rest endpoint the resource is still the same just that the value which we provided for the schema which we provided is completely different so this is crab key 1 so this is the graph query language now I wanted all books along with the books what do I need can do that as well since we have greater the schema in such a way that we can provide all books and books at the same time we can do this we can provide this as well you can say title and let's say authors what is here all books is going to return all my books along with all books there is going to be one more block called book that is going to return a specific book so with a single rest endpoint you can control the data which is coming in so the data model which we get is controlled based on whatever we pass here this is why graph QL is very powerful if you don't know graph QL is used by our Facebook application the mobile application inside your Facebook has only one dressed endpoint and graph QL is the technology which is powering Facebook so this is how you can create a graph QL in a Springwood application so I'll just summarize what we did in this particular video so what we did to create a springboard application with graph QL we created a graph QL schema which is nothing but this keema we created a schema which has a query we are going to use query type to query the graph QL database and there are two different query types one is all books and one is book so those are the two different types book is going to have is and title publisher authors and then published date we are going to create a model called book we created a model called book book repository is used to query the book we are using the JPA spring JPA here and we are going to expose the resource as the rest slash books inside that we have a post method so we have a post mapping here in the post mapping we are going to get the body which is nothing but the message which may pass here there is nothing but there is a request body and that particular request body we are getting as a query that particular query needs to be provided to the graph API in order to create a graph API we are creating a graph QL service inside the crop QL service we are automatically loading the data into the hsql database we have loaded some three-book information into the hsql database using the book repository then we need to load the data into the graph QL not the data assets but a schema we need to parse the schema so we are passing that using these statements we need to build these kemar now inside that we are creating at runtime wiring then we create the runtime wiring we map that with the graph QL schema which we created and they provide what type of data fetches we need to add for each query here we are providing different data fetches for what type of wiring so for all books we are creating a all books data fetcher which is going to retrieve all the repository information so which is going to fetch all the data from the book repository the other one is the book data fetcher which is going to query only the particular is n number so that is what we have done in the graph QL service we have exposed a gate graph QL to provide API back to be associate so that is all we did apart from that I just created a different port for this for input application and they started off with this using the postman we created a body we created a request body here for this particular rest URL with post mapping we do not add anything else we just added a raw text with this body and based on this if a query we can see that but let me modify this I think I had added something here so let based on this epic query we can see the response which is successful here so this is how you can create a graph QL versus spring integration so I hope you guys understood what is a graph QL and how we can use graph QL in a spring good application do let me know how whether you guys understood or not or let me know what I need to do in the next video what I will do is I will try to create data into the graph to your database or greater into the hsql database in this particular video we did only get on weighted only the fetching part from the graph q1 in the next video I'll just show you how to do create and delete or even the update that's it for this particular video I hope you guys liked the video if you liked the video go ahead and like it if you haven't subscribed to the channel go ahead and subscribe to it meet you again in the next video thank you very much [Music] [Music] you
Info
Channel: Tech Primers
Views: 83,445
Rating: undefined out of 5
Keywords: techprimers, tech primers, what is graphql, graphql, REST vs graphql, graphql api standard, graphql basics, what is graphql used for, what is graphql schema, advanced REST specification, new REST specification from facebook, REST specification from facebook, graphql from facebook, opensource REST, opensource REST specification, opensource REST implementation, spring boot with graphql, spring boot graphql, spring boot graphql example, graphql with spring boot
Id: zX2I7-aIldE
Channel Id: undefined
Length: 39min 58sec (2398 seconds)
Published: Wed Jan 10 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.