graphql used for invoices

This commit is contained in:
Bryce Covert
2018-04-12 10:17:15 -07:00
parent 4165c7d180
commit 7425f7f393
15 changed files with 266 additions and 115 deletions

View File

@@ -5,6 +5,9 @@
[cljs-time.coerce :as c]
[cljs-time.core :as time]
[cljs.core.async :refer [<!]]
[clojure.string :as str]
[clojure.walk :as walk]
[venia.core :as v]
[auto-ap.history :as p]
[pushy.core :as pushy]))
@@ -54,3 +57,50 @@
(dates->date-times)
(conj on-success)
(re-frame/dispatch)))))))
(defn kebab->snake [s]
(str/replace s #"-" "_"))
(defn snake [x]
(if (namespace x)
(keyword (namespace x) (kebab->snake (name x)))
(keyword (kebab->snake (name x)))))
(defn ->graphql [m]
(walk/postwalk
(fn [node]
(cond
(keyword? node)
(snake node)
:else
node))
m))
(re-frame/reg-fx
:graphql
(fn [{:keys [query on-success on-error token variables query-obj]}]
(go
(let [headers (if token
{"Authorization" (str "Token " token)}
{})
query (or query (v/graphql-query (->graphql query-obj)))
response (<! (http/request {:method :get
:headers headers
:url (str "/api/graphql?query=" (js/encodeURIComponent query)
"&variables=" (pr-str (or variables {})))}))]
(if (>= (:status response) 400)
(when on-error
(->> response
:body
:data
(dates->date-times)
(conj on-error)
(re-frame/dispatch)))
(->> response
:body
:data
(dates->date-times)
(conj on-success)
(re-frame/dispatch)))))))