kit-clj / kit

Lightweight, modular framework for scalable web development in Clojure
https://kit-clj.github.io/
MIT License
463 stars 43 forks source link

how to access query-fn in api route? #143

Closed zengxinhui closed 1 month ago

zengxinhui commented 1 month ago

After this commit https://github.com/kit-clj/kit/commit/c2c85f82784c943559cfb0011d5e455ab6ddcf2b, opts is essentially ignored as api-routes does nothing about it.

(defmethod ig/init-key :reitit.routes/api
  [_ {:keys [base-path]
      :or   {base-path ""}
      :as   opts}]
  (fn [] [base-path route-data (api-routes opts)]))

And pages route here https://github.com/kit-clj/modules/blob/master/html/assets/src/pages.clj is still using the same code before the commit for api.clj. Also documented here: https://kit-clj.github.io/docs/integrant.html#accessing_components

(defn route-data [opts]
  (merge
   opts
...))

(defmethod ig/init-key :reitit.routes/pages
  [_ {:keys [base-path]
      :or   {base-path ""}
      :as   opts}]
  (layout/init-selmer! opts)
  [base-path (route-data opts) (page-routes opts)])

Question: what's the recommended way to access query-fy in api route?

zengxinhui commented 1 month ago

Nvm: https://github.com/kit-clj/kit/issues/119

zengxinhui commented 1 month ago

I end up pulling the query-fn from system directly:

(defn get-query-fn [] (:db.sql/query-fn integrant.repl.state/system))

yogthos commented 1 month ago

That approach is fine for dev, but the proper way to do it for prod is to provide it to the api component in system.edn

:reitit.routes/api
 {:base-path "/api"
  :env #ig/ref :system/env
  :query-fn #ig/ref :db.sql/query-fn}

and then grab it from the opts passed to api-routes, e.g:

["/:id" {:get {:parameters {:path [:map [:id integer?]]}
                    :responses  {200 {:body gifs/Gif}}
                    :handler    (partial my-handler (:query-fn opts))}}]
zengxinhui commented 1 month ago

Thank you. I give guestbook.html another read and just realize that the guestbook example was updated. Is there plan to update pages route as well?

yogthos commented 1 month ago

It should work the same way. The routes for the pages already take opts as seen in the guestbook tutorial:

(defn page-routes [opts]
  [["/" {:get (partial home opts)}]
   ["/save-message" {:post (partial guestbook/save-message! opts)}]])

So, you'd just have to update system.edn to provide the query-fn for :reitit.routes/pages and it will be available for the handlers.

zengxinhui commented 1 month ago

I meant that

  1. the template for pages is still using the pre https://github.com/kit-clj/kit/commit/c2c85f82784c943559cfb0011d5e455ab6ddcf2b style of passing opts/query-fn https://github.com/kit-clj/modules/blob/master/html/assets/src/pages.clj
    
    (defn route-data [opts]
    **(merge
    opts**
    ...

(defmethod ig/init-key :reitit.routes/pages [_ {:keys [base-path] :or {base-path ""} :as opts}] (layout/init-selmer! opts) [base-path (route-data opts) (page-routes opts)])

2. https://kit-clj.github.io/docs/integrant.html#accessing_components

(defmethod ig/init-key :reitit.routes/pages [_ {:keys [base-path ] :or {base-path ""} :as opts}] (layout/init-selmer!) [base-path (route-data opts) (page-routes opts)])

yogthos commented 1 month ago

Oh I see, just updated that to match. 👍