Last active
February 21, 2019 02:30
-
-
Save souenzzo/afe8ab53cac43766a04fc02c52376fd9 to your computer and use it in GitHub Desktop.
lacinia HTTP API with generators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{:paths ["."] | |
:deps {org.clojure/clojure {:mvn/version "1.10.0"} | |
com.walmartlabs/lacinia {:mvn/version "0.32.0"} | |
io.pedestal/pedestal.service {:mvn/version "0.5.5"} | |
io.pedestal/pedestal.jetty {:mvn/version "0.5.5"} | |
com.walmartlabs/lacinia-pedestal {:mvn/version "0.11.0"} | |
org.clojure/test.check {:mvn/version "0.10.0-alpha3"} | |
com.rpl/specter {:mvn/version "1.1.2"}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Partido { | |
id: ID | |
nome: String | |
sigla: String | |
descricao: String | |
} | |
type Camarada { | |
id: ID | |
nome: String | |
partido: Partido | |
camaradas: [Camarada] | |
} | |
type Lei { | |
id: ID | |
descricao: String | |
autor: Camarada | |
pros: [Camarada] | |
cons: [Camarada] | |
} | |
type QueryRoot { | |
id: ID | |
Camaradas: [Camarada] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns user | |
(:require [com.walmartlabs.lacinia :as lacinia] | |
[com.walmartlabs.lacinia.pedestal :as lacinia.pedestal] | |
[com.walmartlabs.lacinia.schema :as lacinia.schema] | |
[com.walmartlabs.lacinia.parser.schema :as lacinia.parser.schema] | |
[io.pedestal.http :as http] | |
[com.rpl.specter :as sp] | |
[clojure.java.io :as io] | |
[clojure.spec.alpha :as s] | |
[clojure.test.check.generators :as gen])) | |
(def type->gen | |
{'ID (s/gen uuid?) | |
'String (s/gen string?)}) | |
(defn add-gen | |
[x] | |
(sp/transform (sp/walker :type) | |
(fn [{:keys [type] :as f}] | |
(let [many? (seq? type) | |
type-id (if many? (last type) type) | |
generator (get type->gen type-id (gen/hash-map :id (s/gen uuid?))) | |
generator (if many? | |
(gen/vector generator) | |
generator)] | |
(assoc f :resolve (fn [_ _ _] | |
(let [data (gen/generate generator)] | |
data))))) | |
x)) | |
(def schema | |
(-> (io/resource "schema.graphql") | |
slurp | |
(lacinia.parser.schema/parse-schema {}) | |
add-gen | |
(lacinia.schema/compile))) | |
(defonce http-state (atom nil)) | |
(defn -main | |
[& args] | |
(swap! http-state (fn [x] | |
(when x | |
(http/stop x)) | |
(-> (lacinia.pedestal/service-map schema {:graphiql true}) | |
http/default-interceptors | |
http/dev-interceptors | |
http/create-server | |
http/start)))) | |
(def context | |
{}) | |
(defn q | |
[& [query variables options]] | |
(lacinia/execute schema query variables context options)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment