Skip to content

Commit

Permalink
Merge pull request #204 from wilkerlucio/operations-apply-to
Browse files Browse the repository at this point in the history
Support `applyTo` on resolvers and mutations (issue #203)
  • Loading branch information
wilkerlucio authored Aug 20, 2023
2 parents 6569852 + c953f56 commit cd4a659
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Improve runner error messages, elide ` at path ` part of the message when at root
- Fix `process-one` return value when its `false` (previously it would return `nil` instead of `false`) (issue #195)
- Include failing key on smart map errors
- Support `applyTo` on resolvers and mutations (issue #203)

## [2023.01.31-alpha]
- Fix map container handling on runner (issue #176)
Expand Down
29 changes: 27 additions & 2 deletions src/main/com/wsscode/pathom3/connect/operation.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
(:require-macros
[com.wsscode.pathom3.connect.operation])))

; region declare

(declare operation-type-name)

; endregion

; region type predicates

(defn operation? [x] (satisfies? pop/IOperation x))
Expand Down Expand Up @@ -56,6 +62,17 @@

; region records

#?(:clj
(defn- operation-apply-call [op op-method args]
(let [argc (count args)]
(cond
(zero? argc) (op-method {} {})
(= 1 argc) (op-method {} (first args))
(= 2 argc) (op-method (first args) (second args))

:else
(throw (ex-info (str "Can't call " (operation-type-name op) " with more than 2 arguments.") {:args args}))))))

(defrecord Resolver [config resolve]
pop/IOperation
(-operation-config [_] config)
Expand All @@ -71,7 +88,8 @@
[clojure.lang.IFn
(invoke [_this] (resolve {} {}))
(invoke [_this input] (resolve {} input))
(invoke [_this env input] (resolve env input))]
(invoke [_this env input] (resolve env input))
(applyTo [this args] (operation-apply-call this resolve args))]

:cljs
[IFn
Expand All @@ -94,7 +112,8 @@
[clojure.lang.IFn
(invoke [_this] (mutate {} {}))
(invoke [_this input] (mutate {} input))
(invoke [_this env input] (mutate env input))]
(invoke [_this env input] (mutate env input))
(applyTo [this args] (operation-apply-call this mutate args))]

:cljs
[IFn
Expand All @@ -120,6 +139,12 @@
[::operation => ::operation-type]
(pop/-operation-type operation))

(>defn operation-type-name [operation]
[::operation => string?]
(case (operation-type operation)
::operation-type-resolver "resolver"
::operation-type-mutation "mutation"))

(defn describe-input*
[ast path outs* opt-parent?]
(doseq [{:keys [key params] :as node} (:children ast)]
Expand Down
50 changes: 50 additions & 0 deletions test/com/wsscode/pathom3/connect/operation_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@
::pco/provides {:foo {}}
::pco/requires {}}))))

(testing "user can call resolver via apply"
(let [resolver (pco/resolver 'foo {::pco/output [:foo]} (fn [env input]
(cond-> {:foo "bar"}
(seq env) (assoc :env env)
(seq input) (assoc :input input))))]
(is (= (apply resolver [])
{:foo "bar"}))

(is (= (apply resolver [{:x 1}])
{:foo "bar"
:input {:x 1}}))

(is (= (apply resolver [{:z 3} {:x 1}])
{:foo "bar"
:input {:x 1}
:env {:z 3}}))

#?(:clj
(try
(apply resolver [{:z 3} {:x 1} {:not :ok}])
(is false "Error expected to be thrown")
(catch #?(:clj Throwable :cljs :default) e
(is (= (ex-message e) "Can't call resolver with more than 2 arguments."))
(is (= (ex-data e) {:args [{:z 3} {:x 1} {:not :ok}]})))))))

(testing "validates configuration map"
(try
(pco/resolver 'foo {::pco/input #{:invalid}} (fn [_ _] {:sample "bar"}))
Expand Down Expand Up @@ -144,6 +169,31 @@
::pco/output [:foo]
::pco/provides {:foo {}}}))))

(testing "user can call mutations via apply"
(let [resolver (pco/mutation 'foo {::pco/output [:foo]} (fn [env params]
(cond-> {:foo "bar"}
(seq env) (assoc :env env)
(seq params) (assoc :params params))))]
(is (= (apply resolver [])
{:foo "bar"}))

(is (= (apply resolver [{:x 1}])
{:foo "bar"
:params {:x 1}}))

(is (= (apply resolver [{:z 3} {:x 1}])
{:foo "bar"
:params {:x 1}
:env {:z 3}}))

#?(:clj
(try
(apply resolver [{:z 3} {:x 1} {:not :ok}])
(is false "Error expected to be thrown")
(catch #?(:clj Throwable :cljs :default) e
(is (= (ex-message e) "Can't call mutation with more than 2 arguments."))
(is (= (ex-data e) {:args [{:z 3} {:x 1} {:not :ok}]})))))))

(testing "validates configuration map"
(try
(pco/mutation 'foo {::pco/input #{:invalid}} (fn [_ _] {:sample "bar"}))
Expand Down

0 comments on commit cd4a659

Please sign in to comment.