Skip to content

Instantly share code, notes, and snippets.

@nivekuil
Created September 29, 2022 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nivekuil/579107f0c0e1274cb0c4e9785737f0a9 to your computer and use it in GitHub Desktop.
Save nivekuil/579107f0c0e1274cb0c4e9785737f0a9 to your computer and use it in GitHub Desktop.
basic pathom3 remote
(ns app.client.pathom-remote
(:require [com.wsscode.pathom3.plugin :as p.plugin]
[com.fulcrologic.fulcro.algorithms.transit :as fulcro-transit]
[com.wsscode.pathom3.connect.runner :as pcr]
[com.wsscode.pathom3.interface.async.eql :as p.a.eql]
[com.wsscode.pathom3.connect.foreign :as pcf]
[com.wsscode.transito :as transito]
[promesa.core :as p]
[com.fulcrologic.fulcro.algorithms.tx-processing :as txn]
[taoensso.timbre :as log]
[com.wsscode.pathom3.connect.indexes :as pci]
[com.wsscode.pathom3.error :as p.error]
[edn-query-language.core :as eql]))
#_(p.plugin/defplugin init-state-plugin
{::pcr/wrap-run-graph!
(fn [original] (fn [env] (original env)))})
(defn api-request [body]
(-> (js/fetch (str js/window.location.origin "/api")
#js {:method "POST"
:headers #js {"Content-Type" "application/transit+json"
"Accept" "application/transit+json"}
:body (transito/write-str body {:handlers (fulcro-transit/write-handlers)})})
(p/then (fn [response] (.text response)))
(p/then' (fn [response] (transito/read-str response {:handlers (fulcro-transit/read-handlers)})))))
(def pathom
(p/let [api (pcf/foreign-register api-request)]
(let [env (-> #_(p.plugin/register [init-state-plugin])
(pci/register [api])
(assoc ::p.error/lenient-mode? true)
)]
(p.a.eql/boundary-interface env))))
(def pathom-remote
{:transmit!
(fn transmit! [_ {::txn/keys [ast result-handler]}]
(let [ok-handler (fn [result]
(try
(result-handler (assoc result :status-code 200))
(catch :default e
(js/console.error e "Result handler for remote failed with an exception."))))
error-handler (fn [error-result]
(try
(result-handler (assoc error-result :status-code 500))
(catch :default e
(js/console.error e "Error handler for remote failed with an exception."))))]
(-> (p/let [boundary-interface pathom
res (boundary-interface {:pathom/ast ast})]
(ok-handler {:transaction (eql/ast->query ast)
:body (log/spy res)}))
(p/catch (fn [e]
(js/console.error "Pathom Remote Error" e)
(error-handler {:error e}))))))})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment