Repository: wilkerlucio/pathom
Pull request #131 (2019-11-28 12:38:23)

Fix join with augmented env when entity is an atom


Patch:
diff --git a/src/com/wsscode/pathom/core.cljc b/src/com/wsscode/pathom/core.cljc
index 3a006743..31e14a90 100644
--- a/src/com/wsscode/pathom/core.cljc
+++ b/src/com/wsscode/pathom/core.cljc
@@ -360,14 +360,14 @@
 
 (defn join
   "Runs a parser with current sub-query. When run with an `entity` argument, that entity is set as the new environment
-   value of `::entity`, and the subquery is parsered with that new environment. When run without an `entity` it
+   value of `::entity`, and the subquery is parsed with that new environment. When run without an `entity` it
    parses the current subquery in the context of whatever entity was already in `::entity` of the env."
   ([entity {:keys [ast query] ::keys [entity-key] :as env}]
    (if (atom? entity)
-     (if (::env @entity)
+     (if-let [env' (::env @entity)]
        (do
          (swap! entity dissoc ::env)
-         (join (assoc (get @entity ::env)
+         (join (assoc env'
                  :ast ast
                  :query query
                  entity-key entity)))


Test patch:
diff --git a/test/com/wsscode/pathom/connect_test.cljc b/test/com/wsscode/pathom/connect_test.cljc
index 1b2ba8ed..fcd7979c 100644
--- a/test/com/wsscode/pathom/connect_test.cljc
+++ b/test/com/wsscode/pathom/connect_test.cljc
@@ -1259,12 +1259,23 @@
   (fn [_ {:keys [color-async]}]
     {:color-async2 (str color-async "-derived")}))
 
+(defresolver `env-data
+  {::pc/output [:env-data]}
+  (fn [{:keys [env-x]} _]
+    {:env-data env-x}))
+
 (defmutation 'call/op-async
   {::pc/output [:user/id]}
   (fn [env input]
     (go
       {:user/id 1})))
 
+(defmutation 'call/augment-env
+  {}
+  (fn [env _]
+    {:user/id 1
+     ::p/env  (assoc env :env-x 42)}))
+
 (def async-parser
   (p/async-parser {:mutate
                    pc/mutate-async
@@ -1303,7 +1314,11 @@
    (deftest test-mutate-async
      (testing "call mutation and parse response"
        (is (= (async/<!! (async-parser {} [{'(call/op-async {}) [:user/id :user/name]}]))
-              {'call/op-async {:user/id 1, :user/name "Mel"}})))
+              {'call/op-async {:user/id 1, :user/name "Mel"}}))
+
+       (testing "augmenting env"
+         (is (= (async/<!! (async-parser {} [{'(call/augment-env {}) [:env-data :user/name]}]))
+                '{call/augment-env {:env-data 42, :user/name "Mel"}}))))
 
      (testing "pathom output context"
        (is (= (async/<!! (async-parser {} ['(call/op {:pathom/context {:some/info "data"}})]))
diff --git a/test/com/wsscode/pathom/core_test.cljc b/test/com/wsscode/pathom/core_test.cljc
index 1841b80f..faffc2b2 100644
--- a/test/com/wsscode/pathom/core_test.cljc
+++ b/test/com/wsscode/pathom/core_test.cljc
@@ -227,7 +227,15 @@
                                                           ::p/env (assoc env ::x 42)} env))}
                                 {::x (fn [env] (::x env))}]}
              '[{:data [:label ::x]}])
-           {:data {:label "value" ::x 42}})))
+           {:data {:label "value" ::x 42}}))
+
+    (testing "when data item is an atom"
+      (is (= (parser {::p/reader [p/map-reader
+                                  {:data (fn [env] (p/join (atom {:label  "value"
+                                                                  ::p/env (assoc env ::x 42)}) env))}
+                                  {::x (fn [env] (::x env))}]}
+               '[{:data [:label ::x]}])
+             {:data {:label "value" ::x 42}}))))
 
   (testing "optimized final return for values"
     (is (= (parser {::p/reader [p/map-reader]