Enterprise Java

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.

Siva Janapati

Siva Prasad Rao Janapati is an Architect. He has hands on experience on Java, JEE, Spring, Oracle Commerce, MOZU Commerce, Apache Solr, Apache Kafka, Node.js, JBoss, Hibernate, Memcached, MySql, Oracle, MongoDB, APIGEE, Cloud Native, BlockChain and other open source/enterprise technologies. He loves to explore new technologies and trends.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button