Fork me on GitHub
#pathom
<
2019-04-08
>
Chris O’Donnell01:04:52

I'm trying to write a plugin which adds an authorization header to graphql requests. This is what I have right now, but it doesn't seem to be adding the header as I expect it would. (I've checked that id-token-str has the expected value.) Any thoughts on what's going wrong?

(def auth-plugin
  {::p/wrap-parser
   (fn [parser]
     (fn [env tx]
       (let [{:keys [id-token-str]} @auth]
         (parser (cond-> env
                   (seq id-token-str)
                   (assoc-in [::p.http/headers :authorization] (str "Bearer " id-token-str)))
                 tx))))})

(def parser
  (p/parallel-parser
   {::p/env {::p/reader [p/map-reader
                         pc/parallel-reader
                         pc/open-ident-reader
                         p/env-placeholder-reader]
             ::p/placeholder-prefixes #{">"}
             ::p.http/driver p.http.fetch/request-async}
    ::p/mutate pc/mutate-async
    ::p/plugins [(pc/connect-plugin {::pc/indexes indexes})
                 auth-plugin
                 p/error-handler-plugin
                 p/request-cache-plugin
                 p/trace-plugin]}))

wilkerlucio09:04:07

hello @codonnell, I don't see any graphql integration in your setup, usually the request for that is part of this integration, or just you just mean regular http calls?

Chris O’Donnell10:04:21

Was trying to avoid pasting a whole bunch of code; I'll include a snippet with all of the relevant code.

Chris O’Donnell10:04:57

@wilkerlucio I did mean graphql; I'm using the connect integration.

wilkerlucio10:04:43

@codonnell gotcha, ok, so your setup looks correct, are you able to load the graphql schema without providing the auth? that's the only place I see that would not go though the plugin and so would miss the auth

Chris O’Donnell10:04:44

Oh derp, that is exactly the problem.

Chris O’Donnell11:04:13

Never mind, that was a problem. Still not seeing the authorization header get added when I move the schema fetching to after I add the token value.

wilkerlucio11:04:15

I believe this new setup still doesn't send the auth, because it doesn't go though the parser, so you have to add the headaer in the my-gql when sending to load index

wilkerlucio11:04:47

I did check the code, the env is send though from the parser to the gql, so I believe it will work once there

wilkerlucio11:04:04

another important thing to notice is that the specific namespaces are important on the connect integration

wilkerlucio11:04:26

so since your prefix is ::pcg/prefix "little-gift-list.type", every attribute must start with that

wilkerlucio11:04:57

so in the GiftList for example, instead of :query [::gift/id ::gift/name you probably want :query [:ittle-gift-list.type.gift/id :ittle-gift-list.type.gift/name ...

wilkerlucio11:04:34

ah, nevermind, I just realised you did proper alias for those, nice 🙂

wilkerlucio11:04:17

probably still an issue with the load-index

Chris O’Donnell11:04:59

Ah of course, that makes sense. I replaced (pcg/load-index my-gql indexes) with (pcg/load-index (assoc-in my-gql [::p.http/headers :Authorization] (str "Bearer " (-> auth/auth deref :id-token-str)))) and can confirm that the header is added. Thanks for the help!

metal 4
Chris O’Donnell23:04:07

Is there a way to coerce values that come back in graphql responses? For example, I have uuids and timestamps which are encoded as json strings that I'd love to convert to uuid/datetime types before transacting them into my app db.