Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when merging lists in fragments #452

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/com/walmartlabs/lacinia/executor.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
(:require
[com.walmartlabs.lacinia.internal-utils
:refer [cond-let q to-message
deep-merge deep-merge-value keepv get-nested]]
deep-merge keepv get-nested]]
[flatland.ordered.map :refer [ordered-map]]
[com.walmartlabs.lacinia.select-utils :as su]
[com.walmartlabs.lacinia.resolve-utils :refer [transform-result aggregate-results]]
Expand Down Expand Up @@ -183,7 +183,7 @@
(if (su/is-result-tuple? right-value)
(let [{:keys [alias value]} right-value]
(if (contains? left-value alias)
(update left-value alias deep-merge-value value)
(update left-value alias deep-merge value)
(assoc left-value alias value)))
(deep-merge left-value right-value)))

Expand Down Expand Up @@ -611,5 +611,3 @@
(let [{:keys [root selections]} parsed-query]
{constants/parsed-query-key parsed-query
constants/selection-key (->RootSelections root selections)}))


16 changes: 5 additions & 11 deletions src/com/walmartlabs/lacinia/internal_utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -407,27 +407,21 @@
nil
coll))

(declare deep-merge)

(defn deep-merge-value
(defn deep-merge
"Merges two maps together. Later map override earlier.
If a key is sequential, then each element in the list is merged."
[left right]
(cond
(and (map? left) (map? right))
(deep-merge left right)
(merge-with deep-merge left right)

(and (sequential? left) (sequential? right))
(mapv deep-merge left right)

(or (map? right) (sequential? right))
(or (map? right) (sequential? right))
(throw (ex-info "unable to deep merge"
{:left left
:right right}))

:else
right))

(defn deep-merge
"Merges two maps together. Later map override earlier.
If a key is sequential, then each element in the list is merged."
[left-value right-value]
(merge-with deep-merge-value left-value right-value))
15 changes: 15 additions & 0 deletions test/com/walmartlabs/lacinia/merge_selections_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,18 @@ query ($who : String) {
}
}
}"))))

(deftest merge-list-in-fragment
(is (= {:data {:luke {:name "Luke Skywalker"
:appears_in [:NEWHOPE :EMPIRE :JEDI]}}}
(q "
{
luke: human(id: \"1000\") {
name
appears_in
...props
}
}
fragment props on human {
appears_in
}"))))
Loading