Fork me on GitHub
#fulcro
<
2021-09-03
>
zhuxun200:09:20

Is there a way to load a global resolver that resolves to a value (as opposed to an entity)?

Jakub Holý (HolyJak)15:09:21

No, Pathom resolvers must return a map, so {:answer 42}

zhuxun215:09:49

But that's what I mean -- :answer is not resolving to a map (e.g. {:answer {:number/id #uuid "xxxx", :number/value 42}}), it resolves to a value 42.

zhuxun215:09:41

I know that if it was resolving to a map with an ID, I could simply do (df/load! this :answer this) and modify the :query of Mycomp accordingly.

zhuxun215:09:27

But when it's resolving to a value, what should I put on the second argument of df/load!?

zhuxun200:09:00

For example, I have a global resolver that resolves :answer to 42. Now I want to load :answer to my component:

(defsc MyComp [_ {:keys [answer]}]
  {:ident (fn [] [:component/id :my-comp])
   :query [:answer]
   :componentDidMount (fn [this] (df/load! <HOW DO I WRITE THIS TO LOAD :answer?>))}
  (div (str answer)))

zhuxun201:09:29

It would be great if the second argument of df/load! (`server-property-or-ident`) can be set to nil to indicate that the query [:answer] is sent at the root level, rather than under another field (i.e., a server property or ident)

Jakub Holý (HolyJak)20:09:23

Here is the solution: https://github.com/fulcrologic/fulcro/blob/develop/src/main/com/fulcrologic/fulcro/data_fetch.cljc#L233 So this works

(pc/defresolver forty-two 
  [_ _]
  {::pc/input  #{}
   ::pc/output [:answer]}
  {:answer 42})
...
(df/load! app/app :answer nil)
This will send to Pathom the query [:answer] and get the response {:answer 42}

👍 2
zhuxun201:09:06

Right now, my workaround is to load under another global resolver that does return an entity. For example, if I have the global resolver :current-session, then I use (df/load! this :current-session this), even though :answer has nothing to do with :current-session.

zhuxun201:09:13

Not sure if there's a better way.

Tom H.04:09:02

Hey folks, does anyone have an example that uses Pathom 3 as a fulcro remote?

wilkerlucio13:09:43

if you use Pathom on the server side, you can follow the example here: https://pathom3.wsscode.com/docs/tutorials/serverless-pathom-gcf

pathom 2
wilkerlucio13:09:05

if you wanna use Pathom directly from CLJS, here is an example remote helper:

(defn pathom-remote [request]
  {: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."))))
                      key           (-> ast :children first :key)
                      entity        (some-> ast :children first :query meta :pathom/entity)
                      ident-ent     {key (conj entity key)}]
                  (-> (p/let [res (request
                                    (cond-> {:pathom/ast ast}
                                      entity (assoc :pathom/entity ident-ent)))]
                        (ok-handler {:transaction (eql/ast->query ast)
                                     :body        res}))
                      (p/catch (fn [e]
                                 (js/console.error "Pathom Remote Error" e)
                                 (error-handler {:error e}))))))})

💯 2
wilkerlucio13:09:26

request in this case is a boundary interface fn: https://pathom3.wsscode.com/docs/eql/#boundary-interface

👍 2
zeitstein17:11:16

Excuse the basic question, @U066U8JQJ, but what is the namespace p in p/let and p/catch above? Also trying to hook up a Fulcro remote with Pathom 3 from CLJS.

wilkerlucio17:11:10

the p is related to the Promesa library, its a dependency of Pathom 3, so you probably already have it around, under promesa.core

👍 1
hadils18:09:50

I am using a remote to call asynchronous JS functions in Fulcro, but I am not sure how to get a channel from a Pathom parser, in order to pass my results from my async resolvers. Any ideas?

Jakub Holý (HolyJak)19:09:25

I do not follow. What has your presumably custom remote has to do with Pathom and why do you need (core.async?) channel from pathom? Is Pathom running on backend or fronted?

hadils20:09:47

Front end. My custom remote uses pathom to parse incoming requests. I would like my resolvers to return asynchronously

hadils20:09:09

It is modeled after the remote used in the book.

Jakub Holý (HolyJak)20:09:12

Pathom has multiple parsers including -parallel and -async, at least some of them do that

Jakub Holý (HolyJak)20:09:22

https://blog.wsscode.com/pathom/v2/pathom/2.2.0/core/async.html > If you want to write parsers to run in Javascript environments, then async operations are the norm. The async parser is a version of the parser were you can return core.async channels from the readers instead of raw values. This allows for the creation of parsers that do network requests or any other async operation.

hadils00:09:13

Thanks @U0522TWDA ! I did not see the example.