Skip to content

Commit

Permalink
Fix placeholders using different parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkerlucio committed Sep 16, 2023
1 parent 6424c1a commit f953959
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## [NEXT]
- Fix placeholders using different parameters

## [2023.08.22-alpha]
- BREAKING: `::p.error/missing-output` is now converged to `::p.error/attribute-missing` (issue #149)
- BREAKING: Placeholders won't override entity data via params by default (issue #187)
Expand Down
20 changes: 18 additions & 2 deletions src/main/com/wsscode/pathom3/connect/runner.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,26 @@
::pcp/node-or
(run-or-node! env node)))

(defn ast-contains-params? [{:keys [children]}]
(some #(seq (:params %)) children))

(defn fast-placeholder-merge?
[placeholder-ast]
(not (ast-contains-params? placeholder-ast)))

(defn placeholder-merge-entity*
"Create an entity to process the placeholder demands."
[{::pcp/keys [graph] :as env}]
(zipmap (::pcp/placeholders graph) (repeat (p.ent/entity env))))
[{::pcp/keys [graph] ::keys [source-entity] :as env}]
(let [current-entity (p.ent/entity env)
index-ast (::pcp/index-ast graph)]
(reduce
(fn [out ph]
(assoc out ph
(if (fast-placeholder-merge? (get index-ast ph))
current-entity
source-entity)))
{}
(::pcp/placeholders graph))))

(defn placeholder-merge-entity
"Create an entity to process the placeholder demands."
Expand Down
25 changes: 24 additions & 1 deletion test/com/wsscode/pathom3/connect/runner_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2939,6 +2939,29 @@
{:foo "baz"
:>/path {:foo "baz"}}))

(testing "keep split when placeholder uses different parameters"
(check-all-runners
(pci/register
(pco/resolver 'param-dep-resolver
{::pco/output [:x]}
(fn [env _]
{:x (str "foo - " (:foo (pco/params env)))})))
{}
['(:x {:foo 1})
{:>/b ['(:x {:foo 2})]}]
{:x "foo - 1", :>/b {:x "foo - 2"}})

(check-all-runners
(pci/register
(pco/resolver 'param-dep-resolver
{::pco/output [:x]}
(fn [env _]
{:x (str "foo - " (:foo (pco/params env)))})))
{}
[{:>/a ['(:x {:foo 1})]}
{:>/b ['(:x {:foo 2})]}]
{:>/a {:x "foo - 1"}, :>/b {:x "foo - 2"}}))

(testing "with batch"
(is (graph-response? (pci/register
[(pco/resolver 'batch
Expand All @@ -2955,7 +2978,7 @@
:>/go {:x 10
:y 11}})))

(testing "modified data"
(testing "placeholder-data-params"
(check-all-runners
(-> (pci/register
[(pbir/single-attr-resolver :x :y #(* 2 %))])
Expand Down

0 comments on commit f953959

Please sign in to comment.