Ring
GraphQLize built on top of Lacinia, a de-facto library for implementing GraphQL APIs in Clojure.
Getting started with GraphQLize using Ring involves only few steps. Let's dive in.
Adding Dependencies
Create a new Clojure project using (or leiningen) and add the GraphQLize and other dependencies.
Configuring DataSource
The next step is configuring the DataSource. In this example, we are going to use to manage the database connection.
note
For brevity, this sample uses def
to define the states. In a real-world project, you can replace it with , , or .
- Postgres
- MySQL
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 .
Creating Lacinia Schema
Then create a lacinia schema from the data source using GraphQLize.
Adding GraphQL Endpoint
The final step is adding a Ring route /graphql
to expose the GraphQL API.
The graphql-handler
function does the following
- It de-serializes the GraphQL request body from the Ring request.
- Then using the
lacinia-schema
created in the previous step, it executes this GraphQL request by invoking the Lacinia's execute function. - Finally, it serializes the
result
of theexecute
function and send it back to the client as a Ring response.
note
This sample uses without coercion for simplicity. An example of the above sample using Reitit's Spec coercion is available in .
Test Drive
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.
To introspect, we are going to make use of , a tool to visualize GraphQL API as an interactive graph. To add Voyager, download this file and put it under the resources/static directory.
Then configure Ring to use this static directory for serving static files.
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 . Like Voyager, download this file and put in the static 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 .
note
You can also customize certain default behaviours of GraphQLize in future releases.