Integrating GraphQL with a Java client (using Spring Boot)

How to create a simple GraphQL call from Java.

My initial reaction to figuring out GraphQL

There is quite a bit of activity in the GraphQL space. Apollo have a suite of tools as does the more official sounding GraphQL website, which also offers a set of libraries to help quick start your journey into GraphQL.

Both of these have great documentation on writing server-side GraphQL services but I found the documentation around writing client libraries difficult to follow and inconsistent.

Apollo has an android client library and code gen but only seems to work with Gradle (for Android usage) and a code-gen tool that generates to Scala. Good luck trying to find easy integration to a simple endpoint, you'll get lost in caching options and data-fetching. GraphQL is a simplification for APIs right? Also I don't really want Scala dependencies just to query an API.

So, you've spend a few hours reading up but still no closer to understanding how this whole thing works when you just want to get data. Then you look at terrible answers like this on Stack Overflow and get even more confused. Why is this so confusing?

Well, here's the thing. GraphQL is far more straightforward than these sites make out. It helps to think of interacting with GraphQL as you would interact with a database (hence the QL part!).

You can use builders for SQL too, ORMs to simplify development. Or you can just write it yourself. I'd recommend the latter until the space matures, particularly the Java GraphQL space. It's stuck between Javascript and Android at the moment.

Generally in Java you might look to query a db like this:

"select * from users where user_id = ?"

passing in some data as an integer.

With GraphQL it's the same thing. You have a query and you give it data. This is then embedded in a JSON string.

So, create a POJO with a variable and query member variables:

public class GraphQLQuery {
private Object variables;

private String query;

// Getters and Setters

Then put your generated GraphQL query and variables in:

String query =
    "\n    query Topology($duration: Duration!) {\n      getGlobalTopology(duration: $duration) {\n        nodes {\n          id\n          name\n          type\n          isReal\n        }\n        calls {\n          id\n          source\n          target\n          callType\n          detectPoint\n        }\n      }\n    }\n  ";

Duration duration = new Duration("2017-09", "2019-09", "MONTH");

GraphQLQuery topologyQuery = new GraphQLQuery();
// Use a singletonMap to retain the object name
topologyQuery.setVariables(Collections.singletonMap("duration", duration));

Object results = restTemplate.postForEntity("http://localhost:8080/api/graphql", topologyQuery, Object.class);

Hope this helps. If not, drop me an email and I'll take a look.

Subscribe to Antler - Applied Tracing

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.