Fork me on GitHub
#graphql
<
2023-04-26
>
kpav16:04:37

Hey all! I've got a question: How can I add an unauthenticated route to the service map generated by lacinia.pedestal2? I'm trying to add a /health endpoint to the service-map generated by lacinia.pedestal2/default-service. I've basically got it "working", but the issue I'm running into is with the authentication interceptor I wrote -- I'd like all the Lacinia related endpoints to be authenticated against, but I'd like /health to be open. Code in ๐Ÿงต

kpav16:04:57

The code looks something like this (this is slightly simplified from what I'm actually doing, but the core logic is here):

(let [health-endpoint #{["/health" :get (fn health [_] {:status 200 :headers {} :body "true"}) :route-name ::health]}]
  (-> schema/load-schema
      (lp/default-service {:port 8000 :host "localhost"})
      (update ::http/routes #(into % health-endpoint))
      http/default-interceptors
      (update ::http/interceptors
              into
              [http/json-body
               (auth/jwt-authentication-interceptor)])))

kpav16:04:24

I'm fairly new to lacinia and pedestal, so I'm probably missing something obvious here...

kpav16:04:02

The jwt-authentication-interceptor looks something like this (again, slightly simplified...):

(defn jwt-authentication-interceptor
  []
  (interceptor/before
   ::jwt-authentication
   (fn [ctx]
     (authenticate ctx))))

hlship17:04:47

This is a case where you just don't use the default-service function; as documented, it's initial scaffolding but you will want to unpack it into your application's code so that you can customize what it does ... in this case, putting the auth interceptor into the /api route rather into the service's default interceptors (which puts auth before routing).

๐Ÿ‘€ 2
kpav17:04:16

Awesome thanks! I literally have the default-service function pulled up in my editor now to see how it works, I'll pull that logic out and modify as needed ๐Ÿ™‚

kpav18:04:44

Alrighty, after a bit of hacking around I got it working! Now to clean it all up... Thanks again @U04VDKC4G

hlship21:04:40

I've burned myself and many others by trying to create things that are both simple and endlessly customizable. In the end, you needed to internalize so much of the internals of such systems to be able to customize them that it was less work for everyone involved to do as we do here: use it initially, then cut-and-paste-and-modify.

kpav13:04:59

Yeah, I can imagine that being a pretty challenging issue working on an OSS project like this -- trying to maintain the delicate balance between generality and specificity