-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring.cljc
33 lines (29 loc) · 1.02 KB
/
ring.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(ns grafeo.ring
(:require [#?(:clj cheshire.core :cljs goog.json) :as json]
[alumbra.printer :as printer]
[grafeo.util :as util]))
(defn- json-str
"Convert the `obj` to JSON."
[obj]
#?(:clj (json/generate-string obj)
:cljs (json/serialize (clj->js obj))))
(defn query
"Returns the :query parameter in the Ring request :body for `ast`."
[ast & [opts]]
(printer/pr-str ast opts))
(defn body
"Returns the Ring request :body for `ast`."
[ast & [{:keys [indentation variables]}]]
(json-str {:query (query ast {:indentation (or indentation 0)})
:variables (util/underscore-keys variables)}))
(defn request
"Returns a Ring request map for the GraphQL document."
[ast & [{:keys [indentation variables] :as opts}]]
(merge {:content-type "application/json"
:body (body ast opts)
:request-method :post
:scheme :http
:server-name "localhost"
:server-port 4000
:uri "/graphql"}
(dissoc opts :indentation :variables)))