Created
May 3, 2017 15:41
-
-
Save gws/c6f9f2fa8b1ea95af9bea9277246953d to your computer and use it in GitHub Desktop.
Om.next: reading joins and unions while preserving remotes
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
;; Attribution note: this was taken from @petterik on Slack on 2017-05-02 | |
;; Here are two functions I use for reading nested queries, joins and unions. | |
(defn read-join [{:keys [parser query target ast] :as env}] | |
(let [ret (parser env query target)] | |
(if target | |
(when (seq ret) | |
{target (assoc ast :query ret)}) | |
{:value ret}))) | |
(defn read-union [{:keys [query] :as env} union-key] | |
(let [union-query (cond-> query (map? query) (get union-key))] | |
(read-join (assoc env :query union-query)))) | |
;; Here's what the implementation of your reads would be with them | |
(defmethod app-read :router-data [{:keys [query parser] :as env} k p] | |
(read-join env)) | |
(defmethod app-read :route/data [{:keys [state query parser] :as env} k p] | |
(let [st @state | |
route (get st :app/route)] | |
(read-union env route))) | |
;; Since joining child component's queries is so common, I've special cased | |
;; keys starting with "join", e.g. {:join/child (om/get-query Child)} | |
(defmethod app-read :default | |
[env k p] | |
(= "join" (namespace k)) | |
(read-join env)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment