-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.clj
58 lines (49 loc) · 1.36 KB
/
http.clj
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(ns grafeo.http
(:require [clj-http.client :as http]
[grafeo.core :as lang]
[grafeo.ring :as ring]
[grafeo.util :as util]))
(def defaults
"The default client options."
{:accept "application/json"
:as :auto
:coerce :always
:date-format "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"})
(defn wrap-defaults [client]
(fn [request]
(client (merge defaults request))))
(defn wrap-document [client]
(fn [{:keys [document] :as request}]
(client (if document
(ring/request document request)
request))))
(defn wrap-response [client]
(fn [request]
(let [response (client request)]
(update response :body
(fn [body]
(if (or (map? body)
(sequential? body))
(util/hyphenate-keys body)
body))))))
(def request*
(-> http/request
wrap-defaults
wrap-document
wrap-response))
(defn request [client doc & [opts]]
(let [doc' (lang/parse-document doc)]
(request* (merge client {:document doc'} opts))))
(comment
(->> (request
defaults
'((query
HeroNameAndFriends
[($episode Episode)]
(hero
[(episode $episode)]
name
(friends name))))
{:variables {:episode "JEDI"}})
:body clojure.pprint/pprint)
)