Skip to content

Commit

Permalink
allow variables to be missing if nullable, adjust error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Scherer committed May 15, 2017
1 parent c992358 commit 5c3bb91
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/alumbra/canonical/literal_value.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
(throw
(IllegalArgumentException.
(format
"null value not allowed for expected type '%s'."
"Null value not allowed for expected type '%s'."
(type-shorthand t#)))))
(when-not ~predicate
(throw
(IllegalArgumentException.
(format
"value does not match expected type '%s': %s"
"Value does not match expected type '%s': %s"
(type-shorthand t#)
v#)))))))

Expand Down
25 changes: 16 additions & 9 deletions src/alumbra/canonical/variables.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@
{:keys [alumbra/variable-name
alumbra/default-value
alumbra/type]}]
(let [v (get variables variable-name ::none)
type (as-type-description type)]
(if (= v ::none)
(if default-value
(resolve-value opts type default-value)
::none)
(resolve-literal-value opts type v))))
(try
(let [v (get variables variable-name ::none)
type (as-type-description type)]
(if (= v ::none)
(if default-value
(resolve-value opts type default-value)
(resolve-literal-value opts type nil))
(resolve-literal-value opts type v)))
(catch IllegalArgumentException t
(throw
(IllegalArgumentException.
(format "Variable '$%s': %s"
variable-name
(.getMessage t))
t)))))

(defn resolve-variables
[opts {:keys [alumbra/variables]}]
(->> (for [{:keys [alumbra/variable-name] :as variable} variables
:let [variable-value (resolve-variable opts variable)]
:when (not= variable-value ::none)]
:let [variable-value (resolve-variable opts variable)]]
[variable-name variable-value])
(into {})
(assoc opts :variables)))
6 changes: 3 additions & 3 deletions test/alumbra/canonicalizer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@
(let [query "query ($q: CatQuery!) { randomCat(q: $q) { name } }"]
(is (thrown-with-msg?
IllegalArgumentException
#"null value not allowed"
#"Null value not allowed"
(canonicalize query {})))
(is (thrown-with-msg?
IllegalArgumentException
#"null value not allowed"
#"Null value not allowed"
(canonicalize query {"q" nil})))))))

(deftest t-variable-type-mismatch
Expand Down Expand Up @@ -178,7 +178,7 @@
(testing "nullability mismatch."
(is (thrown-with-msg?
IllegalArgumentException
#"null value not allowed for expected type 'CatQuery!'"
#"Null value not allowed for expected type 'CatQuery!'"
(canonicalize
"query ($q: CatQuery!) {
randomCat(q: $q) { name }
Expand Down

0 comments on commit 5c3bb91

Please sign in to comment.