Combine GraphQL With Java to Build a Flexible and Modern API
Avoid under-fetching and over-fetching data when retrieving data using REST APIs.
Join the DZone community and get the full member experience.Join For Free
In the past few years, developers have used RESTful web services over HTTP(s) to expose business functions using an API. The REST API uses server-driven fixed data responses, which means a developer (client) can't determine the result of the response. Instead, the server sends all the data back to the client, which is called over-fetching. The developer (client) needs to invoke multiple REST APIs after the first call until the client gets the required data, which results in under-fetching.
To create new microservices, developers using these REST APIs have been looking for ways to minimize over-fetching and under-fetching when retrieving data along with business logic.
GraphQL provides a client-driven query language and runtime to prevent this overhead on the client-side and instead retrieve the exact data that the REST API requires. When GraphQL came out, many developers thought that it could replace existing REST API specifications. However, it's not a replacement but an alternative.
This article explains how to consume GraphQL services using Quarkus applications. Quarkus is a Kubernetes-based framework for writing Java applications. If you haven't created a Quarkus application before, please read Writing Java with Quarkus in VS Code before continuing.
Add GraphQL Extensions to Your Quarkus Project
You should see the following in the terminal:
Add an Entity and a Service for GraphQL's API
Create two entity Java classes—for this example, which queries data on movies. call them
Hero—to represent GraphQL schemas that are a set of possible data (e.g., objects, fields, relationships) that an end-user can retrieve:
Create a GraphQL API With CDI Bean
Implement a few methods to retrieve the
Hero data with parameters in the CDI (Contexts and Dependency Injection) bean class (e.g.,
GalaxyService.java). Generate some example data:
Now, create a GraphQL API class (e.g.,
FilmResource.java) to inject the CDI bean:
@GraphQLApi annotation enables you to use the CDI bean (e.g.,
GalaxyService) for a GraphQL endpoint.
@Query annotation allows you to make the method (e.g.,
getAllFilms) queryable with a specific name (e.g.,
Access GraphiQL's UI, a GraphQL Interface
smallrye-graphql extension enables you to use a GraphiQL tool for easy interaction with your GraphQL APIs when you run your Quarkus application. Access the GraphiQL user interface (UI) with the following endpoint after you start your application using Quarkus' dev mode (
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/ -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \ --\___\_\____/_/ |_/_/|_/_/|_|\____/___/ 2020-07-29 22:25:18,324 WARN [io.sma.graphql] (Quarkus Main Thread) SRGQL010000: Schema is null, or it has no operations. Not bootstrapping SmallRye GraphQL 2020-07-29 22:25:18,552 INFO [io.quarkus] (Quarkus Main Thread) quarkus-getting-started 1.0.0-SNAPSHOT on JVM (powered by Quarkus x.xx.x.) started in 1.246s. Listening on: http://0.0.0.0:8080 2020-07-29 22:25:18,553 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated. 2020-07-29 22:25:18,553 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy, smallrye-graphql]
Once the Quarkus runtime starts, access GraphiQL's UI locally using:
You will see:
Query the GraphQL API
Try querying GraphiQL using:
Click the Play button, and you will see:
Imagine a new client application requires
episodeID data but doesn't need to invoke the previous API (i.e., over-fetching), which includes unnecessary data (e.g.,
releaseDate). Try to query GraphiQL again with:
You will see:
GraphQL is only continuing to grow in popularity, and it integrates nicely with Java. If you're looking for more ways to learn, try changing the query data to meet your business requirements over GraphQL APIs. And remember, the complete Java code for this how-to is available in the GitHub repo.
This post was originally published by myself at Red Hat Developer Blogs.
Published at DZone with permission of Daniel Oh. See the original article here.
Opinions expressed by DZone contributors are their own.