Home » Java » Enterprise Java » Are you ready to see GraphQL in action?

About Siva Janapati

Siva Janapati
Siva Prasad Janapati is a software engineer working in the e-Commerce domain. He has hands on experience on Java, JEE, Spring, Apache Solr, Node.js, JBoss and other open source/enterprise technologies. He loves to explore new technologies and trends.

Are you ready to see GraphQL in action?

In last article we have discussed about GraphQL advantages over REST. In this article we will see GraphQL in action. I have created a sample application to showcase differences between REST and GraphQL. First we will see REST implementation of simple product detail end point. I have used Spring Boot to demonstrate REST. Download sample project and follow steps outlined in README to setup the project. I am not discussing setup details here as it is out of scope for this article. Assuming that your project is up and running to make a call to http://localhost:8080/product/{product_id} endpoint to get product detail JSON as shown below.

GraphQL

If you observe above JSON, we are getting entire product JSON including reviews and technical specifications though we are not interested about all the elements of a given product.

Now we will see GraphQL in action by getting product details in selective manner. To demonstrate GraphQL again I used Spring Boot. Download sample project and follow steps outlined in README to setup the project. I am not discussing setup details here as it is out of scope for this article. Assuming that your project is up and running to see GraphQL in action. In this case I am interested to get only product id, title, short description and list price of a given product. Let us see how we can query to get interested details.

GraphQL

Now as a service consumer I am interested to get product id, title, short description, list price and reviews. In this case GraphQL gives flexibility to query what we want. See below query and response when we use GraphQL.

GraphQL

To demonstrate GraphQL I have used GUI based plugin GraphiQL. For consuming from other applications we can configure endpoint in application.properties.

graphql.servlet.mapping=/graphql
graphql.servlet.enabled=true
graphql.servlet.corsEnabled=true

Now we can make a call to above end point by passing  URL encoded query parameter as shown below. You can learn more about query and mutations https://graphql.org/learn/queries/

GraphQL

Hope you enjoyed this article. I will comeback with another article. Till then, Happy Learning!!!

Published on Java Code Geeks with permission by Siva Janapati, partner at our JCG program. See the original article here: Are you ready to see GraphQL in action?

Opinions expressed by Java Code Geeks contributors are their own.

(-1 rating, 1 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of