Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gws/c6f9f2fa8b1ea95af9bea9277246953d to your computer and use it in GitHub Desktop.
Save gws/c6f9f2fa8b1ea95af9bea9277246953d to your computer and use it in GitHub Desktop.
Om.next: reading joins and unions while preserving remotes
;; 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