From bfe8881c98003c0893ad4dbc6a15d6a253ccc21c Mon Sep 17 00:00:00 2001 From: Tom Willcocks Date: Thu, 1 Feb 2024 14:05:54 +0000 Subject: [PATCH] don't attempt to merge non-maps --- src/com/walmartlabs/lacinia/executor.clj | 6 ++---- src/com/walmartlabs/lacinia/internal_utils.clj | 16 +++++----------- .../lacinia/merge_selections_test.clj | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/com/walmartlabs/lacinia/executor.clj b/src/com/walmartlabs/lacinia/executor.clj index 57f4c123..75926183 100644 --- a/src/com/walmartlabs/lacinia/executor.clj +++ b/src/com/walmartlabs/lacinia/executor.clj @@ -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]] @@ -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))) @@ -611,5 +611,3 @@ (let [{:keys [root selections]} parsed-query] {constants/parsed-query-key parsed-query constants/selection-key (->RootSelections root selections)})) - - diff --git a/src/com/walmartlabs/lacinia/internal_utils.clj b/src/com/walmartlabs/lacinia/internal_utils.clj index 11a916db..880226be 100644 --- a/src/com/walmartlabs/lacinia/internal_utils.clj +++ b/src/com/walmartlabs/lacinia/internal_utils.clj @@ -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)) diff --git a/test/com/walmartlabs/lacinia/merge_selections_test.clj b/test/com/walmartlabs/lacinia/merge_selections_test.clj index e1cffa9c..87e6e708 100644 --- a/test/com/walmartlabs/lacinia/merge_selections_test.clj +++ b/test/com/walmartlabs/lacinia/merge_selections_test.clj @@ -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 +}"))))