Fork me on GitHub
#pathom
<
2021-10-28
>
jgertm16:10:25

Is this a bug, or WAI?

(def env
  (pci/register
   [(pbir/static-table-resolver :module/id
                                {"foo" {:ast/definitions [{:name "bar"}
                                                          {:name "baz"}]}})
    (pco/resolver `annotate-ids
                  {::pco/input [:module/id {:ast/definitions [:name]}]
                   ::pco/output [ {:ast/definitions [:ast/id]}]}
                  (fn [env {:keys [module/id ast/definitions]}]
                    {:ast/definitions
                     (mapv
                      (fn [{:keys [name]}] {:ast/id [id name]})
                      definitions)}))]))

(try
  (p.eql/process env
                {:module/id "foo"}
                [{:ast/definitions [:ast/id]}])
  (catch Throwable t t))
;; => #error {
 :cause "Pathom can't find a path for the following elements in the query: [:ast/id] at path [:ast/definitions 0]"
 :data {:com.wsscode.pathom3.connect.planner/graph #:com.wsscode.pathom3.connect.planner{:nodes {}, :index-ast #:ast{:id {:type :prop, :dispatch-key :ast/id, :key :ast/id}}, :source-ast {:type :join, :dispatch-key :ast/definitions, :key :ast/definitions, :query [:ast/id], :children [{:type :prop, :dispatch-key :ast/id, :key :ast/id}]}, :available-data {:name {}}, :unreachable-paths #:ast{:id {}}}, :com.wsscode.pathom3.connect.planner/unreachable-paths #:ast{:id {}}, :com.wsscode.pathom3.path/path [:ast/definitions 0], :com.wsscode.pathom3.error/phase :com.wsscode.pathom3.connect.planner/plan, :com.wsscode.pathom3.error/cause :com.wsscode.pathom3.error/attribute-unreachable}
 :via
 [{:type clojure.lang.ExceptionInfo
   :message "Pathom can't find a path for the following elements in the query: [:ast/id] at path [:ast/definitions 0]"
   :data {:com.wsscode.pathom3.connect.planner/graph #:com.wsscode.pathom3.connect.planner{:nodes {}, :index-ast #:ast{:id {:type :prop, :dispatch-key :ast/id, :key :ast/id}}, :source-ast {:type :join, :dispatch-key :ast/definitions, :key :ast/definitions, :query [:ast/id], :children [{:type :prop, :dispatch-key :ast/id, :key :ast/id}]}, :available-data {:name {}}, :unreachable-paths #:ast{:id {}}}, :com.wsscode.pathom3.connect.planner/unreachable-paths #:ast{:id {}}, :com.wsscode.pathom3.path/path [:ast/definitions 0], :com.wsscode.pathom3.error/phase :com.wsscode.pathom3.connect.planner/plan, :com.wsscode.pathom3.error/cause :com.wsscode.pathom3.error/attribute-unreachable}
   :at [com.wsscode.pathom3.connect.planner$verify_plan_BANG__STAR_ invokeStatic "planner.cljc" 1490]}]
 :trace
 [[com.wsscode.pathom3.connect.planner$verify_plan_BANG__STAR_ invokeStatic "planner.cljc" 1490]
  [com.wsscode.pathom3.connect.planner$verify_plan_BANG__STAR_ invoke "planner.cljc" 1480]
  [com.wsscode.pathom3.connect.planner$verify_plan_BANG_ invokeStatic "planner.cljc" 1509]
  ...]}

wilkerlucio16:10:26

the problem is that you are trying to redefine the value of :ast/definitions, you cannot change a value once its defined (and its gets defined when you use it as the input)

wilkerlucio16:10:16

you can note your resolver isn't even called, because :ast/definitions is realized by the static table resolver

wilkerlucio16:10:21

using a different name will work

wilkerlucio16:10:33

(def env
  (pci/register
    [(pbir/static-table-resolver :module/id
       {"foo" {:ast/definitions [{:name "bar"}
                                 {:name "baz"}]}})
     (pco/resolver `annotate-ids
       {::pco/input [:module/id {:ast/definitions [:name]}]
        ::pco/output [{:ast/definitions-id [:ast/id]}]}
       (fn [env {:keys [module/id ast/definitions]}]
         {:ast/definitions-id
          (mapv
            (fn [{:keys [name]}]
              {:ast/id [id name]})
            definitions)}))]))

(comment

  (p.eql/process env
    {:module/id "foo"}
    [{:ast/definitions-id [:ast/id]}]))

jgertm16:10:51

I see, so as a general rule, for a given entity, all attributes need to be defined in the same resolver?

wilkerlucio16:10:02

I wouldn't say that, because you can always extend sideways from resolvers, but you can't get an input and replace it with modified data, once an attribute is set, it doesn't change anymore

wilkerlucio16:10:13

this is important to keep consistency across the process

wilkerlucio16:10:34

in your case you can see you ask for :ast/definition on the input, and try to set it modified on the output, which can't happen

wilkerlucio16:10:33

ideally in your case, I think :ast/definitions should have the ID already, instead of trying to compute it based on the :module/id

wilkerlucio16:10:09

(pbir/static-table-resolver :module/id
  {"foo" {:ast/definitions [{:id ["foo" "bar"]}
                            {:id ["foo" "baz"]}]}})

wilkerlucio16:10:52

so then you can have a resolver to extract the :name from the :id, altough you should use namespaced keywords there, to avoid making definitions in names that are too broad (like :id and :name)

jgertm16:10:33

Understood. Note that this is a simplified example, in my actual code I have a parser that produces the AST, without node IDs, and I figured I would write a resolver that adds them, but maybe that's too odd because node IDs have their parent's ID as a prefix.

jgertm17:10:30

@U066U8JQJ would you say that Pathom3 is actually a good fit for writing a query-based compiler?

wilkerlucio14:10:49

@U0YAC4ZMZ sorry, I don't have enough context on this problem to make a statement, can you tell more about the case?