Created
May 6, 2019 19:35
-
-
Save jpthomson/435bb325b8bb854130757052b30f5b64 to your computer and use it in GitHub Desktop.
Simple pull implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn parse | |
([q] (parse q {})) | |
([q m] | |
(cond (list? q) (parse (first q) (assoc m :params (apply hash-map (rest q)))) | |
(map? q) (apply merge m (map parse (first q))) | |
(vector? q) (assoc m :children (mapv parse q)) | |
:else (assoc m :dispatch-key q)))) | |
(defn resolve-value [env {:keys [dispatch-key] :as ast} value] | |
(if-let [f (get-in env [:resolvers dispatch-key])] | |
(f env ast value) | |
(get value dispatch-key))) | |
(defn pull-ast [env ast value] | |
(reduce (fn [m {:keys [dispatch-key children] :as child}] | |
(let [rekur #(cond->> % children (pull-ast env child)) | |
itter #(if (sequential? %) (mapv rekur %) (rekur %))] | |
(assoc m dispatch-key (itter (resolve-value env child value))))) | |
{} (:children ast))) | |
(defn pull [env query value] | |
(pull-ast env (parse query) value)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment