Spring Tips: @Controllers: GraphQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi spring fans welcome to another installment of spring tips in this installment we're going to wrap up our series looking at all things at controller and we're going to focus specifically on graphql now bear in mind we've already done fairly recently a video on graphql so i'm not going to rehash all of that and i definitely encourage you to check that out for the broader bigger context but suffice it to say that graphql is the latest and greatest addition to the spring ad control controller portfolio it is a technology built on top of the graphql java project which is mature ga widely available widely used et cetera so widely used in fact that it's the implementation that twitter uses to power their graphql api so you know it scales what we've done is working with them in tandem with them we've built a component model on top of the graphql java project that supports spring sort of use cases idiomatic spring component models and so on we integrate it with ad controller now graphql graphql itself was born at facebook sorry meta in 2012. the tension that they faced at the time was they had lots of different services and these services had their own data layers but those services were naturally separated there were individual services and the clients had a conflict they had a tension with that because they wanted the same view of the data they wanted all the results in one co-located view one one fetch one network call as opposed to having to make multiple concurrent network calls possibly at the expense of bandwidth and efficiency and latency um and not to mention battery power if you're on an iphone or android right so all these things meant that they need some way to have their cake and eat it too they wanted to have separate services but they want their clients to be able to call just one endpoint and get all the data it needs not to mention they wanted this to be fairly future proof so one problem that all organizations face is that as you build edge services that adapt the data downstream to the views of the need and the needs of the client you have to keep adding those new views those new endpoints with graphql this is not necessarily true anymore right now with graphql you can have the client dictate what data it needs it can ask for their needs and get only the data that it needs this makes it particularly compelling for edge service use cases and data federation i want to thank you for watching and you know as always enjoy the episode all right let's build a graphql service graphql support is relatively new in the spring ecosystem but it's unique in that it is uh the first time we've built a framework that doesn't have a particular flavor of uh of runtime associated with it so you need http somewhere but we don't care if you're using http uh with the servlet api or using http with neddy in this the reactive stack so i'm going to add the reactive stuff because that's what i tend to choose by default unfortunately at the moment at the time of the recording there is no graphql starter in here but i wouldn't be surprised if that's here by the time you're watching this and they hopefully not too distant future uh so i'm just gonna add this i'll choose um kotlin as well and we'll hit generate say uao graphql [Music] okay the first things first we need to add the graphql dependency now of course it's not yet ga so i need the milestone and snapshot versions of the repositories to be added to my maven build so i'll go back here and i'll choose this i'll choose a snapshot there not because i want that dependency but because i want to be able to copy and paste this repository configuration down here and the plug-in repository configuration and then with that done i can go ahead and add the graphql springboot starter so i'll say graphql spring boot starter it's an experimental dependency at the moment milestones and that's it now in graphql graphql is a technology that's created by facebook in 2012 it was open source in 2015 and it has uh this idea of a connected graph where you can do uh where it can actually stitch together different parts of the object graph with different resolvers each resolver in this case corresponds to a handler method in the spring world and so in order for it to work we need to expose it so i'm going to say graphql and you know i've got the graphql path as graphql that's the default that's fine there's also a websocket endpoint right if you want to do that websocket path it can be graphql i like to have that enabled as well and this is very important graphql uses http but it's not restful there's no there's no it doesn't purport to be the best citizen for http quite the contrary you can use graphql with websockets or http and i wouldn't be surprised if one day there's support for something like r socket so now i've got a graphql application and my application is going to create some endpoints that mat that model and serve up different parts of my data but of course that data has a schema and this is one of the things i love about graphql is that it has a it has a upfront schema that you have to worry about right that schema defines what is accessible and that schema is introspectible so other clients can look at that and go oh i know what you i know what you're capable of i know what endpoints you have i know what data you return this makes it very easy to discover and work with different apis all right so let's create a query here the query is the root object this is kind of a well-known object in the graphql world there are three different modalities three different ways to work with a graphql endpoint there's a query which is a thing that reads data from the server there are mutations which update things on the server and there are subscriptions which are long lived kind of like websocket or sockety connections or queries they're basically queries that you would serve over a long lift connection okay so the the query is the root for all the read operations right the type called query you can have other types as well one called mutation one called subscription uh but the only one that you really need to have to get started is query and here i'm going to have an endpoint that returns many different customers the brackets around the type indicate more than one of so we need to define the type customer now and the type customer has an id it has a name uh and that's it for now okay so now i want to create a graphql controller handler now i want to create now i want to create a graphql controller so i'll say class greeting graphql controller add controller and so the first way we're going to do this is to create a schema mapping and the schema mapping endpoint describes the type and this tape in this case the type is query and the field is customers so i'm going to say customers handler is going to return a reactive stream but we need to actually provide the type here so data class customer val id int val name string okay good so we're going to say just i'm gonna create a customer here id and then a okay customer to be right good so that's a very simple schema mapping endpoint that the schema the type is called query this is the query we're creating a handler or resolver or data fetcher you'll see those names used interchangeably in the graphql community so we're creating a controller handler method that acts as a resolver or data fetcher those two terms you'll often see used interchangeably in the graphql community across different clients for this field called customers on the type called query if we go to that we can see query customers okay let's try that out okay we can go to localhost graph iql and we get this convenient editor we can use to run requests against the endpoint and it shows me if i do control space it shows me what data is available i hit id i want the field and the name fields to come back so there we go there's my two records great now uh that was easy right very easy okay well what if i want to what if i wanted to get the customers by a certain name well in this case i would create a schema mapping type name query and we create a new field here called customers by name and this would be customers by name and it takes an argument string name okay and so fun customers by name argument name string equals let's say uh let's say that we extracted out all of this private valve db equals list of okay we don't need the generic type there anymore it's assumed and here we're going to say this.db.filter it dot name we don't actually we don't need the filter there we just return them all but here we're going to say this.db.filter it dot name equals name all right and that probably can be equals equals yeah there you go perfect so this is my customers by name and point and that's a field on the schema let's go ahead and try this request again good what about by name it's refresh and now it's saying hey you want to use the customers by name endpoint great it takes a field the field will be a and there we go if we do b we get b right so it's very very easy to use to send data to get filtered views of the data et cetera now the schema approach this approach to having a query that everybody uses is so common in fact that there are annotations that are sort of meta annotations just like the relationship between request mapping and git mapping so instead of doing this i can actually do query mapping and it'll infer that you the type is query and it'll infer that the name of the field is customers by name by the name of the controller and our method so in this case i can replace both of these thusly okay so it's exactly the same thing just cleaner okay there we go no problem now the last thing i might want is order data associated with my schema right with my customers so maybe i have a type called orders and it has an id and a customer id and i want a collection of orders given order all right so there is that so i'm going to return a collection of orders and in order to solve that i'm going to use schema mapping again because a query mapping is a special case for the type it named query but if your type is named customer in that case you need to use schema mapping so customers and you know here you don't need to specify the field i just did that last time okay so the i'm going to use i'm going to re this is a resolver that will stitch together the whole picture of the customer but in order to do that in order to get all the order data i need to i need to have a reference to the customer so i'll say then return a list of let's say orders and the data for orders will look like this it'll say data class order val id int val customer id int and i'll be returning order and the first parameter is of course the id and then customer.id two is this one and then three is that one etc right there's my list of orders uh and i don't need to specify the field again it's assumed go ahead and hit res request if i want the customer data and the order data i can refresh the page the schema has now been loaded by the client and you can see that that's there as well it has an id and a customer id field right by the way you don't need the commas i just do that because i'm used to it but it's not required by any stretch and i can get that as well right so there's the orders for each of the customers and i've only got one customer in this case of ideas if i look for all of the customers and i don't have a there's no parameter here there's no arguments so all the customers i get back all the customers for all the orders now that is two different processes or two different operations on the server side first i'm getting all the customers and then for each customer i'm getting all the orders and that results in two that results in two different handler methods being invoked but that's okay because at least i'm getting the response and it invokes them if you return a reactive type it'll invoke them concurrently because the reactive type is async so if you if you say i have i need to call this other microservice that has the order data and you use the reactive web client or the r socket requester you'll get back a reactive stream a flux of t and you can return that flux of t from this orders handler method and that'll that'll result in those calls being done in a concurrent fashion so you're getting all the customers and then concurrently calling the order service 10 different times all right this has been a quick look at graphql obviously i have another video on that one you can look at for more details and you can see subscription mapping and mutation mapping but as you can see it's very similar to what we've looked at before you can do exception mapping you've got annotations you've got all that kind of stuff so this is very very powerful all right swing fans what'd you think of that we learned about graphql and this as i said before we've already rehashed the larger sort of story around graphql before so i'll definitely defer you to that particular video for the bigger picture and bear in mind that even since we recorded this installment uh there has been a new release of spring graphql with new features like at batch mapping so if you have an n plus one you can say i want to for every thing every part of the graph that has to be resolved that would be an n plus one kind of collec uh cl fetch for every one of those give me all the ids and i'll return them all in one fell swoop so it's just at batch mapping and it's super convenient right this kind of stuff is great um obviously i hope you got something out of this if you did please like and subscribe we are always happy to uh to see that people are interested let me know what you thought in the comments and we'll be back next week to wrap up our series looking at all things at controller thanks so much and stay tuned and see you next week
Info
Channel: SpringDeveloper
Views: 901
Rating: undefined out of 5
Keywords: Web Development (Interest), spring, pivotal, Web Application (Industry) Web Application Framework (Software Genre), Java (Programming Language), Spring Framework, Software Developer (Project Role), Java (Software), Weblogic, IBM WebSphere Application Server (Software), IBM WebSphere (Software), WildFly (Software), JBoss (Venture Funded Company), cloud foundry, spring boot, spring cloud
Id: eVqmB2hsIVk
Channel Id: undefined
Length: 15min 43sec (943 seconds)
Published: Wed Dec 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.