Last active
July 7, 2021 08:19
-
-
Save MrEbbinghaus/eb14937a0b0909530cbfa85c26c79139 to your computer and use it in GitHub Desktop.
Access plugin for pathom
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
(defmulti check-access! (fn [_env [k v]] k)) | |
(defmethod check-access! :decide.models.proposal/id | |
[env input] | |
; Add code to check access and add input to access-cache | |
false) | |
(defmethod check-access! :default [_ _] true) | |
(defn has-access? [env input] | |
(or | |
(contains? @(:access-cache env) input) | |
(check-access! env input))) | |
(def access-plugin | |
{::p/wrap-parser | |
(fn [parser] | |
(fn [env tx] | |
(parser (assoc env :access-cache (atom #{})) tx))) | |
::pc/wrap-resolve | |
(fn [resolve] | |
(fn [env input] | |
(let [allowed? | |
(if (seq? input) ; batched? | |
(every? (fn [input] (every? #(has-access? env %) input)) input) | |
(every? #(has-access? env %) input))] | |
(when allowed? | |
(resolve env input)))))}) | |
;; To optimize access-checks resolvers can add access prematurely | |
(defresolver resolve-proposals [{:keys [db access-cache] :as env} {::process/keys [slug]}] | |
{::pc/output [{::process/proposals [::proposal/id]}]} | |
(if-let [response (d/pull db [{::process/proposals [::proposal/id]}] [::process/slug slug])] | |
(do | |
(swap! access-cache (fn [state] (apply conj state (map #(find % ::proposal/id) (::process/proposals response))))) | |
response) | |
{::process/proposals []})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I could generalize the plugin to make it reusable.