Skip to content

Commit

Permalink
throw an exception on input coercion failures.
Browse files Browse the repository at this point in the history
Previously, the generated error containers were passed to resolvables in
place of the actual parameter values. This should now be fixed.

Fixes alumbra/alumbra#17.
  • Loading branch information
Yannick Scherer committed Mar 22, 2017
1 parent 01f95ac commit 4225f27
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/alumbra/claro/coercion.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
(data/error
(format "could not coerce value to '%s': %s"
type-name
(pr-str value))))))
(pr-str value))
{:value value
:type-name type-name
:throwable (format "[%s] %s"
(.getName (class t))
(.getMessage t))}))))

(defn- wrap-coercer-exception
[f type-name]
Expand Down Expand Up @@ -52,9 +57,15 @@

(defn coerce-value
[{:keys [schema scalars]} type-name value]
(-> (or (get-in scalars [type-name :decode])
(let [{:keys [type->kind]} schema]
(if (= (type->kind type-name) :enum)
csk/->kebab-case-keyword
#(default-coercer type-name %))))
(call-coercer type-name value)))
(let [result (-> (or (get-in scalars [type-name :decode])
(let [{:keys [type->kind]} schema]
(if (= (type->kind type-name) :enum)
csk/->kebab-case-keyword
#(default-coercer type-name %))))
(call-coercer type-name value))]
(if (data/error? result)
(throw
(ex-info
(data/error-message result)
(data/error-data result)))
result)))
25 changes: 25 additions & 0 deletions test/alumbra/claro/coercion_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[properties :as prop]
[generators :as gen]
[clojure-test :refer [defspec]]]
[clojure.test :refer :all]
[camel-snake-kebab.core :as csk]
[claro.data :as data]
[alumbra.claro.fixtures :as fix]))
Expand Down Expand Up @@ -173,3 +174,27 @@
(execute!)
(get-in [:data "result"]))]
(output-valid? result)))))

(deftest t-input-coercion-exception
(let [decode #(throw (ex-info "oops." {:value %}))
encode str
execute! (fix/execute-fn
{:schema schema
:query {:as-id (->Identity nil (constantly true))}
:scalars {"ID" {:encode encode, :decode decode}}})]
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"could not coerce value to 'ID'"
(execute! "{ asId(v: 10) }")))))

(deftest t-output-coercion-exception
(let [decode str
encode #(throw (ex-info "oops." {:value %}))
execute! (fix/execute-fn
{:schema schema
:query {:as-id (->Identity nil (constantly true))}
:scalars {"ID" {:encode encode, :decode decode}}})
result (is (execute! "{ asId(v: 10) }"))]
(is (= {"asId" nil} (:data result)))
(is (= "could not coerce value to 'ID': \"10\""
(-> result :errors first :message)))))

0 comments on commit 4225f27

Please sign in to comment.