Skip to content

walmartlabs/lacinia-pedestal

Repository files navigation

com.walmartlabs/lacinia-pedestal

Clojars Project CI API DOCS

A library that adds the Pedestal underpinnings needed when exposing Lacinia as an HTTP endpoint.

Lacinia-Pedestal also supports GraphQL subscriptions, using the same protocol as Apollo GraphQL.

Lacinia-Pedestal Manual | API Documentation

Usage

For a basic Pedestal server, simply supply a compiled Lacinia schema to the com.walmartlabs.lacinia.pedestal2/default-service function to generate a service, then invoke io.pedestal.http/create-server and /start.

(ns graphql-demo.server
  (:require [io.pedestal.http :as http]
            [com.walmartlabs.lacinia.pedestal2 :as lp]
            [com.walmartlabs.lacinia.schema :as schema]
            [com.walmartlabs.lacinia.util :as util]))

(def hello-schema 
  (-> {:objects 
        {:Query
         {:fields
            ;; String is quoted here; in EDN the quotation is not required 
            ;; You could also use :String
            {:hello {:type 'String}}}}}
       (util/inject-resolvers {:Query/hello (constantly "hello")})
       (schema/compile)))

;; Use default options:
(def service (lp/default-service hello-schema nil))

;; This is an adapted service map, that can be started and stopped.
;; From the REPL you can call http/start and http/stop on this service:
(defonce runnable-service (http/create-server service))

(defn -main
  [& args]
  (println "\nCreating your server...")
  (http/start runnable-service))

Lacinia will handle POST requests at the /api endpoint:

$ curl localhost:8888/api -X POST -H "content-type: application/json" -d '{"query": "{ hello }"}'
{"data":{"hello":"world"}}

You can also access the GraphQL IDE at http://localhost:8888/ide.

Development Mode

When developing an application, it is desirable to be able to change the schema without restarting. Lacinia-Pedestal supports this: in the above example, the schema passed to default-service could be a function that returns the compiled schema. It could even be a Var containing the function that returns the compiled schema.

In this way, the Pedestal stack continues to run, but each request rebuilds the compiled schema based on the latest code you've loaded into the REPL.

Beyond default-server

default-service is intentionally limited, and exists only to help you get started. Once you start adding anything more complicated, such as authentication, or supporting multiple schemas (or schema versions) at different paths, you will want to simply create your routes and server in your own code, using the building-blocks provided by com.walmartlabs.lacinia.pedestal2.

GraphiQL

The GraphiQL packaged inside the library is built using npm, from version 1.7.1.

If you are including lacinia-pedestal via Git coordinate (rather than a published version of the library by using a :mvn/version coordinate), then the library will need to be prepped for use via clj -X:deps prep.

The prep action for lacinia-pedestal requires that you have npm installed.
The prep action generates the CSS and JavaScript files that are used to execute GraphiQL.

License

Copyright © 2017-2024 Walmart

Distributed under the Apache Software License 2.0.

GraphiQL has its own license.