Skip to content

Commit

Permalink
df/load! properly handles components with a union query (#557)
Browse files Browse the repository at this point in the history
df/load! was altering component queries and making them invalid when
they were a union query at the top level component. More info can be
found in issue#556.

This commit wraps the component query in a temporary top level root
query before eliding attributes provided by the without option, which is
what was causing the problem.
  • Loading branch information
scizo authored Dec 10, 2024
1 parent c40144a commit 2193b47
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/main/com/fulcrologic/fulcro/data_fetch.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@
"Remove items from a query when the query element where the (node-predicate key) returns true. Commonly used with
a set as a predicate to elide specific well-known UI-only paths."
[query node-predicate]
(-> query eql/query->ast (elide-ast-nodes node-predicate) eql/ast->query))
(-> query
(as-> <> [{::temp-root <>}])
eql/query->ast
(elide-ast-nodes node-predicate)
eql/ast->query
(get-in [0 ::temp-root])))

(defn load-params*
"Internal function to validate and process the parameters of `load` and `load-action`."
Expand Down
16 changes: 14 additions & 2 deletions src/test/com/fulcrologic/fulcro/data_fetch_spec.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,22 @@
:ident [:parent/by-id :x]
:query [:x :z {:child (comp/get-query InitTestChild)}]})

(defsc UnionComponent [this props]
{:initial-state (fn [params] {:a/id 1 :a/name "Alice"})
:ident (fn [] (cond
(:a/id props) [:a/id (:a/id props)]
(:b/id props) [:b/id (:b/id props)]
:else nil))
:query (fn [] {:a/id [:a/id :a/name]
:b/id [:b/id :b/another-prop]})})

(def app (app/fulcro-app))

(specification "Load parameters"
(let [
query-with-params (:query (df/load-params* app :prop Person {:params {:n 1}}))
ident-query-with-params (:query (df/load-params* app [:person/by-id 1] Person {:params {:n 1}}))]
ident-query-with-params (:query (df/load-params* app [:person/by-id 1] Person {:params {:n 1}}))
union-query (:query (df/load-params* app :prop UnionComponent {:without #{}}))]
(assertions
"Accepts nil for subquery and params"
(:query (df/load-params* app [:person/by-id 1] nil {})) => [[:person/by-id 1]]
Expand All @@ -48,7 +58,9 @@
(:target (df/load-params* app :prop Person {:target [:a :b]})) => [:a :b]
"Constructs a JOIN query (with params on join and prop)"
query-with-params => `[({:prop ~(comp/get-query Person)} {:n 1})]
ident-query-with-params => `[({[:person/by-id 1] ~(comp/get-query Person)} {:n 1})]))
ident-query-with-params => `[({[:person/by-id 1] ~(comp/get-query Person)} {:n 1})]
"Properly handles components with a union query"
union-query => [{:prop (comp/get-query UnionComponent)}]))
(behavior "can focus the query"
(assertions
(:query (df/load-params* app [:item/by-id 1] Item {:focus [:name {:comments [:title]}]}))
Expand Down

0 comments on commit 2193b47

Please sign in to comment.