~drewr/baklava

c34e3c4ca3f227f1396b8b06230bda6b92b2419d — Drew Raines 2 months ago 977c063
Add graphql support
3 files changed, 39 insertions(+), 1 deletions(-)

M project.clj
A resources/schema/workday.edn
M src/baklava/workday.clj
M project.clj => project.clj +1 -0
@@ 6,6 6,7 @@
  :dependencies [[cheshire/cheshire "5.10.0"]
                 [clj-http/clj-http "3.12.2"]
                 [clojure.java-time/clojure.java-time "0.3.2"]
                 [com.walmartlabs/lacinia "1.2.2"]
                 ;; use a fork that fixes symbol collisions
                 [luhhujbb/clostache "1.5.0"]
                 [org.clojure/clojure "1.10.1"]

A resources/schema/workday.edn => resources/schema/workday.edn +12 -0
@@ 0,0 1,12 @@
{:objects
 {:Employee
  {:description "The primary entity in Workday"
   :fields {:id {:type ID}
            :name {:type (non-null String)}
            :employees {:type (list :Employee)}}}
  :Query
  {:fields
   {:employee
    {:type :Employee
     :description "Look up employee by name"
     :args {:name {:type String}}}}}}}

M src/baklava/workday.clj => src/baklava/workday.clj +26 -1
@@ 1,11 1,14 @@
(ns baklava.workday
  (:gen-class)
  (:require [cheshire.core :as json]
            [java-time :as time]
            [clojure.core.match :refer [match]]
            [clojure.edn :as edn]
            [clojure.spec.alpha :as s]
            [clojure.string :as str]
            [clojure.java.io :as io]
            [com.walmartlabs.lacinia :as lacinia]
            [com.walmartlabs.lacinia.util]
            [com.walmartlabs.lacinia.schema]
            [loom.io :as graph]
            [tech.v3.dataset :as ds]
            [ubergraph.core :as uber]


@@ 60,3 63,25 @@
                  (update-in m p (fnil #(assoc %1 %2 nil) {}) el))))
            {}
            (uber/nodes graph))))

(defn get-employee-by-name [dataset name]
  (let [rec (-> dataset
                (ds/filter-column
                 "Preferred Name"
                 #(= % name))
                ds/rows
                first)]
    (when rec
      {:id (str (get rec "Employee ID"))
       :name (get rec "Preferred Name")})))

(defn load-graphql [resource]
  (-> resource
      io/resource
      slurp
      edn/read-string
      (com.walmartlabs.lacinia.util/inject-resolvers
       {:Query/employee
          (fn [ctx args value]
            (get-employee-by-name (:source ctx) (:name args)))})
      com.walmartlabs.lacinia.schema/compile))