Skip to content

Commit

Permalink
Merge pull request #452 from tom-riverford/master
Browse files Browse the repository at this point in the history
Fix error when merging lists in fragments
  • Loading branch information
hlship authored Feb 2, 2024
2 parents aecfc86 + bfe8881 commit f698166
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
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
}"))))

0 comments on commit f698166

Please sign in to comment.