Spark Java

Getting started with GraphQLize in Spark Java is simple and involves only a few steps.

Adding Dependencies

Let's start with creating a new Java Project using Gradle (or Maven) and add the graphqlize-java & the JDBC driver dependencies.

Clojars Project

// ...
repositories {
// ...
maven { url "https://clojars.org/repo" }
}
// ...
dependencies {
// For Postgres
implementation 'org.postgresql:postgresql:42.2.10'
// For MySQL
implementation 'mysql:mysql-connector-java:8.0.19'
implementation 'org.graphqlize:graphqlize-java:0.1.0-alpha19'
// DB Connection Pooling
implementation 'com.zaxxer:HikariCP:3.4.2'
// JSON Deserialization
implementation 'com.fasterxml.jackson.core:jackson-core:2.10.3'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.10.3'
// ...
}

Initializing GraphQLizeResolver

To initialize GraphQLizeResolver, we need a DataSource.

Configuring DataSource

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
public class Program {
private static DataSource getDataSource() {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:postgresql://localhost:5432/sakila");
config.setUsername("postgres");
config.setPassword("postgres");
return new HikariDataSource(config);
}
public static void main(String[] args) {
DataSource dataSource = getDataSource();
}
}
note

Make sure you are changing the above values to refer your database connection. The above example assumes that you are using the sakila database created from this JOOQ's example repository.

With the data source in place, all we need to do is the create a new instance of GraphQLizeResolver using it.

import org.graphqlize.java.GraphQLResolver;
import org.graphqlize.java.GraphQLizeResolver;
// ...
public class Program {
// ...
public static void main(String[] args) {
DataSource dataSource = getDataSource();
GraphQLResolver graphQLResolver =
new GraphQLizeResolver(dataSource);
}
}
note

Currently, it takes around 8 to 12 seconds to initialize. I am planning to work on it in a future release.

Adding GraphQL Endpoint

The next step is adding an API to expose the GraphQL endpoint. To do it, first, add a POJO to model the incoming GraphQL request.

import java.util.Map;
// ...
class GraphQLRequest {
private String query;
private Map<String, Object> variables;
// ... Getters & Setters are ignored for brevity
}
// ...

Then add a new router /graphql and deserialize the request to this GraphQLRequest class using Jackson.

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import static spark.Spark.post;
// ...
public class Program {
// ...
public static void main(String[] args) {
// ...
post("/graphql", (req, res) -> {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
GraphQLRequest graphQlRequest = objectMapper.readValue(req.body(), GraphQLRequest.class);
});
}
}

Finally, get the query & the variables from the request and invoke the resolve method on the initialized instance of GraphQLizeResolver.

// ...
public class Program {
// ...
public static void main(String[] args) {
// ...
post("/graphql", (req, res) -> {
// ...
String query = graphQlRequest.getQuery();
Map<String, Object> variables = graphQlRequest.getVariables();
String result = graphQLResolver.resolve(query, variables);
res.header("Content-Type", "application/json");
return result;
});
}
}

It returns the result as stringified JSON, and we are sending it as response body with the content type as application/json.

Test Drive

To do a test drive of this implementation, start the server and hit the endpoint via curl.

> curl -X POST \
--data '{"query": "query { actorByActorId(actorId: 1){firstName}}"}' \
-H "Content-Type: application/json" \
http://localhost:4567/graphql

You'll get a response like below.

{
"data": {
"actorByActorId": {
"firstName": "PENELOPE"
}
}
}

GraphQL Playground and Voyager

With the GraphQL endpoint up and running, the next step is introspecting the GraphQL schema and try out some more queries.

To introspect, we are going to make use of Voyager, a tool to visualize GraphQL API as an interactive graph. Adding it to our project is easy thanks to static content serve capability of Spark Java.

All you need to do is download this voyager.html file and put it under the src/main/resources/public directory.

Then let the Spark Java know to use this public directory to serve static files.

import static spark.Spark.staticFiles;
// ...
public class Program {
// ...
public static void main(String[] args) {
// ...
staticFiles.location("/public");
// ...
}
}

When you restart the server, the Voyager will be available at http://localhost:4567/voyager.html. A sample output would look like this.

Then to interact with the GraphQL API, let's add the GraphQL Playground. Like Voyager, download this playground.html file and put in the public directory.

This GraphQL playground will be available at http://localhost:4567/playground.html after server restart.

Next Steps

Congrats! You are on course to build impressive applications using GraphQLize in less time. To save yourself some more time, do refer this documentation to know more about how GraphQLize generates the GraphQL schema and the queries.

The sample code is available in this GitHub Repository.

note

You can also customize certain default behaviours of GraphQLize in future releases.