Repository: wilkerlucio/pathom
Pull request #134 (2019-12-05 01:56:09)

Make mutation ast available on connect mutations


Patch:
diff --git a/src/com/wsscode/pathom/connect.cljc b/src/com/wsscode/pathom/connect.cljc
index d0a2052d..3bdfcf47 100644
--- a/src/com/wsscode/pathom/connect.cljc
+++ b/src/com/wsscode/pathom/connect.cljc
@@ -1497,7 +1497,7 @@
 (defn mutate
   "Sync mutate function to integrate connect mutations to pathom parser."
   [{::keys [indexes mutate-dispatch mutation-join-globals]
-    :keys  [query]
+    :keys  [query ast]
     :or    {mutation-join-globals []}
     :as    env} sym' {:keys [pathom/context] :as input}]
   (if-let [{::keys [sym]} (get-in indexes [::index-mutations sym'])]
@@ -1506,14 +1506,14 @@
                       res (cond-> res (and context (map? res)) (merge context))]
                   (if (and query (map? res))
                     (merge (select-keys res mutation-join-globals)
-                           (p/join (atom res) env))
+                           (p/join (atom res) (assoc env ::mutation-ast ast)))
                     res))})
     (throw (ex-info "Mutation not found" {:mutation sym'}))))
 
 (defn mutate-async
   "Async mutate function to integrate connect mutations to pathom parser."
   [{::keys [indexes mutate-dispatch mutation-join-globals]
-    :keys  [query]
+    :keys  [query ast]
     :or    {mutation-join-globals []}
     :as    env} sym' {:keys [pathom/context] :as input}]
   (if-let [{::keys [sym]} (get-in indexes [::index-mutations sym'])]
@@ -1523,7 +1523,7 @@
                         res (cond-> res (and context (map? res)) (merge context))]
                     (if query
                       (merge (select-keys res mutation-join-globals)
-                             (<? (p/join (atom res) env)))
+                             (<? (p/join (atom res) (assoc env ::mutation-ast ast))))
                       res)))})
     (throw (ex-info "Mutation not found" {:mutation sym'}))))
 


Test patch:
diff --git a/test/com/wsscode/pathom/connect_test.cljc b/test/com/wsscode/pathom/connect_test.cljc
index 2bbcee83..46e96a30 100644
--- a/test/com/wsscode/pathom/connect_test.cljc
+++ b/test/com/wsscode/pathom/connect_test.cljc
@@ -15,6 +15,8 @@
   #?(:clj
      (:import (clojure.lang ExceptionInfo))))
 
+(declare quick-parser)
+
 (def base-indexes (atom {}))
 
 (defmulti resolver-fn pc/resolver-dispatch)
@@ -1322,7 +1324,21 @@
 
      (testing "pathom output context"
        (is (= (async/<!! (async-parser {} ['(call/op {:pathom/context {:some/info "data"}})]))
-              '{call/op {:user/id 1 :some/info "data"}})))))
+              '{call/op {:user/id 1 :some/info "data"}})))
+
+     (testing "mutation ast is available in the env"
+       (is (= (quick-parser
+                {::pc/register [(pc/resolver 'my-example
+                                  {::pc/output [:x]}
+                                  (fn [env _]
+                                    {:x (-> env ::pc/mutation-ast :key)}))
+
+                                (pc/mutation 'change
+                                  {}
+                                  (fn [_ _]
+                                    {:y 46}))]}
+                '[{(change {}) [:x]}])
+              '{change {:x change}})))))
 
 (def index
   #::pc{:index-io {#{:customer/id}                                         #:customer{:external-ids  {}
@@ -3284,33 +3300,6 @@
        (reset! quick-parser-trace* @trace)
        res)))
 
-(comment
-  (quick-parser {::p/env       {:counter c}
-                 ::pc/register [(pc/resolver 'branch-resolver
-                                  {::pc/output [:guitar/id :camera/id]}
-                                  (fn [env _]
-                                    (Thread/sleep 100)
-                                    {:camera/id 123}))
-
-                                (pc/resolver 'guitar-name
-                                  {::pc/input  #{:guitar/id}
-                                   ::pc/output [:guitar/name :guitar/factory-name]}
-                                  (fn [_ {:keys [guitar/id]}]
-                                    (Thread/sleep 100)
-                                    {:guitar/name (str id " name")
-                                     :guitar/factory-name "Guitar Factory"}))
-
-                                (pc/resolver 'camera-name
-                                  {::pc/input  #{:camera/id}
-                                   ::pc/output [:camera/name]}
-                                  (fn [_ {:keys [camera/id]}]
-                                    (Thread/sleep 100)
-                                    {:camera/name (str id " name")}))
-
-                                (pc/alias-resolver :guitar/name :thing/name)
-                                (pc/alias-resolver :camera/name :thing/name)]}
-    '[:b :z]))
-
 #?(:clj
    (deftest test-parallel-parser-with-connect
      (testing "skip resolver if value is resolved"