-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
…ercion fails
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,5 +65,19 @@ def to_s | |
end | ||
|
||
alias :inspect :to_s | ||
|
||
# Coerce `input_value` according to this type's `coerce` method. | ||
# Raise an error if the value becomes nil. | ||
# @param [Object] Incoming query value | ||
# @return [Object] Coerced value for query execution | ||
def coerce!(input_value) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
rmosolgo
Author
Owner
|
||
coerced_value = coerce(input_value) | ||
|
||
if coerced_value.nil? | ||
raise GraphQL::ExecutionError.new("Couldn't coerce #{input_value.inspect} to #{self.unwrap.name}") | ||
end | ||
|
||
coerced_value | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,10 +30,11 @@ | |
|} | ||
let(:debug) { false } | ||
let(:operation_name) { nil } | ||
let(:query_variables) { {"cheeseId" => 2} } | ||
let(:query) { GraphQL::Query.new( | ||
DummySchema, | ||
query_string, | ||
variables: {"cheeseId" => 2}, | ||
variables: query_variables, | ||
debug: debug, | ||
operation_name: operation_name, | ||
)} | ||
|
@@ -194,4 +195,28 @@ | |
end | ||
end | ||
end | ||
|
||
describe "query variables" do | ||
let(:query_string) {%| | ||
query getCheese($cheeseId: Int!){ | ||
cheese(id: $cheeseId) { flavor } | ||
} | ||
|} | ||
|
||
describe "when they can be coerced" do | ||
let(:query_variables) { {"cheeseId" => 2.0} } | ||
|
||
it "coerces them on the way in" do | ||
assert("Gouda", result["data"]["cheese"]["flavor"]) | ||
end | ||
end | ||
|
||
describe "when they can't be coerced" do | ||
let(:query_variables) { {"cheeseId" => "2"} } | ||
This comment has been minimized.
Sorry, something went wrong.
lifeiscontent
|
||
|
||
it "raises an error" do | ||
assert(result["errors"][0]["message"].include?(%{Couldn't coerce "2" to Int})) | ||
end | ||
end | ||
end | ||
end |
It seems like the
coerce
methods should be the ones that raise, that way it can provide an appropriate error message. Also,nil
might be an acceptable value, unless it is a non-null type.