Effortlessly Consume GraphQL APIs with Spring Boot | @Javatechie

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to Javi in the last graphql tutorial we covered what graphql is its benefits in the industry and also we demonstrated an e-commerce crowd app using graphql right now that we have set up a graphql service it's important to know how to consume it otherwise it would not be very useful isn't it so in this tutorial I will show you how to consume a grab ql service using spring grab ql client okay all right so without any further delay let's get started [Applause] [Music] so before we start writing the graphql client let me show you what all graphql service we have exposed as part of this inventory service so if you'll go to the intellig idea we have created this particular project and if you'll come to this resource we have the get all the product from the DV get the product by category and update the stock in inventory and then add a new order okay so if You observe there are two different annotation we have used query mapping and mutation mapping so for read operation you can go for query mapping for write or update operation or delete operation you can go for this mutation mapping okay now if you'll take the look into the postman call you can find this under the query mapping there are two different service I mean two different graphql query and as part of the mutation there are two different graphql query now if you I I I'll try to access this gate products just click on it and what all field you want as part of the response let's say I need name and price nothing else now if I trigger this query I'm only getting the name and price okay that is what we need as part of the graphql implementation now this inventory service is up and running I need to design the client like catalog service sales team Warehouse service so that they can consume this inventory service grab qu okay so let me design one client then there will will try to access all the methods we have defined in inventory service Okay cool so in case if you don't understand how we have designed the inventory service and how we have exposed the graphql query you can go to my YouTube channel and check out this particular video graphql and springboard API real world example you can understand how we have designed the inventory service now let's start creating the client from the scratch I will take the help from spring initializer now we are using the latest spring board version 3.3.1 just Define all the field now you need to add all the required dependency I'll add the web dependency then we need to add the graphql dependency for spring then also we need to add the wave flux dependency also We'll add the lumbo that's it okay now generate the project and import it to the intellig idea so here we go if I will open the palm. XML we have only added the two dependency graphql wave and another one this W FL okay all good now what I can do let's start configuring the things in the client side that's very simple to understand so my graphql service is up and running on this particular host and put the way we do consume the r API using race template or web client similar way we need to configure a template who can connect to this grab qu service and get the result okay similarly like race template we need a template who can communicate from my application to the grab Quil server okay so for that let me create couple of package then we'll configure the template so I have created couple of package config client dto service and controller class these are the common standard we do follow now first let's start configuring the client or start configuring the template so let me create a class so it just graphql client config you can give any name now I just need to annotate at theate configuration then we need to create a bean of graphql client okay so let me create a bin so this is the client now let me return that return HTTP gra client. Builder do build so now if you'll open the Builder method and if you'll see the Constructor to build a graphql client it needs an argument called web client okay so you know that we can use the web client to consume the race API similarly this HTTP graph qu client need a instance of web client to consume the graphql query okay so what we can do we can create the object of web client here now here just Define the base URL okay what is the base URL to consume your graphql services so if you see here this is what my base URL just copy this and paste it then you just need to pass this instance to the builder then just annoted here at the bean so it's simple right we create a bean of grab client who need the help of web client so we just create the instance of web client we give it to this graph client while building the instance that's it okay so this is the way you can consume the graphql services using this particular client that's cool now let's create our client class and we'll consume the query from the inventory service so let me create a class inventory client or something like that just annoted at theate component now here we can inject the graphql client now we'll start consuming the query but before that the first query we want to consume this one okay so let me minimize this yeah so I want to consume this get products if you'll click on it you just need to Define what all field you want to consume for example if you'll go here let's say catalog service want to consume only name and price as part of the response so just go here and choose the field you need we need only name and price just trigger the query we got the result so you need this query to consume just copy this we'll just configure in the client so just go to the client I'll write a method who will return me the list of product and the field I need so let's say public list of you can Define any name let's say I name it item okay get products or view products now I will create the item better let me create as part of the dto and as part of the response what all field you need I need only name and price so I can Define the name and price find just annoted here at the rate data we need all argument Constructor we also need no argument we Define name and price because that is what I need as part of the response just come here just UT the statement that's it now you need to build the query to consume because see at the end if you'll go to the postman this is what the query build by the graphql to get the result so you need to hit this particular query it is not a raised API so you cannot configure the host and URL rather since this is the graphql you need to relay on this particular query syntax okay so just copy this query syntax then come to your client and Define here string something like that and then paste it if you'll trigger this part particular query then only you will get the result name and price if you need any other field just include the field as part of your query what you need that is straightforward okay now what is next I have the query I have the graph qu client then let me get the result so what you can do just do graphql client dot give the document is nothing your graphql query okay and you want to perform the retrive you want to retrive the response for which graphql query if you see here this is what the graphql query okay I want to retrive the result for this particular method that is what beave as my end point okay so I'll just paste this that saying that okay this is the method consider as a end point and this is what the query I want to execute give me the result of type what as a entity list of class item do class Okay then if I'll just return the statement it will return me the mono of list okay so I I'll name it products the reason this grab client internally use the web client which is part of the reactive programming so you must need to include the web flux and you can play with the mon and flux but since this is the traditional API rather playing with that what I'll do I'll simply block the reactive call I'll get the object of type list of product just return that or better I can directly return from here that's fine okay see how simple is this just Define the query like the HTTP endpoint we Define for the rest API to consume then give that query and specify which method you want to access it will act as a endo and then what is your expected response type okay that's it it's it's dead e like the r API okay now let's try for other methods so close it now the second method is get products by category click on it and just Define what field you need and give the category let's say something what is that category we have Electronics right now if I'll trigger this query I got all the result just Define what all field you need I need name category and price and stock let it be now just trigger the query in result you will find only the field you selected okay now I need this particular syntax to configure as a graph qu query just copy this and here just write another method public we will again return the list of item so Define view products then you need to pass that input just Define the query string graph ql query instead of hard code the query in the method it will be good practice if you can keep it in some configuration file okay since this is the demo I'm just adding it this is the query but I want to pass this category as input I don't want to hard code it right so what I can do I will make it Dynamic using string dot format what field you want to pass that is category and here just Define percentile s okay this field will be mapped here now again same just copy this but which method you want to call you want to call this particular method just paste it and I want the result as a list of item but make sure this API need name category price and stock okay so better what I can do either you create a separate D for this method or just add the field as part of your existing dto okay so it is up to you based on your need you need to design the format of your response so for now let let me add this field name category price and stock okay cool but if you remember in the first API call we need only name and price we don't need those two value category and stock so in that case it will be poped as a null right so to avoid that what I can do there is annotation Json include so it's simple I'm saying don't include if the field have the default value if it is a null or zero or any decimal for float don't include that as part of the response okay so it's it's always good practice rather than creating multiple dto keep a generic dto and add the field as per your need and Define this annotation to avoid the or populate the null value as part of the response Okay cool so go to the client we're done with the second method now let's try accessing any of the mutation method okay when I say mutation method it's read not read write operation okay come here let's say I want to update the stock or I want to receive new shipment so give the ID let's say for one ID one what is the ID we have let me check from the this one right for this laptop we have the price 75,000 stock is 100 so what I will do for this ID I will increase the stock okay currently it is 100 let's see how we can do that in receip new shipment give the ideas 1 and quantity I want to keep as a 200 okay and what all field you need as part of the response just Define that I need only name and price and stock okay now send the request earlier it was one so I added 200 now the stock price is 300 okay so this is kind of right operation let's try it out we have tried for the query mapping let's try for the mutation mapping so let me copy this because I need to trigger this query from my client go here then simply write a method public what it will return a single product object right return the item object and what all field it needs If You observe here it needs to field ID and the quantity you want to update correct so this should be request so what I do I'll just create a DTU class for input request Now define the field private okay so this is my request field ID and quantity now go to the client just pass here item request will be my input then just copy the query string just copy the query from here okay so again this is the hardcoded value which I want to pass Dynamic so I'll just use string okay just remove this then pass the argument we need to pass the two two argument one is the ID one is the quantity okay so for first argument I can Define percentile s for the second argument I can Define percentile D and I will pass the value here so it's simple right get the input from item request. get ID item request. get quantity this is my query now it's simple use the grab Quil client just add the document which is nothing your query okay and what you want to do retrive the result for this particular method and which type of uh response you want you want a single entity of item docl class just block this and you'll get the single object now just create the service class and call each and every method to validate so I'll create a service class here new Java class let's let's say catalog service just annoted here atate service and inject the inventory client then just Define the method and call them so it's simple right I just copy paste because we are just creating the method and in Booking the client so this is this is straightforward right now let me quickly Define a controller class so that we can Define the endpoint from the consumer and we'll try to access through the HTTP let me do that so go to the controller package then just add the class simple guys I have just defined the end point will call the catalog service and catalog service will call the client and it will inbook from the inventory service that is what the simple thing all good now also let me change the port of the server so it's 9191 right so better my client will be run on the 9292 that's good now let's run the app and we'll test each and every H method what we have defined in our consumer but make sure your inventory service which is your grab server should up and running when you are accessing or when you are consuming it let's run it so it is started yeah on Port 9292 now just go to the postman and just new HTTP method not graphql because we have designed the client which is HTTP will consume the grab ql okay so HTTP Local Host onp 9292 what is the URL cut log first let me view all the products okay just add here SL products send the request you'll get only the field called name and price that is what we are querying from the graphql server let's send the request and we'll see see Here If You observe you're getting only name and price so if you in future if you need any other field just simply add in this particular query instead of I mean name price you also want some more field let's say you want stock okay so you can Define the field and based on that just modify your DTU so being a client you are modifying things from your end what you need we have not doing any changes on the server side code server side code will give me the response saying that okay this is what things you ask me I have given it to you now it's your call how you want to map your response or how you want to design your response okay so that is how we are getting only the name and price fine the first AP is working our this particular view all the products now let's view the products by category just go to the postman and it is get only products by category and just give the category type what is that Electronics or something like that okay we got the result based on category we are expecting name category price and stock that is what the field we have defined as as part of the query being a client I'm asking inventory service hey when I will filter the electronic category electronic you give me these many fields name category price stock if you have thousand of fields I don't need all as for my need I only require these four field which I'm getting in future if you need more field just modify the query from the client and design your uh dto okay cool this is what we are trying the read operation now let's do some write operation to validate so let's change it to the post and change the url again this is not part of the catalog service you can define a another client called sales client or any other client who can add the new shipment okay so admin can do that right so you can create some different uh inventory admin service or something like that let's see what is the URL this and item request D only need ID and quantity Let's test it out we know that the uh product ID one is having the stock 300 we we'll try to increase it okay go to the body raw then you can Define ID is one quantity which you want to increase the stock amount 200 more it is 300 currently we are increasing 200 more which will be 500 right just send the request great you got the result 500 now this is what something we send the request from the consumer we trigger the grab server and grab server will update in the DV to validate that let me show you the it really update in the DV or not I will access this so in the request this is we trying to access directly the server side code I mean server side query we got the result which is getting fed from the DV and we got the result 500 that is what we have done here correct from the client we update the stock amount to 200 more earlier it was 300 now we got the result 500 irrespective of HTTP method type or irrespective of mapping whether it is query mapping or subscription mapping or uh mutation mapping you can design your consumer uh based on the HTTP you want to access it okay yeah so that's cool this is what how you can design your graphql client to consume the graphql server okay so that is how I have done for the catalog service if you want you can give a try for sales team or Warehouse team okay so do let me know in the comment section if you have any doubts or any concern feel free to raise your hand in comment section that's all about this particular video guys thanks for watching this video meet you soon with A New Concept
Info
Channel: Java Techie
Views: 5,605
Rating: undefined out of 5
Keywords: #SpringBoot, #GraphQL, #APITutorial, #JavaDevelopment, #SpringFramework, #GraphQLAPI, #SpringBootTutorial, #JavaProgramming, #APIIntegration, #CodingTutorial, #GraphQLClient, #BackendDevelopment, #SoftwareDevelopment, #JavaSpringBoot, #TechTutorial, javatechie
Id: 9uho8hsrJ5Y
Channel Id: undefined
Length: 27min 30sec (1650 seconds)
Published: Sat Jun 29 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.