One of the core design goals of GraphQLize is not to tie to any web development framework and remain as a drop-in JVM library.

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

Adding Dependencies

Let's start by creating new Scalatra project and then add the graphqlize-java & other required dependencies in build.sbt

// ...
resolvers += "Clojars" at ""
libraryDependencies ++= Seq(
// ...
"org.scalatra" %% "scalatra-json" % ScalatraVersion,
"org.json4s" %% "json4s-jackson" % "3.6.7",
// For Postgres
"org.postgresql" % "postgresql" % "42.2.10",
// For Mysql
"mysql" % "mysql-connector-java" % "8.0.19",
"com.zaxxer" % "HikariCP" % "3.4.2",
"org.graphqlize" % "graphqlize-java" % "0.1.0-alpha20"

Initializing GraphQLizeResolver

To initialize GraphQLizeResolver, we need a DataSource.

Configuring DataSource


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

// ScalatraBootstrap.kt
// ...
import com.zaxxer.hikari._
class ScalatraBootstrap extends LifeCycle {
def dataSource: HikariDataSource = {
val config = new HikariConfig
new HikariDataSource(config)
override def init(context: ServletContext) {
val graphqlResolver = new GraphQLizeResolver(dataSource)
// ...

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 data class to model the incoming GraphQL request.

Create a new file GraphQLizeScalatraServlet.scala and add the following

package org.graphqlize.scala
case class GraphQLRequest(query : String, variables : Option[Map[String, Object]])

Then add a new router /graphql and deserialize the request to this GraphQLRequest class using Jackson. Finally, get the query & the variables from the request and invoke the resolve method on the initialized instance of GraphQLizeResolver. It returns the result as stringified JSON

// imports are ignored for brevity
class GraphQLizeScalatraServlet(graphQLResolver : GraphQLResolver)
extends ScalatraServlet with JacksonJsonSupport {
protected implicit val jsonFormats: Formats = DefaultFormats
before() {
contentType = formats("json")
post("/graphql") {
val gqlReq =
if (gqlReq.variables.isDefined)
.resolve(gqlReq.query, gqlReq.variables.get.asJava)
// Patch:
object JsonMethodsExt extends org.json4s.jackson.JsonMethods {
def parse(in: JsonInput): JValue = super.parse(in, useBigIntForLong = false)
def parseOpt(in: JsonInput): Option[JValue] = super.parseOpt(in, useBigIntForLong = false)
// ScalatraBootstrap.scala
class ScalatraBootstrap extends LifeCycle {
// ...
override def init(context: ServletContext) {
val graphqlResolver = new GraphQLizeResolver(dataSource)
context.mount(new GraphQLizeScalatraServlet(graphqlResolver), "/*")

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" \

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 Scalatra.

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

When you restart the server, the Voyager will be available at http://localhost:8080/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 src/main/webapp directory.

This GraphQL playground will be available at http://localhost:8080/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.


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