Fork me on GitHub
#pathom
<
2022-12-01
>
kendall.buchanan19:12:12

Fun question: Is there such a notion as dispatching between resolvers based on the value of a key? For example, say you have :my/favorite-color which could have both :blue or :red values. In one resolver, you include :my/favorite-color as an input, but you only want to use this resolver if the value is :blue. Why? Because one resolver might require another input, like :my/favorite-season, in which case, you only want to access that key for :blue values, not :red values. (Perhaps for performance reasons—`:my/favorite-season` could be irrelevant for values of :red).

wilkerlucio21:12:16

no, that's not an notion for Pathom itself, planning doesn't consider anything on the data, this is important to allow proper caching, and also, this could make the whole processing much more complex to handle (from the. Pathom side). that said, there are ways to accomplish something like this, its totally valid to do a pathom request from inside a resolver, so here is one way you can do it:

(ns com.wsscode.pathom3.demos.dispatch-by-value
  (:require
    [com.wsscode.pathom3.connect.indexes :as pci]
    [com.wsscode.pathom3.connect.operation :as pco]
    [com.wsscode.pathom3.entity-tree :as p.ent]
    [com.wsscode.pathom3.interface.eql :as p.eql]))

(pco/defresolver favorite-season
  [{:keys [my/id]}]
  {:my/favorite-season (str "season for " id)})

(pco/defresolver favorite-thing
  [env {:keys [my/favorite-color]}]
  {:my/favorite-thing
   (case favorite-color
     :blue
     (let [season (p.eql/process-one env (p.ent/entity env) :my/favorite-season)]
       (str "with season " season))

     :red
     "go with red")})

(def env
  (pci/register
    [favorite-season
     favorite-thing]))

(comment
  (p.eql/process env
    {:folks
     [{:my/id             1
       :my/favorite-color :blue}
      {:my/id             2
       :my/favorite-color :red}]}
    [{:folks [:my/favorite-thing]}]))

👏 1
kendall.buchanan21:12:58

Mmm, gotcha. I’ve often wondered about the advisability of invoking a Pathom query from within a resolver, and so far been able to avoid it.

kendall.buchanan21:12:19

But I can understand the value of steering clear of dispatching on values.