-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.cljd
94 lines (80 loc) · 2.38 KB
/
api.cljd
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(ns tea.api
(:require
[cljd.walk :refer [keywordize-keys]]
[clojure.string :refer [join]]
["package:graphql/client.dart" :as g]))
(defn- log [& args]
(dart:core/print (join " " args)))
(def ^{:private true :const true}
base-url "https://countries.trevorblades.com/")
(def ^:private http-link (g/HttpLink base-url))
(def ^{:private true :conts true} query-get-countries
"query {
countries{
name
code
}
}")
(def ^{:private true :const true} query-get-country
"query getCountry($code:ID!){
country(code:$code){
name
capital
code
native
currency
phone
emoji
continent {
name
code
}
}
}")
(def ^{:private true :const true} query-get-continents
"query getContinent($code:ID!){
continent(code:$code){
name
code
countries { name code }
}
}")
(def ^:private client
(g/GraphQLClient :link http-link
:cache (g/GraphQLCache :store (g/InMemoryStore.))))
(defn- keywordize-blunt [entry]
(cond
(dart/is? entry Map)
(loop [result {}
entries (seq entry) ]
(let [[k v] (first entries)]
(if k
(recur
(assoc result
(keyword k)
(keywordize-blunt v))
(rest entries))
result)))
(dart/is? entry List)
(map (fn [e] (keywordize-blunt e)) entry)
:else entry))
(defn ^:async get-countries []
(let [opts (g/QueryOptions :document (g/gql query-get-countries))
result (-> client (.query opts) await)]
(if (.hasException result)
{:error (.exception! result)}
(keywordize-keys (-> result .data keywordize-blunt :countries)))))
(defn ^:async get-country [code]
(let [opts (g/QueryOptions :document (g/gql query-get-country)
:variables (. {"code" code} #/(cast String dynamic)))
result (-> client (.query opts) await)]
(if (.hasException result)
{:error (.exception! result)}
(keywordize-keys (-> result .data keywordize-blunt :country)))))
(defn ^:async get-continent [code]
(let [opts (g/QueryOption :document (g/gql query-get-continents)
:variables (. {"code" code} #/(cast String dynamic)))
result (-> client (.query opts) await)]
(if (.hasException result)
{:error (.exception! result)}
(keywordize-keys (-> result .data keywordize-blunt :continent)))))