Pedestal

GraphQLize built on top of Lacinia, a de-facto library for implementing GraphQL APIs in Clojure.

Getting started with GraphQLize using Pedastal 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.

Clojars Project

;; deps.edn
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
org.graphqlize/graphqlize {:mvn/version "0.1.0-alpha21"}
;; db connection pool
hikari-cp {:mvn/version "2.10.0"}
;; for postgres
org.postgresql/postgresql {:mvn/version "42.2.8"}
;; for MySQL
mysql/mysql-connector-java {:mvn/version "8.0.19"}
;; Lacinia <-> Pedastal Service
com.walmartlabs/lacinia-pedestal {:mvn/version "0.13.0-alpha-1"}}}

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 .

;; src/server.clj
(ns server
(:require [hikari-cp.core :as hikari]))
(def db-spec (hikari/make-datasource {:adapter "postgresql"
:database-name "sakila"
:server-name "localhost"
:port-number 5432
:maximum-pool-size 1
:username "postgres"
:password "postgres"}))
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.

(ns server
(:require ; ...
[graphqlize.lacinia.core :as l]))
(def db-spec ...)
(def lacinia-schema (l/schema db-spec))

Adding GraphQL Endpoint

The final step is adding pedestal endpoint to expose the /graphql API. With the help of library, we can do it with ease.

(ns server
(:require ; ...
[com.walmartlabs.lacinia.pedestal :as lacinia-pedestal]))
(def db-spec ...)
(def lacinia-schema ...)
(def service
(lacinia-pedestal/service-map
lacinia-schema {:port 8080}))
(defonce runnable-service (server/create-server service))
(defn -main []
(server/start runnable-service))

Test Drive

To do a test drive of this implementation, start the server

> clj -m server

and hit the endpoint via curl.

> curl -X POST \
--data '{"query": "query { actorByActorId(actorId: 1){firstName}}"}' \
-H "Content-Type: application/json" \
http://localhost:8080/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 , 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 pedestal to use this static directory for serving static files.

(ns server
(:require ; ...
[io.pedestal.http :as http]))
; ...
(def service
(assoc
(lacinia-pedestal/service-map
lacinia-schema {:port 8080})
::http/resource-path
"/static"))
; ...

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.