| id | pedestal |
|---|---|
| title | Pedestal |
| sidebar_label | 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.
Create a new Clojure project using deps (or leiningen) and add the GraphQLize and other dependencies.
;; deps.edn
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
org.graphqlize/graphqlize {:mvn/version "0.1.0-alpha20"}
;; 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"}}}The next step is configuring the DataSource. In this example, we are going to use Hikari Connection Pool 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 Component, Mount, or Integrant.
:::
import Tabs from "@theme/Tabs"; import TabItem from "@theme/TabItem";
<Tabs defaultValue="postgres" values={[ { label: 'Postgres', value: 'postgres' }, { label: 'MySQL', value: 'mysql' } ] }>
;; 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"}));; src/server.clj
(ns server
(:require [hikari-cp.core :as hikari]))
(def db-spec (hikari/make-datasource {:server-name "localhost"
:maximum-pool-size 1
:jdbc-url "jdbc:mysql://localhost:3306/sakila"
:driver-class-name "com.mysql.cj.jdbc.MysqlDataSource"
:username "root"
:password "mysql123"})):::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.
:::
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))The final step is adding pedestal endpoint to expose the /graphql API. With the help of lacinia-pedestal 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))To do a test drive of this implementation, start the server
> clj -m serverand hit the endpoint via curl.
> curl -X POST \
--data '{"query": "query { actorByActorId(actorId: 1){firstName}}"}' \
-H "Content-Type: application/json" \
http://localhost:8080/graphqlYou'll get a response like below.
{
"data": {
"actorByActorId": {
"firstName": "PENELOPE"
}
}
}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. To add Voyager, download this voyager.html 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 GraphQL Playground. Like Voyager, download this playground.html file and put in the static directory.
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.
:::note You can also customize certain default behaviours of GraphQLize in future releases. :::
