Moving beyond REST: GraphQL and Java & Spring by Pratik Patel @ Spring I/O 2019

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] good afternoon everyone thanks for coming to this session on rest and graph QL and Java and spring and I think he's already done an introduction so I don't need to do anything as I say that I'm also a Java champion among other things the last one is my favorite the pearl recovery group how many people still code in Perl today anybody okay so a few people yeah we're gonna meet at 6:30 for beers in the Pro recovery group downstairs so anyways one thing quick I want to say is that if you're not familiar with what IBM does in the spring world we do lots and lots of different things spring works really well on IBM cloud and we have an amazing runtime called open j9 which is a drop in substitution for adopt open JDK and the hotspot VM so they especially open j9 it uses less memory and starts up faster and lots and lots of other stuff but come by the IBM booth get a free t-shirt like this and come hang out with us and obviously we're going to have beverages sponsored by IBM later today so before I get started I just want to take a quick survey how many people are currently using graph QL ok so a few people how many people are using it with Java on the back end graph QL server ok now what I want to do is do a quick introduction to graph QL and then look at an implementation of graph QL written with two different libraries that help you build out a graph QL solution on on Java on the server side so let's talk about graph QL a little bit the first thing about graph QL is that it is actually a query language right that's what graph QL stands for graph query language so it's very similar to how we do programming in Java world where when we're building inheritance model using Java classes we're essentially building out an object graph right we sometimes call it a tree but it is actually a graph when you're doing object-oriented programming so graph QL actually can mirror the way that we build applications with domain driven design and go into the domain driven development quite well because it Matt is up to the way that we already build applications in the java world using o design okay now you might be thinking well we have to learn another language fortunately graph kill is very simple and straightforward there's not much difference in terms of the way that we as think as object-oriented Java developers already but yes it is a new language and it is a new specification so it takes a little bit of time to understand some of the nuances around it and how to build things with it I hope to do that in the next 47 minutes here today okay so the other thing to note is that graph qaul is an API query language and the key word there being API okay so the emphasis on graph QL is around building api's and as we go through and do some demos you'll see hopefully how that works in terms of going and building out graph QL queries and how they relate to an API that you're building behind it okay so graph QL was created by Facebook and was open sourced in 2015 and it has been maintained as part of the essentially a community effort now and it is more than anything else it's a specification there's not a implementation or a reference implementation of graph QL okay so it's more specification than an implementation and when we do some demos and code here in a few minutes what we're going to see is implementations of the graphical specification on the server side because that's one thing important to note that it is actually a spec and not an implementation so how many people watch Silicon Valley in here is that okay so good I wasn't sure if it was just us in America who watch it but anyways this is a scene from Silicon Valley so you might be thinking we just rewrote all of our API Zoar endpoints or whatever using rest and now I'm up here telling you that you should look at something like graph QL right and maybe you're feeling like the guys in this picture we should just burn it all down and why do we bother alright so yes you may have rewritten everything in rest or maybe you're in the process of moving over to a more restful based API in architecture but the nice thing about graph QL is that it is not a pure replacement for rest the way that I like to think about it is that graph QL is just another tool in your tool belt for building applications so you don't have to throw away everything that you've written and rest you can happily coexist with rest and with graph QL on the same server or in the same architecture you can use both if you want which is really nice okay we'll get into that a little bit as well in a few minutes and one of the things you can actually do is that if you already have restful api is especially if you have a restful distributed api that's in multiple different endpoints of multiple different servers or services or however you've designed your architecture you can actually wrap your underlying restful api with graph QL on top and this is actually a very powerful extraction because you can have multiple teams that are building things maybe they already have built things in brest they have api's exposed and rest you can actually go and just wrap them and create like a meta api on top of existing api's so that your front-end applications or your mobile applications have a single unified entry point into all these different disparate api's and data sources and again we'll get into that in a few minutes as well all right so as mentioned already we can have any data sources underneath and in fact with graph QL because it's just a specification for the front-end or from the front to the back end it doesn't even have to be a data source it doesn't have to be a database in the back you can have something else running in the background and wrap that with a graph cue all API which is actually super powerful okay so we'll look at that as well in a minute now one thing that's different about graph QL compared to restful endpoints is that it is a single endpoint so when you set up a graph QL instance it will only go to one place all right slash graphic you all or whatever you want to call it and the other interesting thing about graphic you all compared to rest is that it is schema based right we'll have a look at a graph kill scheme in a minute when we go and explore what it actually looks like but these are the two key differences and we'll talk about more but the two key differences between graph QL and standard rest is that there's a single endpoint instead of multiple endpoints you know when you're building out a restful endpoint you may have a slash users endpoint and you may have slash shopping cart or slash items or whatever so you have different endpoints with graph QL you only have one endpoint and you send a query in to get the exact data that you need and there is a schema behind it so instead of being something that you just construct on the fly and you say well this is our users endpoint and we have the four verbs HTTP verbs to get and post and put et cetera graph QL can take either get or post but single endpoint and we pass it a query to see what it looks like and we're going to do that now okay it's time for a quick demo and you know how demos work so hopefully this works well all right so let's have a quick look at a few queries here and hopefully that's big enough let's double check yeah it should be fine all right so what I have here first we start with query and we say all starships get me the starships underneath and the name in the model and you can see that's printed here but let's go and actually amount to that and let's see what we can do let's say what class of starship is that and let's go run this and you can see that now we get the starship classes Corvette and this is a destroyer of course this is from Star Wars but let's see what other data points we can add here ok and you can see as I'm working through this this is what's called graphical or graph eql which is a browser for an underlying graph QL endpoint ok so we could say let's see how big the different types of starships are so you can see these are the length of course a Star Destroyer is much bigger than ACR 90 Corvette now let's actually see how we can go look at this a little bit more detail so what we can actually do is because graph QL produces a schema we can actually go look at the schema and if you have a developer who's not on your back-end team where the graph QL is actually produced your front-end developer whether mobile or web developer can actually come here into this graphical Explorer for your graph QL endpoint and have a look at actually what all is exposed through your graph QL API right so you can see that in this specific demonstration API we've so far been looking at starships so if I go down to starships here's the starship relation and I can click into here and we can go look at what are the different types of things under that all right so we have starships and all of the schema that is associated associated with that specific type of starship alright so we can see here's a description of all the different fields which are made available to us so we have name model class who's the manufacturer let's go see who makes the manufacturers right and again because we have a predefined schema on this we have this tool which allows us to go and browse and inspect and look at this specific graph QL endpoint that we have so we can come into here and we can say who are the different manufacturers of that let's get rid of this for now right now if we go run this you'll see that we can go and see that these are the different types of people who make that Star Wars starship right but let's go back here real quick and we should be able to also see what films that specific starship appeared in so again we're in we're just browsing the graph QL schema here using this tool called graphic QL or graphical and we can see all the different things which are contained in it but if we scroll down a little bit we can also see that there's this something called film connection so now if I go into film connection we can see that there's basically different types of films that are associated with each starship so I can come into here and I'm not gonna try to type this all out right now alright so I'm gonna go and run this specific thing it's the same as before let's query all the starships let's get the name and model called philip connect to many relationship in this case remember graph QL is a schema it's not it's different than sequel it's not the same thing as SQL or sequel it's more of a a graph based schema rather than a relational schema but in this case we have a relationship from starship in two films right here so let's go run this and we'll see that the 90 Corvette appeared the first movie it appeared in was a new hope and then the Star Destroyer also appeared in a new hope and let's scroll down to see if we can find a different one right so the imperial shuttle first appeared in the Empire Strikes Back now the way it was only the way I was able to get the first one is I used an attribute here called first and said give me the first film that that starship appeared in so I can put two here and go run that and now we'll see that for example the Star Destroyer appears in a new hope and also in the Empire Strikes Back now if I get rid of this all together you can probably guess what's going to happen right it'll show me all the films that that specific starship appeared in okay so what we're doing is we're just very simply using the graph QL query language exploring what we can do with the graph QL endpoint alright so let's do a couple of extra ones here so we did this first one with the film connection we can also go get the count so I can say query all starships count and I'm gonna go run that and you can see that doesn't work huh why doesn't that work it says in the message that can occur if you'll count on starship do you mean total connection um so let's go put that in and actually I didn't need to go and look at the error message I could have just come into here and when I hit control space it shows me all of the attributes or the fields that I have access to which is really nice so if you have a new developer who's come onto your project into your company and they're the front-end developer or even if they're a back-end developer you can just say disco browse the graph QL endpoint to learn all the different things we expose in some way this can also serve as documentation how many people love writing documentation in here anybody okay there must be this room must be full of developers because me as a developer I hate writing documentation but you have to do it right but this is great because from the schema we can get this documentation essentially for free okay anyways so let's see and go run this and this database has a total of 37 starships inside of it alright so the other thing we can do which is interesting is that we can go and get something from one collection the starships collection and in the same call also get something that's totally unrelated so the different species in the Star Wars world has nothing to do with the different starships right they're totally different so I can go and run this and at the top I'll get all my starships so let's just collapse all the starships and then right underneath it you'll see that I have a list of all the species in the Star Wars universe so I'm basically going and fetching two collections in the same database or in the same graph qo call right and I'll tell you about why this is important in a few minutes but essentially I'm able to go get two different unrelated collections which are inside of I schema in the same API call okay we already looked at how we can do collections traversal from starships into films right this is a one-to-many relationship of course or maybe a many-to-many relationship because probably different starships appeared in different films and vice versa alright so the other thing we can do is when we're constructing this alright so we're just the queries just a string that we're doing so say we're using a JavaScript front-end how many people build front-end and JavaScript as well just out of curiosity so for a number of people so when we're building this in JavaScript how do we do this there are some libraries that help you build a graph QL queries in JavaScript and similarly for iOS and Android but we don't want to do like string concatenation and string construction when we want to reuse a query over and over again so you might say inside of my application I want to go and get the starship multiple times right but I want to get different ones and we don't want to come in here and do string concatenation or for example let me go find the right one I have it here somewhere or maybe I don't right so I may not want to go and actually get and hard-code the actual name of the starship inside of my application of course I can't find it I've written in earlier there it is oh all right anyways so what we can do is we can use essentially query parameters like we have in this example so what we do is when we construct graph QL on the client we can say here my query variables I'm going to call it starship ID with this specific ID and then in my graph QL query I say dollar starship ID again that's this thing right here right and it's we give it a type of ID exclamation point so this is the other thing about graph QL which is interesting is that not only do we define the structure of our data or of our schema we also put in the specific type that each field and each data element is so in this case we're saying that this I the starship ID is of type ID and then we're able to go and use that to go and fetch a specific starship like so okay so we looked at how to traverse relationships how to use query parameters how to use pagination and modifiers like first so that we can just get a page of data if we want and other things like that all right oops all right so let's go back to here not now all right so let's make sure we covered everything we talked about how we can traverse a relationship like so we talked about how we can get a count and how we can do a query this is what I was looking for earlier right so we don't want to hard-code the ID like we have here we'd rather use a query variable so that we can reuse the same string and go and get a result out of it all right and then we did this as well how do we do use query variables now if you noticed when we were inside of our graph QL queries let's go back to this one right here okay let's find a simple one right so like this or if we look at this you can see that I had to type everything out let's say structure class manufacturers oops and and I keep hitting the wrong button for some reason okay sorry costing credits and maybe we'll do the length and etc etc right but in graph QL you do have to go and type every single field that you want you can't just say start there's no wild-card parameter in graph QL right so like in rest you can just say go get me all the user info and you get all the user info right how can you get every field in an object the short answer is that in graft you cannot you have to be explicit about which specific fields you want from a specific object now it's actually a deliberate thing that there's a wild card to get everything you have to specify every field that you want in a graph QL object that is in the schema that is actually by design and not on purpose so at this point you're probably thinking well if we're going and getting having to type every field every time we want to go get it is this a good thing or is it an anti-pattern or is it just bad or he's just ready to leave and just stay on rest well the reason why and they shut the lights off can you still see me in the dark is that okay all right so I don't want to stand there the whole time all right so this is actually a good thing because there are a couple of things that it enforces number one you only get what you asked for so you don't get everything say that you have a really big object that you're exposing to your restful endpoint you don't go and get everything all right and that's actually a good thing you can the other thing with graph QL as you saw previously is that I can go get things from two unrelated objects in the same call sometimes when you're building out the front end for a an application and you need two different things from two different places and you have a restful endpoint how do you go get those two different things you have to make two API calls right you have to reach back to the server and say I want all the user info and now I want all the item info but you have to make two different calls because they're in two different restful endpoints with graph QL you can just make one calling go get everything which is great right so the two specific things that graph QL has in terms of advantages over rest are the two things I just talked about the first thing is what we call over fetching I just want these three fields from the user object and not the other ten fields which are there in graph QL you have to be explicit and say go get me the user info go get me the user info and just go get me these two things all right so in rest that problem is known as over fetching also unrest there's something known as under fetching I need something from the starships collection and from the species collection and they're not related so in rest I'd have to go make a call for my starships and a call for my species so I'm making two call instead of one so graph QL solves that by allowing you to fetch different collections inside the same query ie it fixes the under fetching problem okay so these are the two key advantages of graph QL over a restful endpoint the other thing is that for those of you who raised your hands who are JavaScript front-end people you know that when you make a network call and you immediately make a network call your nesting two network calls and in grow in JavaScript that's asynchronous so you have to nest them and you start getting into what we call callback hell in asynchronous environments and graphic you all you can avoid that because you just make call and get two different things from two different data sets if you want right also just like we're able to go and fetch from two different unconnected objects we can also go update two unconnected objects in the same network call so it's a variation of the under fetching problem where we can make a graph call query that does a mutation that's what it's called in graph QL to go and change an object inside of the endpoint and say go and change this object and at the same time change this other object okay so grab QL is quote-unquote faster and graph QL is quote-unquote simpler now I have that in quotes because that's not necessarily always the case right there are times when graph QL is actually slower than restful endpoint and there are times that rust is actually simpler than a graph QL endpoint but in general as you're building out and doing API design you will find that graph QL on the whole is a little bit faster or sometimes much faster and also simpler than rest but not always alright so let's talk about wildcards a little bit more right another reason why it's actually nice to be explicit about what you specifically are getting from the endpoint is say that someone on the team over here removed a field from the object right you were you weren't expecting it maybe you're working on another team or maybe you're working on the front end right or maybe you're a server-side engineer and you want to play a joke on your front-end engineer or something right so if you remove a field what happens in rest it's just not there anymore all right or what happens when you add a field on to an object in rest it just automatically now shows up right so both of those are problems now if I go back to my graph QL editor here oh graphical okay let's say that this field costing credits was removed or let's just make up a field let's say that a field called home planet was removed from our back-end now when I go run this query you'll see that it gives me some weird air for some reason okay well anyways the point here is that it actually gives you an error let's actually try something let's get rid of this simplify this a little bit I'm missing a bracket or nine thank you okay let's try now there we go thank you all right so let's say that we used to have a field called home planet well I'm look what I'm looking at this in graph graphical you can see it actually says that but if I actually run it what do we get we actually get an error so if we have some kind of unit or integration test how many people don't write tests for their software okay there's a couple of hands over there I applaud your style I have a t-shirt that says I don't normally test but when i test i test on production so right it's it's a it's a big hit at conferences anyways the point here is that because we have a schema and that schema is rigid in some ways what rigid isn't the right word it's very well defined if you remove a field you will get an error on the front end your graph QL query will no longer work because it'll give you this error your server side will return something and say I don't know what feel you're talking about okay and the other part of this is that when we add a field we may not want to go get it in our existing queries all right so before I showed for example this film connection here right and this film connection may be like a really large object that's embedded inside of another object and say a server-side engineer adds that new field in that has this massive data behind it and now all of a sudden in your restful endpoint you just wanted the user info now you're pulling in the whole item catalog or whatever is a related field to it right that's also bad because that causes a big performance problem or other problems so actually being explicit in the fields that are there and the fields that are removed and feels that are added is actually a good thing because it enforces good design it enforces good API design okay no a lot of this a lot of stuff that we do in rest is kind of like magic right we have let's just go get the user info let's go get this and that whatever so we one of the things we avoid with graph with graph QL is we avoid that magic it's a little bit more concrete what we're working on so with the not having the wild card with the app breaks and obvious ways and we don't over fetch on new relationships that may be very large and that's very important right but you might be thinking oh it's such a pain for me to have to type out every field every time I want to go get that user info or item object or whatever maybe it has 20 or 30 or 50 fields inside of it and such a pain typing them over over and over again well what we can do is we can use something called fragments to help alleviate this pain in graph QL all right what does the fragment look like say we have something like users so we would create a fragment call user fragment that would contain some basic field data that we want to get maybe every time we use it so you can see I have a fragment called user fragment here and then when I want to go use that user fragment you can see on the bottom I say fetch users user with I it to and then I do dot dot dot user fragment so that uses that user fragment then you can reuse that fragment in the different places where you need to go get the same set of fields every time so you don't have to type them out over and over again okay all right and we'll get into this a bit later but how do we actually go and change data so far we've only gone into a two-week query data out of graph QL and this is what you do for mutation we'll look at that in just a minute okay actually we're gonna jump to our code here and talk about graph QL and Java so there are two main libraries for doing stuff there's one called graph QL Java which is a very low-level library for building graph cool stuff I will have a look at that in a second and there's another one called graph QL SPQR or pronounced speaker which is a graphical library which is a little bit more sophisticated for building out a graph QL server and I like diagrams so I put this one together that shows you how you do a diagram or how you do graph kill or where it says so you stub your database or it could be anything else you can still use JDBC and JPA and then you have graph queue on top of it which makes your endpoint which is what your client would actually then use okay and the data could be anything right so we're gonna use it are just a h2 in memory our DBMS for this demo but it can be anything you can expose something off the file system if you wanted or if you have some kind of network server if you have a Redis server that has like transitory information like just stored in memory you can expose that through graph cool you can expose anything you want it's not necessarily just a database behind it okay so when we're defining an API and we can wrap anything you want okay that's one of the great things about graph queue on again let's jump into our demo what about 21 minutes so I'm gonna speed things up a little bit and this is our first demo here okay so in this one I'm just using the standard graph QL Java API so we need to have a couple different things but let's start with our domain objects so you can see I have my ant ant identity so this is just standard JPA and here is my annotations for all those different things all right standard entity object book and author okay so we have two different annotations or entities here okay just make sure right and every book has an author so we say here's our author relationship from book and these are just standard repositories so you can probably guess what this is if I open this up this is just a crud repository and again just a card repository is everyone from familiar with spring data repositories like jpn credit repositories yeah okay most I mean is this a spring conference okay just making sure so you probably know what this is so I won't spend any time on that right what I want to spend time on is let's have a look at the actual application so what we have is this is an air handler we'll look at this air handler in just a second but you can see if we go up here this is a standard spring boot application right here okay and now I have a couple beans defined here book resolver the query resolver a mutation resolver and this is just a command line runner for doing some testing but let's go actually have a look at those and again those are just beans they get injected into our graph QL server so let's start with query and we take the repositories like so and we have find all books and find all authors and that goes in queries the repositories and we have count book and count authors okay so our query basically implements this graph QL graph QL query resolve our objects so this is when you're using this specific library this is the entry point for our graph QL server so we basically create this query object that implements graph QL runner or sorry graph QL resolver and we go and define these specific operations to go and look at this so let's go start this up from the command line make sure this works I think I already have it running okay great so let's go into here and let's just run count books really quickly right so you can see we have one book in here right now I'm just gonna comment this out and now I'm gonna run find all books like so let's go run that and you can see I currently have one book in our database and if we remember find all books and count books and go into our query class which implements graphical query resolver you can see I've find all books find all authors count books count authors so if I change that if I were to change this from find all books to find authors it would return that okay but let's go look at the resolve really quickly and what the resolver looks like so iBook resolver implements graph QL resolver and you can see underneath the hood it basically goes and gets the authors repository and the reason why we need this resolver is because our our book has a relationship to another object we wish to expose through graph QL called author so we have to set up this resolver so from within our query or find all books it can go and figure out where that underlying contained object is right and if we wanted to save a author we would create a mutation which implements graph QL mutation resolver and then it goes exposes something called a new book so if I again if I come into here and let me just comment this out for now write comment it out and if I were to come here and say what was it called again in your book right so I'm gonna be lazy and just say u that's not right what did that happen oh I think my command key got stuck okay I'm gonna skip that demo for now but anyways this is how we do it now I don't want to spend too much time on this because well this is good for building like a low-level API unfortunately there's a lot of work that needs to be done all right one of the reasons we use spring and things like micro profile with annotations and c-d-i and all those things is because we don't want to write all this code we want to spend as little time writing as code as as possible how many people like to write lots of code by the way anybody know why don't we like to write a lot of code because the more code you'd write the more code you have to test and the more code can potentially have bugs and break down right so that's why we like to use spring because we can write less code so the library I prefer to do it you do this is called graph QL speaker which uses annotations so that we have to write less of this kind of boilerplate and have less ceremony to do this kind of stuff so let's have a quick look at another sample project that uses this other library okay so let's start with our basic object and in this case because I really don't want to write code in this project I'm using what's called Project Lombok which you can see is imported here but it gives me annotations like this so I don't have to write all the getters and setters and all that stuff how many people use Project Lombok in here just how to carry out okay good that's I think everybody's hand went up so I won't explain that part all right but you can see I have my entity object here and then I say Oh at graph QL query name is ID description blah blah blah private long ID graph cool query name blah blah blah so instead of having to write those resolvers and other things like that I basically just put an annotation on a field and say this field is now queryable right this is great so let's work I have to do so now we look at our car service nothing fancy in here but I have get car by ID and this is where so in here I say go and allow this thing to be queryable and now if I go into my car service which has my cars like so alright I can say get car by ID name ID etc going fine from the repository object that car and also you can see it's annotated with a craft QL query for car so this is our route entry point into the car object but I've also defined a mutation called add graph QL mutation the name of that mutation is car safe car right so it can be different than the actual name of the method if I want and it takes in a type of this specific car and you can see there's some extra annotations in here and I call the underlying graph QL repository let's go run this make sure it works real quick ok let's quit out of here and do a spring boot run on this real quick see if this is working ok ok come on setting up the database and everything so it might take a second and you can see I'm just injecting in some initial values for us to play around with ok so let's go again to our graphical just do a reload and oops I think I ran that mutation I didn't mean to do that ok so let's go look at our cars and let's make sure this works oh wow thank god it worked ok but if you remember in from this output right here I just just for testing I inserted this into the memory database for our a Jaguar Porsche blah blah blah blah blah so it looks like I have nine things so I should have nine cars here and now if we go back to our code really quickly we were calling the car right so this is the name that I actually referenced right here four cars right I'm sorry I was getting a list of cars so graph QL query name equals cars is basically running this line of code in our server okay now the great thing about this is that we just go and annotate things and say this is the entry point for my cars object what gives me a list of cars and this is my car over here by car object forgetting a specific car and because we have strong typing in Java and all the stuff is there we basically are graph QL schema is auto-generated for us by just inserting these annotations into the appropriate places which is really nice that means it's less work for us to do and again we get this nice API browser called graphic graphical and we can go browse the docs here so we can say let's go look at the entry point for query and here's cars and it contains a type of car and that car has a giffy URL ID is cool and name so if i go back to my code you can see that we have some additional stuff well that's interesting where is this where is this gift fee URL coming from hmm okay so it's not in my car object so where is that it is where's that gift fee URL if you remember what I said earlier graph QL is just a specification for building out an API and we have to build out that API as developers so in this case what I'm doing is I'm using my car entity object as the base a let's say the base object and then I could just add in some random crap to it if I want okay so let's go search in this code base for Q to URL where is that thing oops hope so I can type giffy URL okay there's something called giffy service here so that's this string okay okay let's go here and now again I'm in my car service alright so if i zoom in here you can see I'm in car service and now if I scroll down a little bit you can see I have this thing called give free URL alright so what I'm actually doing is I'm saying that in the context of this car object go and add an extra field which is not part of that actual entity which we're basically just adding it like dynamically if you will as part of the query mechanism in graph QL so we're saying go get the give you RL right and what this is doing that underneath the hood it's is using this giffy service which goes and makes a call to giffy and gets us back the actual URL I don't need to show that part but the point here is that again it's we don't just wrap a sinto simple entity object or just a java class or something what we can do is we can actually just add something or anything in this case I go and make a call to the giffy service which is here and I go and attach that data to that actual object at runtime in this car service ok so it's just a demo of like it's it can be very flexible and you can do all sorts of crazy or useful things even to go and build out this API so let's go back to our API browser here so you can see that we have these gifty URLs and everything ok now let's say we want to add a car all right so let's go I'm just gonna comment this out for now all right so let's go and add something okay I'm gonna say mutation save car Tesla Model S extreme well the Tesla 3 just came out we'll add that in a minute ID name etc ok so ID will get Auto generated we have to provide a name which we do right here so here's car : name : now let's go look at this specific thing let's go find the save car in here ok let's do F save car oh here it is save car and you can see it takes a car type but it expects it to be wrapped inside of some kind of car container thingamajig and we passed that down into here all right so let's go run that really quickly okay so we got the data back and now if right and again a mutation is how we make a change or essentially do something like an update or whatever if you want think in terms of sequel right now you can see this is taking a second the reason why I took a minute is because I don't have all the data there that I need for this pacific call right what's the bit of data that I don't have contained in the entity object the gippy URL exactly so that's why I took a second because the server actually reached out thankfully the Internet is working on my laptop right so when it was going and inserting that and we were going in getting this data it actually going had to had to go and call this giffy service which goes and makes and you can see this is building an HTTP client it actually reached out to give he got that data back and attached it to that specific object okay so that's how we did that specific thing meant let's just double check did we get a giffy for this yeah I'm just interested to see what kind of gift he had pull back for a Tesla what okay whatever it's maybe that bit of code needs some work but anyways you get the idea right so similarly we have something else here another kind of static well doesn't go out to the internet called is cool so again if we flip back to our browser you can see that all these cars are cool or not cool or some of them are cool but we go and essentially just have like a static set or whatever okay but anyways the point here is that the way that we can build out an API is very flexible and because we have this nice library like graph QL speaker we can build out out very quickly without having to go and add a lot of boilerplate and have extra classes we can just use the annotations to build everything and if we're interested to see how all this comes together let's just have a quick look at the well let's start at the top here's our graph cool web configure all right so if we go look at this what we're doing is we're using the spring web NBC configure right here right we say this is we're gonna add this extra view for graphic graphical so the graphical like UI that I've been using just to demo this it's just a jar file if you drop that jar file into a graph QL server application we can very easily expose it like so so that we have this testing endpoint as well and of course we have to have an index dot HTML which is the front-end code or whatever but we can have this at this specific location all right so if i zoom in here you can see that I'm going to I'm going to slash graph QL and that's how we set that part up over here okay and again you just have to drop a jar file if we go into our palm down XML and we scroll up it'll be here somewhere I hope hope it's here somewhere all right so actually it's embedded inside of this graph QL speaker library but it's very easy to set that up and let's finally just have a quick look at the graphical controller so what we do is we say initialize a new graphical schema generator and again that class is from our graph QL speaker library right right here and we'll say use these resolvers build a new annotation resolver it'll go and scan the class path find those annotations use operations from this service right so you saw the car service before create a value map or factory this is just for the Jackson so that creates our schema these lines of code and go and create our graph QL schema dynamically ok so we don't even have to go and like do any hard coding or whatever now I didn't show this earlier but I do want to mention it quickly you don't have to build everything out in code like this what you can actually do is this is from the previous example using the low-level graph QL Java library all right we can actually go and write out the actual schema using the graph QL schema format which is this stuff right here right or if you're lazy like me you just say you know what I'm just going to I'm just gonna go and annotate all my stuff like so and you go and build a schema for me because I don't want to do that ok so anyways we build out the schema we create a new graph QL from that schema here's our post mapping for the slash craft QL endpoint and the rest of it is relatively straightforward we say from the graph QL schema go and execute that new query based on the input that was provided okay and that's how we build out a graph QL server in Java using this specific library alright so let's just make sure we've covered everything here I think we have I'm gonna just skip over these slides I only have a couple minutes but with this using either graph QL the graph QL Java library or the graphical speaker you can build out a graph QL server like we did here using spring boot or if you use micro profile you can use micro profile or whatever right and actually you may have noticed earlier that inside one of these applications which one was it right if you want to also build out something that just runs from the command line you can see it you has anybody written a spring command line app before anybody a few people yeah so this is just a standard spring command line runner objects so you can actually write a graph QL like program I'm not a server in this case by just going again and using this library okay so let's go back to here get back to our slides okay so we've covered pretty much everything you need to know but there are a couple of things that we haven't covered for example authentication and authorization how do you do that against graph QL there's unfortunately we don't have time to get into all that but you can find some examples out there which walk you through setting up authentication and authorization it's not very difficult it's what you would expect we also didn't get too much detail into features like pagination right I showed how you can just get the first one or the first two or last one last two in an earlier demonstration but the thing to note is for example when you want to do pagination when we're building out a graph QL server we have to write everything you saw me add annotations to the car service and to the car object so when we're doing that we're basically building out a graph QL server so if you want to do pagination you have to have the ability inside of your entity object or your service object to go and get paginate of data right so it doesn't come for free that server-side stuff doesn't come for free you have to still have the ability to do pagination and then graph QL allows you to expose that pagination into the graph QL library okay so we didn't cover pagination in our code examples here we talked a little bit about mutations and changes in updates but we didn't get into too much detail on that either unfortunately so I'm gonna stop there because I have about 20 seconds left where how much time do I have left am I done no more time I'll keep going okay [Music]
Info
Channel: Spring I/O
Views: 19,627
Rating: undefined out of 5
Keywords: springio19, REST, GraphQL
Id: iHxu2bq3fxI
Channel Id: undefined
Length: 47min 28sec (2848 seconds)
Published: Fri Jun 28 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.