graphql used for invoices
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
(ns auto-ap.graphql
|
||||
(:require
|
||||
[com.walmartlabs.lacinia.util :refer [attach-resolvers]]
|
||||
[com.walmartlabs.lacinia.schema :as schema]
|
||||
[com.walmartlabs.lacinia :refer [execute]]
|
||||
[com.walmartlabs.lacinia.executor :as executor]
|
||||
[com.walmartlabs.lacinia.resolve :as resolve]
|
||||
[auto-ap.db.invoices :as invoices]
|
||||
[auto-ap.db.vendors :as vendors]
|
||||
[auto-ap.db.companies :as companies]
|
||||
[auto-ap.db.utils :as utils]
|
||||
[clojure.walk :as walk])
|
||||
[com.walmartlabs.lacinia.util :refer [attach-resolvers]]
|
||||
[com.walmartlabs.lacinia.schema :as schema]
|
||||
[com.walmartlabs.lacinia :refer [execute]]
|
||||
[com.walmartlabs.lacinia.executor :as executor]
|
||||
[com.walmartlabs.lacinia.resolve :as resolve]
|
||||
[auto-ap.db.invoices :as invoices]
|
||||
[auto-ap.db.vendors :as vendors]
|
||||
[auto-ap.db.companies :as companies]
|
||||
[auto-ap.db.utils :as utils]
|
||||
[clojure.walk :as walk]
|
||||
[clojure.string :as str])
|
||||
(:import
|
||||
(clojure.lang IPersistentMap)))
|
||||
|
||||
@@ -19,14 +20,20 @@
|
||||
{
|
||||
:company
|
||||
{:fields {:id {:type 'Int}
|
||||
:name {:type 'String}}}
|
||||
:name {:type 'String}
|
||||
:email {:type 'String}}}
|
||||
:vendor
|
||||
{:fields {:id {:type 'Int}
|
||||
:name {:type 'String}}}
|
||||
:name {:type 'String}
|
||||
:invoice_reminder_schedule {:type 'String}}}
|
||||
:invoice
|
||||
{:fields {:id {:type 'Int}
|
||||
:total {:type 'String}
|
||||
:invoice_number {:type 'String}
|
||||
:date {:type 'String}
|
||||
:company_id {:type 'Int}
|
||||
:vendor {:type :vendor
|
||||
|
||||
:resolve :get-vendor}
|
||||
:company {:type :company
|
||||
:resolve :get-company}}}}
|
||||
@@ -44,22 +51,65 @@
|
||||
{}
|
||||
x))
|
||||
|
||||
(defn snake->kebab [s]
|
||||
(str/replace s #"_" "-"))
|
||||
|
||||
(defn kebab [x]
|
||||
(keyword (snake->kebab (name x))))
|
||||
|
||||
(defn kebab->snake [s]
|
||||
(str/replace s #"-" "_"))
|
||||
|
||||
(defn snake [x]
|
||||
(keyword (kebab->snake (name x))))
|
||||
|
||||
(defn ->graphql [m]
|
||||
(walk/postwalk
|
||||
(fn [node]
|
||||
(cond
|
||||
|
||||
(keyword? node)
|
||||
(snake node)
|
||||
|
||||
:else
|
||||
node))
|
||||
m))
|
||||
|
||||
(defn <-graphql [m]
|
||||
(walk/postwalk
|
||||
(fn [node]
|
||||
(cond
|
||||
|
||||
(keyword? node)
|
||||
(kebab node)
|
||||
|
||||
:else
|
||||
node))
|
||||
m))
|
||||
|
||||
(defn get-invoice [context args value]
|
||||
|
||||
(println (<-graphql args))
|
||||
(let [extra-context
|
||||
(cond-> {}
|
||||
(executor/selects-field? context :invoice/vendor) (assoc :vendor-cache (by (vendors/get-all) :id ))
|
||||
(executor/selects-field? context :invoice/company) (assoc :company-cache (by (companies/get-all) :id )))]
|
||||
(resolve/with-context (invoices/query args) extra-context)))
|
||||
(resolve/with-context
|
||||
(map
|
||||
->graphql
|
||||
(invoices/get-graphql (<-graphql args))) extra-context)))
|
||||
|
||||
(defn get-vendor [context args value]
|
||||
(if-let [vendor-cache (:vendor-cache context)]
|
||||
(vendor-cache (:vendor_id value))
|
||||
(vendors/get-by-id (:vendor_id value))))
|
||||
(->graphql
|
||||
(if-let [vendor-cache (:vendor-cache context)]
|
||||
(vendor-cache (:vendor_id value))
|
||||
(vendors/get-by-id (:vendor_id value)))))
|
||||
|
||||
(defn get-company [context args value]
|
||||
(if-let [company-cache (:company-cache context)]
|
||||
(company-cache (:company_id value))
|
||||
(companies/get-by-id (:company_id value))))
|
||||
(->graphql
|
||||
(if-let [company-cache (:company-cache context)]
|
||||
(company-cache (:company_id value))
|
||||
(companies/get-by-id (:company_id value)))))
|
||||
|
||||
(def schema
|
||||
(-> integreat-schema
|
||||
@@ -68,6 +118,8 @@
|
||||
:get-company get-company})
|
||||
schema/compile))
|
||||
|
||||
|
||||
|
||||
(defn simplify
|
||||
"Converts all ordered maps nested within the map into standard hash maps, and
|
||||
sequences into vectors, which makes for easier constants in the tests, and eliminates ordering problems."
|
||||
@@ -81,9 +133,15 @@
|
||||
(seq? node)
|
||||
(vec node)
|
||||
|
||||
(keyword? node)
|
||||
(kebab node)
|
||||
|
||||
:else
|
||||
node))
|
||||
m))
|
||||
|
||||
(defn query [q]
|
||||
(simplify (execute schema q nil nil)))
|
||||
(defn query
|
||||
([q]
|
||||
(simplify (execute schema q nil nil)))
|
||||
([q v]
|
||||
(simplify (execute schema q v nil))))
|
||||
|
||||
Reference in New Issue
Block a user