Fork me on GitHub
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
   (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)))

(def parser
   {::p/env {::p/reader [p/map-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})


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.


@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.


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


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


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


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


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


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


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.