 Hello, Developers! My name is Matt Raebel, and today I'd like to show you how to GraphQL in Java. Let's get started. This screencast is based on a blog post that we published on the OctaDeveloper blog back in January. Everything's still up to date. Everything works like expected. So if you want to get a more in-depth overview of GraphQL and how to do everything, please read the blog post. So at the bottom of this blog post, it links to a GitHub repo. And in this repo, I have a demo.adoc that I created that has all the demo steps for live coding. And so if you want more detailed information, please read the blog post. I'm just going to write a bunch of code and get stuff done here. So you'll see we need Java 11, HTTPI installed, and an OctaDeveloper account. But we'll go ahead and create that as part of this. So we'll create a GraphQL API with Spring Boot and Java. We'll run it. We'll test it with JUnit 5, and then we'll secure it. Since this is ASCII Doctor, I'm going to use my handy-dandy ASCII Doctor plugin. So it looks pretty good. So we'll start by creating a new project on start.spring.io with Web, Spring Data, Spring Data West, and H2. So if we use that link, you'll see it actually populates everything for us. The blog post uses 232, but 233 should work just fine. And then we'll click Generate. And then expand the downloaded package here. And we'll open that up in IntelliJ. Go into Downloads, GraphQL Demo, and Idea, Palm.xml. And then in the Palm.xml, we'll add a dependency on GraphQL Speaker. So that's GraphQL Schema Publisher and Query Resolver as a dependency. So we'll do that right here. Yes, I want to edit this now. So Dependency, then Artifact ID is GraphQL, SPQR. And then the Group ID, grab that right here. And then the version is 0.0.4. So let's make sure that's imported. And then we'll create a food entity class that we can query and update and all that. So call it food. And then I'm going to use one of my live templates. GraphQL food is the name of it. And you'll see it's just a regular JPA entity, except it uses these GraphQL annotations. So you can query by ID or you can query by name. And that's all you really need to do. So we're still using JPA, but it allows us to use GraphQL as well. If you want to look at my live templates, you can actually go to this GitHub URL, MRABLE Idea live templates. And in here, there's all the live templates that I've used in this demo. So if you wanted to import them into your Intel JN since you could use them as well. So we will create a food repository next. And this uses repository rest resource. But we actually don't need it. We just need repository. There we go. Okay. And now a food service to read, save and delete our entities. And we'll use GraphQL service. So you can save this uses GraphQL API annotation there. And then it allows you to query foods. Go ahead and read by ID. Create one with save food, delete food, and is good calculated property. So the cool thing about this is you're actually using GraphQL and returning a calculated property as an is good property. And it just looks and says, hey, avocado or spam. If those aren't in there, then it's good. The person that wrote this didn't like avocado or spam apparently. Now we'll create a beam to initialize the app with sample data. So we can do that right here. There we are. And so it uses an application runner and basically it goes and puts in pizza, spam, eggs and avocado and save those. So there's some sample data to start with. And then we'll enable the web UI to test the GraphQL API by adding this property in our application dot properties. And then we can run it. So you can do it from the command line. Let's try that using mbnw spring boot run. And if you type spring boot run all day and you kind of want to stop doing that in the build section, you can actually define a default goal and then make that spring boot run. And then all you have to type is mbn or mbnw depending on if you're using the wrapper or not. And so now we can open our browser to 8080. And you see this is a playground here. And we can run a sample query for the foods, right? And run it. And you'll see we get all the foods back, right? Pizza's good. Spam is not good. Eggs is good. Avocado's not. Kind of like avocado. And you can also specify a specific food by ID if you use this query. I didn't like it. Oh, we're missing that initial bracket. There we go. And now we just get back pizza, right? And that's all we get is just the name. So if we wanted to, you know, get back more, what you do is good and get a little code completion there. And then you get that back. So you just get what you want. And you're also able to manipulate this response, right? You know, the ID is not in there. If you want it to be in there, you could add it. And then now you get it. So you can create a new food by running a mutation. So save food. We give it the name. So it saved it. And now if we wanted to go back to what our last one was, undo. And now we see that pasta is in there. So cool. Everything's working. Now let's test it. In this IntelliJ project, there's a GraphQL test. So not much in here. Let's go ahead and place the foods. You'll see we use auto configure mock MVC. It's a little bit different from the default. And then we basically expect this response, right? All of those default data that we're putting in. So we're making a request to default data. We're passing in that. We're giving it the proper media types. And we expect that to be returned. Now we can stop this and we can run MVN test. There we go. Built success. Test passed. Nice. So there's Octa's Maven plugin to create an Octa account and configure a Spring Boot app to work with it. So you'll see it already has an existing one in there. So I'm going to override it. I have to type yes all caps. My name is Matt. So it creates a new account for you. It creates a new Octa organization. And when you run Spring Boot as part of this shortly after, it'll actually register your app on Octa and create a Spring Boot app. And it'll integrate those Octa settings into your app. We have to check our email for a verification code. Okay. So now it's all set up. We can open this link to reset our password. And pick a nice image. Now we're in. We're in our organization, but we don't really need to do anything in there yet. We need to create or run this command. So it doesn't need any input from us. It just automatically goes ahead and creates one. And so now if we go here and look at our applications, you'll see it created one. It took that artifact ID that it had and made all that work. And then if we look at general, you'll see it put in the proper login redirect URI for Spring Security and the Octa Spring Boot starter. And if we were to look in our application.properties, you can see it added those properties as well. So now we can create a MyAccessTokenController that displays the access token. So let's do that. You'll see what this does is it basically grabs Spring Security's OAuth client. Oh, this didn't work because as part of it, it also added the Spring Boot starter for Octa. So that is right down here. And we haven't updated Maven, right? To know about that. So now it's got the authorized client service here. And MyAccessToken basically goes and gets that using Spring Security's API. And it gets authorized registration ID. It gets the name. And then it's able to load the authorized client and get the access token and value from that. So now we can restart our app. Once that's up, we can hit the MyAccessToken. And since we logged in over here, we're still logged in and therefore this returned an access token. So now, if you go to the web UI, you can actually go to where is it? HTTP headers here. Well, let's use HTTP. So we can copy curl right here. See what that looks like. Nope, it's just our access token. Well, let's just define the access token. And then we can use HTTP.py. HTTP.Post. HTTP.Local.host.88. And then you'll see we got a 302. So that authorization token is not working. There we go. So something about setting that variable. I wasn't doing it right. Put it in there raw. Works just fine, right? So that's everything. And you can see that everything's secure now. And you have a fully secure GraphQL API. So if we were to run MVN test, our tests are going to fail because they haven't done anything for those to make them work. So MVN test. You can see that fails with the 403 because access does it tonight. Let's go ahead and fix that. So we can start by opening up palm.xml. Add a dependency on spring security test. Update mock MVC perform to add with the security mock post processors JWT. So right here in this perform, just add a new line. And then go ahead and import that. Oh, import it. Come on, baby, you can do it. Oh, we have an updated Maven. So IntelliJ used to have an automatic update Maven thing, but I'll now try it. Now automatically imported that. So we're good to go. And you could also do a static import. And you're going to make this even easier, right? If we were to just delete it. And you can do the static import. And it looks a lot better. So now let's see if that works. So it's just mocking out the JWT for us. So you can see now that passes. And we can add a few more tests too. So you see these ones, we've got a bunch of them. We've got an order annotation we need to import here from Jupyter. So we're going to add and remove food. And then we're going to post that. Import the static method there from GoMox or HTTP request. Oh, I must have done the wrong one. So static method. Let's do request builders. And then with resolves, yep. And that, so list foods and expect new foods not to be there. You add the new food right with this mutation. And then it should be there. And then you expect again when you list them and then you do a mutation to delete it. And then you list foods and expect new food not to be there. All right. Run add and remove. So you can see that test passed too. So you've developed a GraphQL API with Java and secured it with OAuth as a resource server. And everything works. And even the tests are pretty easy thanks to Spring Security and its ability to mock out JWT. So you can find all the code at octadeveloper octagraphql Java example. And of course the blog post is linked to it from that repo as well. So if you want to read the blog post a little more in depth in this video but hopefully this has been a good experience for you. If you want to follow my team we're on Octadev on Twitter. When we publish new tutorials or videos that's where we tell everyone about it. I'm on Twitter at mrable and we have a YouTube channel which you're probably watching this on. So Octadev on YouTube please subscribe and enjoy more of our videos as we publish them. Thanks for watching. Have a nice day.