lift_enums.clj 1.22 KB
Newer Older
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
(ns lacinia-datomic-pull.lift-enums
  (:require [lacinia-datomic-pull.pattern :refer [enum?]]))

(defn selections->enum-paths [schema selections & [path]]
  (let [path (or path [])]
    (reduce (fn [o {:keys [leaf? field selections] :as selection}]
              (let [newpath (conj path field)]
                (cond (enum? schema selection) (conj o newpath)
                      leaf? o
                      :else (into o (selections->enum-paths schema selections newpath)))))
            []
            selections)))

(defn set-path [m paths cb]
  (let [path (first paths)
        v (get m path ::none)]
    (cond (= ::none v)
          m

          (= 1 (count paths))
          (assoc m path (if (vector? v)
                          (mapv cb v)
                          (cb v)))

          (vector? v)
          (assoc m path (mapv #(set-path % (rest paths) cb) v))

          :else (assoc m path (set-path v (rest paths) cb)))))

(defn lift-enums
  [context ent]
  (reduce (fn [o path] (set-path o path (fn [x] (keyword (name (:db/ident x))))))
          ent
          (selections->enum-paths (:com.walmartlabs.lacinia.constants/schema context)
                                  (:selections (:com.walmartlabs.lacinia/selection context)))))