Spring Tips: Spring GraphQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi spring fans welcome to another installment of spring tips this installment we're going to look at spring graphql which is a brand new project that's not yet ga that's based on the uh it's a spiritual successor to the graphql java binding the official graphical java binding and the support they're in for spring we've taken the project in-house and put some of the best people in the spring team on it and they have taken uh the the code base and turned it into something that integrates more naturally with the way that people expect to build spring applications spring graphql still has you know you can still use the low low level data fetcher api which corresponds more or less you know logically to the the server that api does for spring nbc but then there's a higher level component model based on controllers based on the idea of at controller in spring now you may know that annotation you've certainly probably used spring mvc or spring web flux you might have used the websocket support introduced in spring framework four you might have used the r socket support but suffice it to say at controller is not a http specific annotation it was uh conceived to be a general purpose description of anything that controls requests into a protocol and produces the response out via that protocol it's a very very useful way to think about building services and it's just this is yet another example of where we've taken this component model that people are familiar with and used it to create a a way of describing your services your controllers uh in a way that's idiomatic and hopefully you know pojo centric right a plain old java object this api this integration should feel very natural to you it's got a lot of the same concepts including uh you know including a uh encoding and marshalling and controller handler methods and you can even do exception handlers and things like that so if you've ever used any of these other component models this should be very familiar common ground i love graphql it was originally invented in 2012 uh at facebook facebook was confronted with a bit of a challenge they had a bit of a tension you see they wanted to build different isolated microservices but their clients needed the data that they needed you know as quickly as they can get it in the same view right they didn't want the clients be they android devices or ios devices or web page uh you know views to have to call different microservices and compose the data on the client because who knows what the scenario on the ground looks like for those different clients you might be uh using your iphone going through a tunnel and you know that that latency would be inexcusable and it's also grossly inefficient right why do that when you can have it all done on the server side in inside the server inside the data center where these services have presumably much faster access to each other so the composition of these different um views happens in the data center and the view is sent back to the client the specific view that the client needs is sent back to the client netflix had the same problem they and they started building api gateways through their zool uh api gateway library and and this is a useful approach in the the way this stayed viable despite the proliferation of clients right i mean who can't imagine a scenario where you have a netflix client available to you and a device or an ecosystem they're everywhere the way the reason to stay viable is because api gateways written with zuul are declarative they're small they're meant to be a very lightweight small layer of code of integration code between the clients and the downstream microservices they're almost cheap in fact they got to the point where you could do reloadable rules using groovy so the whole point was that they're declarative and they could be deployed easily and they're more like infrastructure not business logic spring cloud gateway serves the same goal it's an api gateway and it's a lightweight and just declarative and it's even reloadable if you want it to be but still you have to create a new one for each client right to create this new view with the particular data that the client needs because you want this data to be as quickly as quickly resolved as possible so there's that tension you want things logically to be well isolated and teased out into separate services but the client wants all the data co-located in one payload that doesn't require a lot of chatter and lots of back and forth with the server and so facebook when confronted with this problem ended up creating this thing called graphql which allows you to get the data that you want and no more but you specify that in the query so the query decides how to pull apart or tease apart the payload that will come back uh this is great right because it means you don't pay for the things you're not using right if you're if there's a separate resource that you don't need in this particular view you don't pay for it even if it's expensive it requires another microservice you're not going to pay that cost this is actually a really nice fit right i'm glad they open sourced it in 2015 right in 2015 uh this this graphql technology came out and and people started using it it's very popular a lot of other ecosystems and it's starting to get more traction here in the java ecosystem which makes me happy so good stuff there huh and by the way netflix eventually became one of the poster child for uh graphql they use it all the time now they even have infrastructure uh that you can use that is a separate approach to building uh services and that's more uh they can do code generation right so in that in that context you take your java objects and it generates the schema for you what we're going to look at is more the opposite where it's schema centric first we're going to take the schema and then write the handlers that resolve that schema and i i think you're going to find that with when it comes to the spring team more often than not we choose schema centric first right and we like to provide ways to build schema we think that's a useful way to build services so for example if you're looking at our support for spring ws we started with the schema the xsd and then you can build the controller handlers that respond to those when it comes to graphql it's the same thing we want to support schema first so keep that in mind keep in mind that there are other approaches to doing this and um i think you'll find that in time there's going to be some nice integra integration opportunities for both if you want that uh but for now let's dive right into it alright so let's go ahead and build a graphql application now obviously graphql is a a way to build apis that has three very simple concepts queries mutations and subscriptions a query is a way to read data from the server a mutation is a way to update data on the server and a subscription is a way to read data over a period of time basically think of it as a long-lived kind of request this is ideal for ongoing data that you might want to read sensor data stock stock market updates that kind of thing um graphql is typed and and so as a result it's uh you know it's got a schema and as a result it's very easy to sort of introspect um this is an example of the spacex api uh you can use it to ask ask the question what endpoints or what fields are available these fields are basically apis i can actually ask for information i can say hey i want to get all the capsules i want to say query capsules show me all the capsules and then i can see that there's different types of data here that's available so i can specify which ones i want right and this is only the data that i want i don't get anything more or anything less and on the server side i'm not paying the cost of loading this other data if i don't use it right so uh the nice thing about graphql is of course i get what i want i get i get only what i want this is great for different clients um so this is a you know an example of where graphql really comes into its own i have this nice interactive query console i have the ability to ask the kinds of questions i want to ask i can introspect all the different endpoints that are there and by the way as you'll see in just a moment there's no reason that this means that all the apis live on this server graphql is great for data integration gateways right it's a great place to pull together and provide one consistent global perspective on all the different endpoints and services in your architecture you can do that from this this graphql api and that in turn can delegate to other api so this is a particularly nice trick to do when you're using reactive apis where you can do concurrent uh sort of surface orchestration composition right scatter gather kinds of calls but it's not complex it's not it's not like you have to worry about writing all that kind of stuff it's just done for you automatically so we're going to create a new application called api and we're going to use the latest and greatest version of java we're going to use the reactive web support now the nice thing about graphql spring graphql in particular works with both imperative i o you know servlets jdbc that kind of thing and in spring mvc or reactive non-blocking io with spring webflux and r2dbc and the like whenever possible i mean almost always these days i use the reactive apis just because it's faster less memory more performant more features more robust more secure more cost effective you know just better in every way unless you have some particular use case that doesn't have an analog in the reactive world in which case it still might be better but so we're going to go ahead and hit generate we'll go to the downloads directory open this up in the ide [Music] okay so uh we don't have graphql on the spring initializer just yet the spring graph kill support is not on the spring initializer just yet so what i like to do is to actually generate my project using a snapshot version or milestone version of spring boot so i'll hit explore i'm going to copy and paste this and i'm going to just paste it all there that gives me the milestone versions and then i can just go back up here 2.5.4 and now i've got access to the milestones and i'm going to need that because spring graphql is a non-ga project while i'm here i'm going to bump up my build to java 17. since it's since that's out and it's now ga i'll go ahead and bring in the experimental spring boot starter for spring graph kill 1.00 m2 and i'll re-import having made all my selections for the most part it's just a regular springboard application that's going to persist data to the database so let's start with a a a record type okay we're going to use record customer and this is going to be a java you know this is a java case class or or a data class if you will and i'm going to persist this record to the database so i'll use the spring data annotation here okay oops i need to r2dbc as well so let's see we'll use 2.54 we'll use r2dbc we'll use h2 we'll hit explore and i'm going to go back and make sure i've got r2dbc and these other dependencies in my build in addition to the spring graphql dependency that i've just added so replace all of this i don't need the jdbc version of h2 so i'm just left with this reload and there we are okay so there we go there's my basic entity i'm going to persist just persist this to the database by using a reactive code repository so customer integer and and now i want to actually create an endpoint to serve up that data so this is where it starts to become interesting right remember i have an object that has two fields id and name it's a record so if you've ever seen records before then you know that they don't have getters and setters right so one thing that's kind of useful is that you should know if you want to change the representation you can always use json property right this is convenient uh in our case it'll work out okay but you know if you ever didn't want to change it just know that's there um and you know you can create a record like so new customer and i have to specify you know stefan okay and i can say customer.id for the id and i can specify var name equals customer.name right so very very convenient uh you know syntax there i can just pull the data the constituent data out it's read only so i can't write the data all right so there's my basic object graph i'm going to need to have data in the data in the data store so i'm going to use springbootism to create a schema file and a data file so schema.sql and data.sql okay uh crate table customer id serial primary key name var car 255 not null okay there's my sql table and i'm going to insert some data into that data store so insert into customer uh name value uh josh let's see let's say stefan um uh dr sire madura yushen phil jurgen um who else that's eight names i need an eighth name i mean there's so many great people mark okay there we go so there's eight random names um this is going to be enough to initialize our data store uh now i would just want to see it work right and so i want to start exposing my endpoints in terms of graphql okay so the first thing i'm going to do is to create a schema folder right so graphql the springbootstarter expects a graphql folder and then a file called schema.graphqls and then there all graphql projects have a type called query that's the it's a well-known type it's just a type there are other types you can create as many as you like but the convention is that you always have one called query from which all the fields hang off right all the fields that represent your endpoints basically they correspond to the the root endpoints in your api they hang off the query and you can navigate from there to other ones right so let's say that i want to have customers right all right well i'm going to create a query for customers and that means i need a type for customer this syntax here the bracket bracket that means more than one of okay and i'm going to give it an id here i'm going to give it a name and this maps more or less to the uh to the shape of the object in my java code but it doesn't have to be that way as we'll see in a bit so okay there we go so let's go ahead and create our graphql controller to map to that right and you know remember at controller lives in the spring stereotype package it's not a web tier component right it doesn't specifically have to do with http that's why we have r socket controllers and we have websocket controllers and we have spring mvc controllers and spring rest api controllers and now graphql controllers right this is all about a generic mechanism to control the orchestration of data right as exposed to the outside world so we're going to create a a customer graphql controller and this customer graphql controller is going to expose the data for our customers so the type is called query and the field is called customers okay that's very important so let's start low level let's use schema name schema mapping so the type name is customer and the field is sorry the type name is query and the field name is customers alright so we're gonna return a reactive stream there and we're going to use our custom repository so we'll inject that into the constructor and we'll say this dot repository find all all right good stuff so let's let's see what that does good so it's up and running we can go to the browser localhost 8080 graph iql and we get this nice you know console very similar to the one that spacex has right the graphql explorer here's the spacex one graph iql graph iql etc so you can now run queries and there's even it's kind of cool there's a little tutorial there right in the bottom you can shift control p to prettify the queries uh merge queries run queries autocomplete pretty cool right okay so let's issue a query now if you don't want remember i told you that you have to specify a query that has the same shape you know as the api or rather the query that you specify dictates the shape of the api but something you have to start in a common place right so every query starts with uh the root right and what what is the root for all your endpoints query right so in theory you should be typing this but it's kind of a convention that if you just see this then that's the same as saying query right so here i'm asking for the customers but notice that if i just leave it like that without the without these specific data it says must have a selection of subfields do you mean customers dot dot right and it wants me to put in fields here so what field well i can hit control space and i get back the customer data for i get back the id for each customer all right not bad what about the name data great i'll get that right what about the type name right type name is customer if i wanted to like in javascript or something that i wanted to inspect and kind of reverse engineer that okay now again this is that theory this is that principle where i'm not paying for stuff i'm not using i don't just because i'm loading data doesn't mean i'm paying for the extra data okay okay so i've got a graphql endpoint i used the schema mapping annotation to make this work now this is pretty fragile right imagine if i should change the type of of the in the schema and imagine if i wanted to resolve some other type as well this would be stringy typed and i don't like strings in my code so a couple things first of all creating endpoints that hang off of query even though it's just another type like any other type in the in the uh schema it is a special kind of special type right it's a it's a query endpoint and so it's very common to have a query uh type and so rather than um you know rather than specifying that each time there's a annotation called query mapping where the default um type name is query right so it's a it's a specialization kind of like git mapping and post mapping and put mapping that relationship to request mapping right and then the name of the field is customers but even this you know the name of the fields can be customers we can derive that from the name of the method okay so query mapping and the field is called customers okay let's try that okay so let's refresh there you go same data right i can et cetera so it's just cleaner that way right okay good so now i've got um you know query mapping and again most of the time you'll probably use query mapping but remember what i said remember that you can you can actually get uh you can actually resolve individual fields that schema mapping is there for a reason you can use it to resolve individual fields for a for a record for an object right so let's suppose that we wanted to get um orders related to the the customer okay so in this case i want to create a new type here i'm going to say type order and it's going to have an id and a customer id okay and we're going to assume that customers have orders okay now this might be a separate microservice this might be a separate mic service to which i'm making my request but conceptually from when i when somebody introspects the api i want them to see that there are orders that belong to a customer therefore it's like a one-to-many relationship that's not how it's implemented in the database right there's no connection there i might have an order service using rsocket and a customer service using http rest but from the perspective of the consumer of the graphql endpoint they are one and the same one is a part of the same aggregate right so how can i implement that well pretty easy right it goes here schema mapping type name is customer and we want to create order right so let's go here record order integer id integer customer id okay so there's our order and we go here we can say we want orders all right and what is the we're injecting a customer here so i'm going to inject that this is the customer against which we're resolving this field now when i say resolve you know depending on which graphql implementation you use you'll hear different jargon or different terminology for the same thing you've got data fetchers uh in the underlying api that you know under that pin underpins uh uh the spring graphql integration that's called a data fetcher other apis often call it resolvers and it's it's just it can be a little confusing but they're all the same thing and to make things even more confusing you as a spring user will probably have heard the term controller handler method or just handler method or or something like that so these are resolvers these are controller handler methods these are data features they're all basically the same thing they're methods that get invoked when the engine the graphql engine needs to resolve data uh associated with a type okay so here we're resolving the orders for a given customer um so it's gonna basically it's gonna load the query mapping endpoint here and then for each customer it's to call this one and this is going to return a reactive stream of orders now you know i don't want to i don't have another database to call another service so let's just synthesize some random data just to make this easier now keep in mind this could be a web service call i could be using r socket and the nice thing about the the engine here is that if we have 10 customers and that results in 10 calls to the order service which is n plus 1 then you're only waiting as long as the slowest individual orders api call right so um because we're because the the calls to the different orders endpoints are concurrent and that's because the return value here is is asynchronous so if i'm using a a reactive stream then i don't have to block right uh i would get i would get 10 results um resolved you know at the same time as opposed to one at a time you know in a serialized fashion okay so now i've got the let's let's create a stream of orders okay so i'm going to say um new arraylist order for var customer id equals 1 customer id is less than or equal to 8 customer id plus plus plus and i'm going to say orders dot add new order and uh the id of course is um uh this is well no sorry we want this to be a custom order i don't don't we so we're gonna say order id equals one order id is less than some random number right math.random times a hundred let's say okay and then order id plus plus so i'm going to say order id and customer dot id okay there we go there's my orders list i'm going to turn that into a stream obviously this is a stand-in for a network call that you would make you know you don't normally just make fake data like we're doing here but i want you to see what's happening here so i can create i can use the reactive web client the non-blocking http web client to call some other http rest api and get that data back and that would be i could just send the reactive stream that gets created from that directly back from this handler method and that would be just fine okay so now here's my orders here's the reactive stream and so whenever somebody asks for that data we'll get it back let's see if that works okay so good i refreshed it hit control space no it didn't refresh it hard enough let's try that again orders good good so there we go we asked for orders and we got random number randoms random orders uh for each uh for each customer okay so customer id the customer id is redundant right obviously the id is customer id1 and then all the customer id fields for each order would be specified there so that's a great example of where i don't want to uh to overly specify that okay good so now i've got uh i've got i'm reading data i'm able to get this and you could do you know imagine like uh you've got the customer's account but you might have their customer profile you might have you know their posts i mean you can there's a whole related set of data and you might want to crawl that graph and that might resolve into several different calls to different microsoft microservices but you're not paying any any complexity here you're not i i don't have to worry about doing that concurrently and reactive programming gets me a lot of the way a lot of the way there anyway but the fact that i can just plug this into the engine and it just does it for me that's wonderful okay all right so now um i'm i'm reading data we've seen how we can read data here a couple different ways what about updating data what if i wanted to add a new customer well let's go back to our schema and we're going to create a mutation so this is a well-known type called mutation right and i'm going to add a customer and this is a you know i'm going to add a customer with a name and that's going to produce a single customer okay same idea but this is the first time you've seen an argument i'm actually creating an argument here for this field and you can do this for read things as well i could actually have a query you know i could say customers by name and i could say name string and this could return customer right so you know maybe or maybe that's a good idea let's try that out quickly before we move on to the mutation so here is my customer repository i'll create a finder called customer find by name string name right and then i want to expose that as a graphql query so query publisher customer find by name argument string name okay and i'm going to say this that repository dot find by name name okay let's try this out so i'm using argument this is kind of like path variable or destination variable it's just a way of us binding the parameter in terms of the concept of graphql right it's called an argument in the graphql world so let's try that okay so now instead of getting all the customers maybe i just want maybe i want the name and the name will be let's find somebody who's in the in the data okay good and i want a i want the customers back but i only want the id and the names so oops customers by name what did i do wrong customers by name oh called customers by name i i named the method like the finder this is where it helps maybe it would be useful to have a constant there instead but uh no big deal we fixed it all right there we go so i said give me the i'm going to find the record uh where the name is equal to madura and i got back that particular record okay very very powerful now this is basically the incantation that you'd use when you update things as well it's just a convention a very useful one that you put that kind of a finder inside of a mutation so let's go ahead and build that now and i could again i could just do a schema mapping and the type name could be mutation and the field name could be add customer you know i could i could totally do this but you can see where this is going i'm going to use mutation mapping instead and i'm going to return a customer add customer string name okay so we say return this.repository.save new customer null name all right very good so there's my uh updated code same as before we need to put that make that an argument let's do a mutation okay so it's up and running let's go ahead and try this out so i've got my customers by name but first i want to invoke the add let's see here i'm going to use mutation add customer name bob i don't know who bob is but sure okay and the results that come back are the id the name okay there we go so i've added customer i got the id of nine remember we only had eight records in the beginning now i can go back and paste this and you know let's search for bob not bad huh i got the i got back the result i expected almost instantly that's very cool so this is one of the things i love about graphql is that there's no it's really not up for debate right there uh i love http i think it's a super cool protocol it gave us the wild and wonderful web but you know building rest apis is it it's it quickly descends into dogma and nonsense that has very little to do on delivering results and very much to do with the purity of the approach and as somebody who feels like he's managed to sort of get a hold on something that passes you know a good style and good discipline for building rest apis i don't particularly mind it but i can imagine it's overwhelming for somebody it certainly was for me it can be overwhelming if you don't know all the uh the rules the quirks the oddities the the uh the approaches that people need to care about if you're just trying to build an api and somebody's coming at you with arguments about put versus post versus you know options do return 201 versus 202 do you use this content type should you do should you use hypermedia is you know it it can get very tedious and you know you have to assume that everybody's on the same page and if you want to solve other problems like introspection you have to have a schema and of course schemas you know how do you do a schema right like do you add that yourself why why is swagger so popular right open api these kinds of things uh they're all just there to give you the some of the benefits that graphql just has out of the box right and there's no it's not hard to figure out if you want to send data to the server you use a mutation there's no discussion about post versus put it's just use a mutation and um you might be saying hey look you're using graphql and it's going over http but this is kind of an abuse of http right because you're not really using http it's just a transport to which i say you're absolutely right and that's a good thing right we're using http but we don't need to in fact you can use websockets which i quite like so we're going to go here and we're going to let's say websocket let's say 60 seconds for this and the graph the websocket graphql endpoint i'll call graphql as well and you know i could i could imagine you know some other protocol in fact right the uh spring graphql lead and spring web ninja uh rosenstein he's awesome he was uh on an episode of of the podcast and he made a great point he said yeah well there's there's no reason conceptually that uh spring graphql couldn't be powered by our socket you know the the transport could be our socket which would be great because these are two very very powerful abstractions graphql is how i like to think about the world now it's very simple and it's easy to build an api that people can immediately start using and r socket is the fastest most natural way to build services as far as i know right uh having tried http and grpc and a few others i just love our socket so you know why couldn't we use our socket as a transport and there's no reason and websockets are nice because you know you can do the reads you can do the queries and the mutations through the graphql r socket sorry through the graphql websocket endpoint no problem but you can also do subscriptions right through that and that's where it gets really interesting is having these endpoints that give you information um over time right so let's go back to our our our project here and we're going to build a um a subscription endpoint okay so let's suppose that we had endpoint that gave us information about events related to the customers right and so these these uh customers are you know things are happening all the time they're being added they're being changed deleted whatever so i want to have a way to consume that event okay so first of all i'm going to create a subscription endpoint a subscription type so type subscription all right and uh that's a well-known one as well so i'm going to create an end point here a field called customer events id id and i'm going to return a customer event so this is a a stream of customer events and normally i would think about it as bracket customer event right if it's if it doesn't have a bracket it's a single value if it's got a bracket it's a plural value but in this case it actually makes sense to keep it like this as as just one value because you're going to get the same endpoint over and over as opposed to calling the same input and getting multiple values over and over or multiple values right you're what i'm actually getting is i'm subscribing to an endpoint and i'm going to get one value each time there's a new value to be got so that makes sense so now i'm going to create a customer event and this customer event is called customer and it's going to have an event type called customer event type and this customer event type in turn is uh an enum actually i'll just create an enum right so updated deleted all right very simple very straightforward okay and i want to create uh i want to map that to my code my java code so let's go back and we're just again i'm just going to fake some data uh to make this easier so we're going to use subscription mapping and our endpoint is going to just produce a stream of customer events so customer event customer events and we need to of course create this customer event and a record it'll be a customer event and customer customer customer event type type okay we need a customer event type so the customer event type will also be in enum here on the java side so customer event type all right and what kind of fields do we have here what fields where we have updated and deleted almost directly line for line uh what we typed in the spring in the graphql right actually customer event type you know i think you could probably even leave a comma there that's probably valid still um and if it is then that means you can just literally copy and paste it that's very very familiar it's hard to get that wrong right uh let's see good so there's my event there's my customer event which has the state of uh the uh the event and and the customer to whom that event applies uh and then our our endpoint here is going to give us all the events for a particular customer so let's resolve this we're going to say given an argument customer id whoops and we want this to be an integer customer id and we need to update this that says customer id right to be very clear all right we're going to take the data i'm going to say repository dot find by id customer id and we're going to then flat map it into a reactive stream of of records so customer and we'll come back i'm going to i'm going to delay the elements that come back right i'm going to create a sequence of results so we have something to look at over time and i'll just get 10 elements let's say take 10 okay now this my job here is to return a reactive stream of value so i'll say stream equals uh stream i'm going to use the java 8 api stream.generate new customer event customer math dot random times well let's see what do i want to do when i say the new customer event right so math that random is greater than 0.5 i suppose so 50 50 chance of it being uh deleted or updated right and obviously this is not actually there's no i'm not deleting stuff right nothing is actually happening but it's just an example it's showing you a feed of data right as a feed of information so that's my java 8 stream i'm going to turn that into a reactive stream i'll say publisher dot from stream stream okay good stuff so there we go there's my uh customer events endpoint um and i want to consume this this is a customer a customer event endpoint i'm going to consume this from websockets the graph iql endpoint uh the browser doesn't do a particularly great job of letting me interact with subscription endpoints uh which is a bummer because i would love to be able to see that interactively so we need to do you know we need to eat the the whole elephant as it were and we need to uh like create a htm html page using a javascript client that actually um that actually talks right to the graphql endpoint and that's fine you know i'll just do the old-fashioned way so i happen to have pre-written that i'm not going to make you sit through me ruining javascript so here we go file index.html and voila okay so let's review what this thing is doing okay so i'm using this uh graphql web service uh websocket rather i'm using this graphql websocket javascript pro client uh you can just link to it and i've got a class can you believe it it has a class and a constructor we did it we did it everybody we did it we did it it finally happened javascript has has some of the things that make java nice good job everybody well done i'm using the graphql websocket uh client to create a client pointing it to a url we'll look at what that is in in a second uh and i've got two methods here one is just for subscriptions and basically it's a query and then a callback like an actual callback uh and then the other one is for for just uh you know queries and mutations where i i just want the result there's only one asynchronous result right for which i promise and async await are ideal right um so here it's it's basically the same api behind the scenes they're both calling this.client.subscribe right so that's what's happening here this.client.subscribe this client does subscribe but because this is just for single values it's not an ongoing stream i'm wrapping it in a promise which is great because i can use it with async await in javascript right and then i'm using that javascript client that i just created because a new graphql it's called i'm creating a client in my little crm class and i'm connecting as a websocket client to the websocket graphql endpoint and i'm going to read the customers here using this query customer's id name okay i'm going to get the data and the customers now i want to uh add data i want to add a customer so here's my query using the mutation so you've seen this this is exactly as what as we put in the um in the uh graph iqr client and then for the subscription i'm querying for the customer and the event right uh and so if we look at that that should be what we need um good right so there we go that's that's a little bit of everything and i'm going to just use all of this so when the javascript page loads i'll create a crm instance i'll subscribe to customer updates uh customer events you know uh and print out the id the name and the event and the time right i'm gonna then add a customer bob and then i'll read all the customers and confirm that this value has been added right it didn't exist before but it does now um okay so let's go ahead and restart and that should work fingers crossed okay we'll go to here i'm going to go to um index.html oops let's see what have i done closed event oh the um the query was incorrect what did i do i said customer event oh it's called event and what did i put in my javascript i wonder i called it event customer an event what's wrong with that customer event customer events oh it's called id uh okay i need to change my javascript there see this is why it's nice to have types and schema okay take three so there you go i'm getting uh you know just i'm getting random updates obviously i just synthesized it uh stefan the event type is wrong right so subscription is complete here's all the customers all nine of them including bob but the subscription i'm getting back the event is customer id name and event.event so is it what is the event type customer event customer event type it says event dot customer.name and then event and that's the reactive stream so it's a customer event and it's a oh type there we go there you go deleted updated updated updated you know etc i'm just doing math.random deleted whatever so you can see that's that and it's all just websockets it doesn't really matter you know i just send in the query and that's it the the query speaks for everything there's no questions about how to form the right envelope and the headers and and all that now obviously we've just begun to scratch the surface here uh you can see that graphql works just fine on top of the reactive stuff it also works just fine on top of imperative io there's also a really good spring security integration already provided at the box we're doing more and more work to make it work nicely in a spring data context as well the the use case of course is that we want your spring data repositories to be able to dynamically project just parts of the graph so that when you send a query asking for just the id and the name we don't load the full entity behind the scenes right and so this is very very convenient obviously in this case you know we only had two fields in our entity and we synthetically added that third one called orders uh by calling another microservice so in this case it wasn't big when it wasn't that big a deal but imagine you had an entity in the database that had ten fields and the client only wanted two well now you're faring around those extra eight for no reason so the work is being done underway there there's already some interesting stuff you can do uh with uh with spring data and graphql and uh and good stuff with spring security as well so watch the space this stuff is getting very good remember this is built on top of the graphql java client that's already been out there for years so while spring graph ql is still experimental i expect it to be you know it's trending towards ga sooner rather than later and for good reason because we're not building a brand new code base we're integrating an existing tried and true and well tested uh implementation instead as always thanks so much for watching and we'll talk to you next time
Info
Channel: SpringDeveloper
Views: 15,065
Rating: 4.9671235 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: kVSYVhmvNCI
Channel Id: undefined
Length: 47min 24sec (2844 seconds)
Published: Wed Oct 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.