Fork me on GitHub
#pathom
<
2021-06-08
>
MatthewLisp15:06:13

Hi everyone! I'm taking a look at the docs/guide regarding Pathom plugins, and i'm seeing this snippet

; define the extension wrapper fn
(defn protect-attributes-wrapper [mse]
 (fn [env source {:keys [key] :as ast}]
   (if (and (contains? source key)
            (contains? protected-attributes key))
 ; the output of this extension must be a map entry or nil
 ; a vector with two elements would also work, but creating a map entry is
 ; more efficient
 (coll/make-map-entry key ::protected-value)
 (mse env source ast))))
I'm wondering about the comment that states the output of this extension must be a map entry or nil However i see that the output is actually a call to the original function, in this example called mse What i'm missing here?

jmayaalv15:06:03

Hi @matthewlisp, the call to (mse env source ast) is in the else leg of the condition.

jmayaalv15:06:49

so in that sample (coll/make-map-entry key ::protected-value) it’s called if something needs to be hidden, if not continue with the normal execution: (mse env source ast)

MatthewLisp15:06:40

wow, right, the comment lines made me forget that we were inside an if statement haha, thanks

👍 3
bmaddy17:06:59

Does anyone know where I could find a simple example of how to connect pathom to datomic (or any other database)? I've tried doing it in a resolver, but it doesn't seem to recurse into child entities so my other resolvers don't get triggered.

(pc/defresolver bill-id-resolver
  [{:keys [db]} {:keys [bill/id]}]
  {::pc/input #{:bill/id}
   ::pc/output all-datomic-attributes}
  ;; {:db/id (:db/id (d/entity db [:bill/id id]))}
  ;; (select-keys (d/entity db [:bill/id id]) all-datomic-attributes)
  (d/pull db all-datomic-attributes [:bill/id id]))
Then I tried in a reader, but it's only getting triggered for the initial lookup-ref and not attributes in the associated pull expression.
(defn datomic-reader
  [{:keys [db ast] :as env}]
  (let [lookup-ref ((juxt p/ident-key p/ident-value) env)
        k (:dispatch-key ast)
        e (d/entity db lookup-ref)
        v (get e k)]
    (println lookup-ref k e v)
    (if v
      {k v}
      ::p/continue)))
Here's the parser I'm using for the reader version. The last-update-t-resolver is just a simple resolver that makes derived data. It works just fine in other parsers.
(def registry
  "All the resolvers to register with the parser."
  [#_bill-id-resolver
   last-update-t-resolver])

(def parser
  (p/parser
   {::p/env     {::p/reader [;; pass through values from the original entity map
                             p/map-reader
                             ;; look up most values in datomic
                             datomic-reader
                             ;; use resolvers for data that doesn't exist yet
                             pc/reader2
                             pc/open-ident-reader
                             ]}
    ::p/plugins [(pc/connect-plugin {::pc/register registry})
                 p/error-handler-plugin]}))

wilkerlucio15:06:28

reader is a low level abstraction, the resolvers are the correct layer to do that

thanks3 2
bmaddy22:06:11

Oops, forgot to include how I'm calling it:

(parser {:db (d/db conn)}
          [{[:ti.bill/id #uuid "799ce7c1-cade-4ecc-8a89-a1df186d724e"]
            [:db/id
             :ti.bill/pro]}])