Skip to content

Commit

Permalink
Track ::pcp/expects on AND nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkerlucio committed Dec 10, 2024
1 parent acd368a commit c10ad88
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- BREAKING: Removed usage of `processor-error`, now the runner will not wrap the errors, this will generate simpler errors.
- Expose query and entity on errors during eql interface calls
- Expose planner failure details on error message
- Track `::pcp/expects` on AND nodes, it includes all expects from the node chain

## [2024.11.23-alpha]
- Fix placeholders using different parameters
Expand Down
26 changes: 19 additions & 7 deletions src/main/com/wsscode/pathom3/connect/planner.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@
; endregion

(declare add-snapshot! compute-run-graph compute-run-graph* compute-attribute-graph
optimize-graph optimize-node remove-node-expects-index-attrs find-leaf-node)
optimize-graph optimize-node remove-node-expects-index-attrs find-leaf-node
expects-from-node-chains)

; region node helpers

Expand Down Expand Up @@ -582,7 +583,7 @@
(if (= 1 (count node-ids))
(set-root-node graph (first node-ids))
(let [{and-node-id ::node-id
:as and-node} (new-node env {})]
:as and-node} (new-node env {::expects (expects-from-node-chains graph node-ids)})]
(-> graph
(include-node and-node)
(add-node-branches and-node-id ::run-and node-ids)
Expand Down Expand Up @@ -793,9 +794,6 @@
(::run-next node)
(move-run-next-to-edge target-node-id (::run-next node)))
(transfer-node-parents target-node-id node-id)
(cond->
(= ::run-and branch-type)
(update-node target-node-id nil combine-expects node))
(remove-node-edges node-id)
(remove-node node-id)
(add-snapshot! env {::snapshot-message "Simplification done"
Expand Down Expand Up @@ -935,7 +933,7 @@
; region node traversal

(>defn find-direct-node-successors
"Direct successors of node, branch nodes and run-next, in case of branch nodes the
"Direct successors of node, branch nodes, and run-next, in case of branch nodes the
branches will always come before the run-next."
[{::keys [run-next] :as node}]
[::node => (s/coll-of ::node-id)]
Expand Down Expand Up @@ -964,7 +962,7 @@

(>defn node-successors
"Find successor nodes of node-id, node-id is included in the list. This will add
branch nodes before run-next nodes. Returns a lazy sequence that traverse the graph
branch nodes before run-next nodes. Returns a lazy sequence that traverses the graph
as items are requested."
[graph node-id]
[::graph ::node-id => (s/coll-of ::node-id)]
Expand Down Expand Up @@ -1008,6 +1006,20 @@
(recur nodes (into (pop queue) (node-branches node)))))
(persistent! nodes))))

(defn expects-from-node-chain [graph node-id]
(transduce
(keep ::expects)
(completing pfsd/merge-shapes)
{}
(find-run-next-descendants graph (get-node graph node-id))))

(defn expects-from-node-chains [graph node-ids]
(transduce
(map #(expects-from-node-chain graph %))
(completing pfsd/merge-shapes)
{}
node-ids))

; endregion

; region sub-query process
Expand Down
15 changes: 13 additions & 2 deletions test/com/wsscode/pathom3/connect/planner_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
::pcp/input {}
::pcp/node-parents #{3}}
3 {::pcp/node-id 3
::pcp/expects {:a {} :b {}}
::pcp/run-and #{2 1}}}
::pcp/index-resolver->nodes {a #{1} b #{2}}
::pcp/index-attrs {:b #{2}, :a #{1}}
Expand Down Expand Up @@ -1457,6 +1458,8 @@
::pcp/node-parents #{4}
::pcp/params {::pco/optional? true}}
4 {::pcp/node-id 4
::pcp/expects {:x {}
:y {}}
::pcp/run-and #{2
3}
::pcp/run-next 1}}
Expand Down Expand Up @@ -1847,6 +1850,12 @@
::pcp/node-id 4,
::pcp/node-parents #{7}},
7 #::pcp{:node-id 7,
:expects {:a {}
:b {}
:c {}
:d {}
:e {}
:f {}}
:run-and #{1
4
2}}},
Expand Down Expand Up @@ -3793,7 +3802,9 @@
:com.wsscode.pathom3.connect.planner/run-and #{1
2},
:com.wsscode.pathom3.connect.planner/run-next 3,
:com.wsscode.pathom3.connect.planner/expects nil}},
:com.wsscode.pathom3.connect.planner/expects {:a {}
:b {}
:c {}}}},
:com.wsscode.pathom3.connect.planner/index-ast {:a {:type :prop,
:dispatch-key :a,
:key :a},
Expand Down Expand Up @@ -3850,7 +3861,7 @@
:com.wsscode.pathom3.connect.planner/run-and #{3
2},
:com.wsscode.pathom3.connect.planner/run-next 4,
:com.wsscode.pathom3.connect.planner/expects nil}},
:com.wsscode.pathom3.connect.planner/expects {:a {}, :b {}}}},
:com.wsscode.pathom3.connect.planner/index-ast {:d {:type :prop,
:dispatch-key :d,
:key :d}},
Expand Down
13 changes: 10 additions & 3 deletions test/com/wsscode/pathom3/connect/runner_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@

(testing "missed attribute comes from AND node"
(comment
(run-graph
(check-all-runners-ex
(pci/register
[(pco/resolver 'a
{::pco/output [:a]}
Expand All @@ -985,7 +985,13 @@
(fn [_ _]
{}))])
{}
[:c])))))
[:c]
{:ex/message (str
"Required attributes missing:\n"
"- Attribute :c was expected to be returned from resolver c but inputs were missing:\n"
" - Attribute :a was expected to be returned from resolver a but it failed to provide it.\n"
" - Attribute :b was expected to be returned from resolver b but it failed to provide it.")
:ex/data {::pcr/attributes-missing {:c {}}}})))))

(deftest run-graph!-final-test
(testing "map value"
Expand Down Expand Up @@ -1035,7 +1041,8 @@
{}
[:value]
{:value 2})
(is (= @spy #?(:clj 3 :cljs 1)))))
(is (or (= @spy 1)
(= @spy 3)))))

(testing "one option fail, one succeed"
(let [spy (atom 0)]
Expand Down

0 comments on commit c10ad88

Please sign in to comment.