Fork me on GitHub
#pathom
<
2020-07-25
>
lgessler20:07:21

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

lgessler20:07:07

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 "..."}))))))

lgessler20:07:53

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}
  ;;...
  )

lgessler20:07:46

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

lgessler20:07:41

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?

wilkerlucio13:07:53

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

lgessler16:07:07

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

lgessler16:07:40

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

lgessler16:07:15

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

lgessler16:07:23

thanks for the help @U066U8JQJ!

wilkerlucio16:07:48

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

wilkerlucio16:07:24

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

lgessler16:07:30

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 😅

wilkerlucio17:07:20

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