Fork me on GitHub
#pathom
<
2022-08-17
>
markaddleman14:08:38

I’ve been thinking more and more about the ideas expressed in https://clojurians.slack.com/archives/C87NB2CFN/p1653418610858519 . I’m thinking that @wilkerlucio is right that “not everything needs a name.” By that, I think he means that not everything needs to be a Pathom attribute. On the other side, one of the key benefits of Pathom is its automatic resolver composition ability: a resolver simply declares what it needs and Pathom does the mechanical work of compositing a resolver chain to produce the necessary result. The problem that I have is my application has a lot of internal-only attributes because I want to take advantage of of auto-composition but the downside is that I would prefer to use these resolvers as functions: calling them based on logic, dynamically composing them based on business logic, and dropping the output attribute (which only adds to cognitive overload), and invoking the function with additional parameters. Smart Maps come close to what I’m looking for. Using smart maps, I can write regular a Clojure function that “declares” what it needs and Pathom will compute its dependencies. The only downside is that there are plenty of times when an input to a function is optional and, if it does not directly exist in the input, I do not want Pathom to compute it. For example, it is natural to write

(defn f[{:keys [required-param optional-param] :as smart-map}]
   (if optional-param (inc required-param) required-param))
I may not want the smart-map to compute optional-param for the purposes of this function even if there are resolvers capable of computing it. Instead, if it is already in the smart-map, I’d like the smart-map to return the value but if it does not exist, I’d like the smart-map to return nil. I can implement this in the function by writing:
(def f[{:keys [required-param] :as smart-map}]
   (let [optional-param (if (contains? :smart-map optional-param) optional-param nil)]
      (if optional-param (inc required-param) required-param))))
This is not quite as nice as the first example but it works. I’m thinking of a new Pathom operator that would combine the best of both worlds and I’d like to get this channel’s thoughts before I write it. The new operator is pco/??. It behaves similarly to the pco/? operator except that pco/?? would only return a value if it is directly available. Thus, the code above could be written as:
(defn f[{:keys [required-param (pco/?? optional-param)] :as smart-map}]
   (if optional-param (inc required-param) required-param))
I have not dug into the implementation yet but I think this operator require will require a new defn-like macro. Any thoughts on this approach? How useful do you think it would be?

wilkerlucio14:08:10

this would require changes in processing and planning, the syntax thing is the easy part, optional marks are EQL params in the query, so you may add any param you need

markaddleman15:08:27

Thanks. I’m going to experiment with this