pathom

:pathom: https://github.com/wilkerlucio/pathom/ & https://pathom3.wsscode.com & https://roamresearch.com/#/app/wsscode
lgessler 2020-07-25T20:36:21.353600Z

hi, I'm trying to implement some simple authentication via a pathom transform and i'm having some trouble when I want to return something that's not a result of calling resolve. putting code in reply

wilkerlucio 2020-07-26T13:15:53.355200Z

yes, looks strange, what parser and reader are you using?

lgessler 2020-07-26T15:14:22.355600Z

p/parser and pc/parallel-reader: https://github.com/lgessler/glam/blob/master/src/main/glam/server/pathom_parser.clj#L46

lgessler 2020-07-26T16:03:07.356Z

i'll try the sync reader in a sec and see what happens

lgessler 2020-07-26T16:20:40.356200Z

confirmed that it doesn't crash with pc/reader , though the return value is [{:all-users {}}] instead of {:server/error? true :server/message "..."} for some reason... anyway, it seems like it succeeds in preventing the call to the resolver to run, so this is good enough 🙂 the error message getting to the client isn't really important anyway

lgessler 2020-07-26T16:21:15.356400Z

would still be curious to know what i was doing wrong though... and i don't understand how big the perf difference is between parallel-reader and reader but i hope it's not terribly great

lgessler 2020-07-26T16:21:23.356600Z

thanks for the help @wilkerlucio!

wilkerlucio 2020-07-26T16:22:48.358200Z

yeah, with the parser you should use the reader2, parallel-reader is only compatible with parallel-parser, this should fix the issue :)

wilkerlucio 2020-07-26T16:23:24.359200Z

sorry the messy confusion around parsers and readers, thats I pain point I plan to fix

lgessler 2020-07-26T16:28:30.359400Z

ok great, thanks! also wow i just realized i had both parallel-reader and reader2 in my ::p/reader... i'm surprised that worked at all 😅

wilkerlucio 2020-07-26T17:22:20.359600Z

they work in a chain processing, so a reader can operate or delegate to the next, if you have then, what would happen is that the first would pick up

lgessler 2020-07-25T20:37:07.353700Z

so I define my transform:

(defn auth-tx [{::pc/keys [resolve] :keys [auth] :as outer-env}]
  "Transform for a Pathom resolver that checks whether the user has sufficient permissions
  for a given operation. Checks the resolver's env for the :auth keyword, which can
  currently be either :user or :admin"
  (assoc
    outer-env
    ::pc/resolve
    (fn [env params]
      (if (authorized env auth)
        (resolve env params)
        {:server/error? true :server/message "..."}))))))

lgessler 2020-07-25T20:37:53.353900Z

and then I put it on my resolver:

(pc/defresolver all-users-resolver [{:keys [neo4j]} _]
  {::pc/output    [{:all-users [:user/id]}]
   ::pc/transform mc/auth-tx
   :auth          :admin}
  ;;...
  )

lgessler 2020-07-25T20:38:46.354100Z

when the user is authorized to perform the action, everything works OK, but if they're not, I get an error: Exception in thread "async-dispatch-15" java.lang.IllegalArgumentException: No implementation of method: :take! of protocol: #'clojure.core.async.impl.protocols/ReadPort found for class: clojure.lang.PersistentArrayMap

lgessler 2020-07-25T20:39:41.354300Z

I'm confused because as far as I can tell (resolve env params) is just returning a plain map--when I replace it with {:all-users []} I don't get this :take! error. so shouldn't it be OK for me to return that map literal in the non-authorized case? is there some kind of function i need to use to wrap it?