started adding graphql

This commit is contained in:
Bryce Covert
2018-04-10 18:36:20 -07:00
parent 53905c317b
commit 4165c7d180
9 changed files with 136 additions and 7 deletions

View File

@@ -0,0 +1,89 @@
(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])
(:import
(clojure.lang IPersistentMap)))
(def integreat-schema
{
:objects
{
:company
{:fields {:id {:type 'Int}
:name {:type 'String}}}
:vendor
{:fields {:id {:type 'Int}
:name {:type 'String}}}
:invoice
{:fields {:id {:type 'Int}
:company_id {:type 'Int}
:vendor {:type :vendor
:resolve :get-vendor}
:company {:type :company
:resolve :get-company}}}}
:queries
{:invoice {:type '(list :invoice)
:args {:imported {:type 'Boolean}
:company_id {:type 'Int}}
:resolve :get-invoice}}})
(defn by [x kf]
(reduce
(fn [m x]
(assoc m (kf x) x))
{}
x))
(defn get-invoice [context args value]
(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)))
(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))))
(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))))
(def schema
(-> integreat-schema
(attach-resolvers {:get-invoice get-invoice
:get-vendor get-vendor
: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."
[m]
(walk/postwalk
(fn [node]
(cond
(instance? IPersistentMap node)
(into {} node)
(seq? node)
(vec node)
:else
node))
m))
(defn query [q]
(simplify (execute schema q nil nil)))