Created
June 20, 2017 03:23
-
-
Save brandonbloom/5bc8375a25eb733c41ed98f0270786e5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(ns blah | |
(:require [com.walmartlabs.lacinia.schema :as schema] | |
[com.walmartlabs.lacinia.util :refer [attach-resolvers]] | |
[com.walmartlabs.lacinia :as lacinia])) | |
(defn batch [ctx id] | |
(prn 'batch id) | |
(swap! (::loader ctx) | |
(fn [loader] | |
(if (contains? (:cache loader) id) | |
loader | |
(update loader :pending conj id)))) | |
id) | |
(defn demand [ctx id] | |
(swap! (::loader ctx) | |
(fn [{:keys [pending cache] :as loader}] | |
(if (seq pending) | |
(do (prn 'flush pending) | |
{:pending #{} | |
:cache (into cache (for [p pending] | |
[p {:label (str "node" p) | |
:left (inc p) | |
:right (* p 2)}]))}) | |
loader))) | |
(get-in @(::loader ctx) [:cache id])) | |
(def definition | |
{:objects {:tree {:fields {:label {:type 'String} | |
:left {:type :tree | |
:resolve :child} | |
:right {:type :tree | |
:resolve :child}}}} | |
:queries {:root {:type :tree :resolve :root}}}) | |
(defn resolve-root [ctx args v] | |
{:label "root" | |
:left (batch ctx 5) | |
:right (batch ctx 20)}) | |
(defn resolve-child [ctx args v] | |
(demand ctx (:field-name ctx))) | |
(def resolvers | |
{:root resolve-root | |
:child resolve-child}) | |
(defn decorator [object-name field-name f] | |
(fn [ctx args v] | |
(f (assoc ctx :object-name object-name :field-name field-name) | |
args | |
v))) | |
(def schema | |
(-> definition | |
(attach-resolvers resolvers) | |
(schema/compile {:decorator decorator}))) | |
(defn execute [gql] | |
(let [vars nil | |
ctx {::loader (atom {:pending #{} :cache {}})}] | |
(lacinia/execute schema gql nil ctx))) | |
(comment | |
(fipp.edn/pprint | |
(execute " | |
query { | |
root { | |
label | |
left { | |
label | |
left { | |
label | |
} | |
right { | |
label | |
} | |
} | |
right { | |
label | |
left { | |
label | |
} | |
right { | |
label | |
} | |
} | |
} | |
} | |
")) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment