pathom

Jakub Holý (HolyJak) 2023-06-27T21:07:52.274289Z

Hello! I have an entity that has one of two possible attributes. How do I avoid Pathom complaining about missing-attribute for the other one? I tried setting it to ::pco/unknown-value but that does not seem to help. (Pathom3 2023.01.31-alpha). This is my resolver:

(pco/defresolver recipe-line-item-resolver [_ {:recipe-line-item/keys [id]}]
  {::pco/input  [:recipe-line-item/id]
   ::pco/output [:recipe-line-item/qty
                 :recipe-line-item/uom
                 :recipe-line-item/ingredient [:ingredient/id]
                 :recipe-line-item/sub-recipe [:recipe/id]]}
  (when-let [res (get-in @pretend-server-database [:recipe-line-item/id id])]
    (merge {:recipe-line-item/ingredient ::pco/unknown-value ; TODO This does not seem to help, FE is still getting missing-attribute error
            :recipe-line-item/sub-recipe ::pco/unknown-value}
           res)))
And the https://github.com/fulcro-community/fulcro-cookbook/blob/d26df693f48fb4e3e19f055e8f4628ed4c2dcfb1/cookbook/dynamic-recursion/dynamic_recursion_cards.cljs. Any hints are most appreciated! 🙏

wilkerlucio 2023-06-27T21:09:01.711699Z

still going to check, at a glance, I see you missing some {} around the nested joins (for ingredients and sub-recipe), maybe it isn't related, but something to fix up 🙂

🙀 1
wilkerlucio 2023-06-27T21:10:58.145719Z

missing attribute means you made a request for that attribute, and it coudln't be fulfilled, filling it with ::pco/unknown-value has the same effect of not returning it

wilkerlucio 2023-06-27T21:11:08.141959Z

so if your request requires it, its gonna be an error

wilkerlucio 2023-06-27T21:11:40.229539Z

one way to avoid the error, is to mark it as optional, at the query request (surround it with (pco/? :recipe-line-item/ingredient)

Jakub Holý (HolyJak) 2023-06-27T21:17:19.771149Z

I see, thank you for the explanation! Is there any other reasonable alternative? The query comes from a fulcro Component, whose defsc macro tries to check the query and does not handle pco/? I could turn it into the opaque fn form but then I lose the benefits of the validation…

wilkerlucio 2023-06-27T21:45:46.844769Z

what pco/? does is simply add an EQL query parameter, you can add the parameter by hand (if the fulcro macro supports it)

🙌 1
wilkerlucio 2023-06-27T21:48:03.415549Z

but is that preventing your query from executing? are you using strict or lenient mode on Pathom 3?

Ben Wadsworth 2023-06-27T22:48:20.105609Z

Your resolver is going to return nil if the when-let is not satisfied which will result a missing attribute as well. You could, instead of returning ::pco/unknown-value return nil which may still convey what you want. From the documentation, I would believe that ::pco/unknown-value should really be used when you have another resolver which could figure out that value as its usage will affect the query that is eventually ran...potentially... if you do have another way to resolve that attribute...

Ben Wadsworth 2023-06-27T22:49:24.724669Z

So... ::pco/unknown-value is a tool to tell Pathom what to do, not a value you resolve

👍 1
Jakub Holý (HolyJak) 2023-06-29T14:53:15.808309Z

> setting it’s value to nil, this way it will not be considered missing, but will have no value outputted to the client Awesome, I did not know/understood this actually was how it works. Will give it a try!

pithyless 2023-06-28T08:16:17.497649Z

I've struggled with similar issues before. I think the mismatch is trying to do entity polymorphism without forcing a union query on the client (in the OP we would need to define some key to differentiate between recipes with ingredients vs recipes with sub-recipes). I've solved this before with lenient mode and a middleware plugin that removes certain missing-attribute from the output. Not particularly happy with this solution and would also like to hear if there are better options. I don't think pco/? is a panacea, because in a highly polymorphic entity everything would essentially have to be marked as optional. I think what's missing is some form of defining multi-specs (where certain keys can exist together or a different set of keys must exist). I guess that is another option: mark most things as optional and then do another check on the result via e.g. clojure.spec or malli that certain multi-spec rules hold true.

Jakub Holý (HolyJak) 2023-06-28T08:46:56.273149Z

No, it was not preventing the query from executing, it just logged unnecessary and scary errors. Fulcro only supports query params at the very top level, ie on the whole query/mutation. This is a rare corner case so the workaround of using :query (fn ...) with (pco/? ...) and losing validation on this one component is acceptable.

Jakub Holý (HolyJak) 2023-06-28T08:49:43.053779Z

> the mismatch is trying to do entity polymorphism without forcing a union query on the client 💯 I could certainly have used a union query instead. I just wanted to keep the frontend simpler, without including an extra concept, as this is aimed at teaching people (a https://fulcro-community.github.io/fulcro-cookbook/dynamic-recursion/dynamic-recursion.html). The current solution is good enough, I think. Thank you all!

wilkerlucio 2023-06-28T13:01:28.312589Z

@holyjak the idea @bwadsworth told is algo a good option if you like to signal that some attribute doesn't have a value, setting it's value to nil, this way it will not be considered missing, but will have no value outputted to the client (and Pathom will not try to fetch it in some other way, in case there is another path)

🙏 1