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 Ktor is simple and involves only a few steps.
Let's start with creating a new Ktor Project using Gradle & Netty. Under Server settings, select Jackson & Content Negotiation.
Then in the created project add the
graphqlize-java & the JDBC driver dependencies.
GraphQLizeResolver, we need a DataSource.
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.
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.
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, and we are sending it as response body with the content type as
To do a test drive of this implementation, start the server and hit the endpoint via curl.
You'll get a response like below.
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.
All you need to do is download this voyager.html file and put it under the /resources directory.
Then configure Ktor to serve this file.
When you restart the server, the Voyager will be available at http://localhost:8080/voyager.html. A sample output would look like this.
This GraphQL playground will be available at http://localhost:8080/playground.html after server restart.
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.