Video Title: How to GraphQL in Java
Hello developers my name is matt rabe 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 octa developer 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 octa developer 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 plug-in 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.com.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 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 mrabel idealive templates and in here there’s all the live templates that i’ve used in this demo so if you want to import them into your intellij instance
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 see
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 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 bean 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 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.properties
And then we can run it so you could do it from the command line let’s try that using mvnw spring booth 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 nbn or nbnw 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 all right pizza is 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 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 we do is good and get a little co-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 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 list 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 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 ambient test there we go build success test passed nice so now we’re going to use octa’s maven plugin to create an octa account and configure our 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 you have to type yes little caps my name is matt so it creates a new account for you 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 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 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 my access token controller 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 uh right down here and we haven’t updated maven right to know about that so so now it’s got the authorized client service here and my access token basically goes and gets that using spring securities api and it gets authorized registration id gets the name and then
It’s able to you know 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 my access token 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 pi so um we can copy curl right here see what that looks like no it’s just our access token well let’s
Just define the access token and then we can use http pi http post http local host ada and 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 right put it in there raw
Works just fine right it shows everything and you can see that everything’s secure now and you have a fully secure graphql kpi so if we were to run nvm test our tests are going to fail because they haven’t done anything for those to make them work so let me untest
All right you can see that fails with the 403 because access is denied let’s go ahead and fix that so we can start by uh opening up palm.xml and we’ll add a dependency on spring security test and then we’ll update mock mvc perform to add with security mod post processors jwt
So right here in this perform just add a new line and then go ahead and import that now we import it come on baby you can do it oh we haven’t updated maven so intellij used to have an automatic update maven thing but i’ll now try it now it automatically
Imported that so we’re good to go and uh you could also do a static import and uh you’re gonna make this even easier right if we were to just delete it now you can do the static import and 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 jupiter so we’re going to add and remove
Food and then we’re going to post that import the static method there from go mock server http request oh i must done the wrong one so static method let’s do request builders and then with results yep and uh 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 pass 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 at jwt so you can find all the code on github
At octa developer octa graph ql 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 mrabel 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
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Who's left the t1000 out?) Nice guide!Thx.
Great tutorial and templates!
can you also some video on batching & caching?
Great tutorial! I exported as war file and upload on local tomcat server, but playground is not appear, showing 404. Plaese let me know
Great tutorial! Quick question…how did you generate the repository at 2:56? What plugin?
Great tutorial. Quick question, if we go for servless architecture, can we do that along with Netflix oss? I created a gateway and microservices using jhipster , how can deploy that in server less? Can you please create a tutorial for speingboot and AWS lamda gateway or GCP cloud function apigee
Nice video on java and graphql … I have one question how can we apply filter using graphql spqr annotations ??