Skip to content

Commit

Permalink
Merge pull request #2433 from stanishev/returning-array-of-errors
Browse files Browse the repository at this point in the history
Enable multiple execution errors for Fields defined to return a list …
  • Loading branch information
rmosolgo authored Aug 21, 2019
2 parents df46b80 + 1f68d14 commit edfc441
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/graphql/execution/execute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def continue_resolve_field(raw_value, field_type, field_ctx)
raw_value.path = field_ctx.path
query.context.errors.push(raw_value)
when Array
if !field_type.list?
if field_type.non_null?
# List type errors are handled above, this is for the case of fields returning an array of errors
list_errors = raw_value.each_with_index.select { |value, _| value.is_a?(GraphQL::ExecutionError) }
if list_errors.any?
Expand Down
6 changes: 3 additions & 3 deletions lib/graphql/execution/interpreter/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ def continue_value(path, value, field, is_non_null, ast_node)
write_execution_errors_in_response(path, [value])
HALT
elsif value.is_a?(Array) && value.any? && value.all? { |v| v.is_a?(GraphQL::ExecutionError) }
value.each do |v|
v.path ||= path
v.ast_node ||= ast_node
value.each_with_index do |error, index|
error.ast_node ||= ast_node
error.path ||= path + (field.type.list? ? [index] : [])
end
write_execution_errors_in_response(path, value)
HALT
Expand Down
32 changes: 18 additions & 14 deletions spec/graphql/execution_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,19 +295,7 @@

describe "more than one ExecutionError" do
let(:query_string) { %|{ multipleErrorsOnNonNullableField} |}
it "the errors are inserted into the errors key and the data is nil even for a NonNullable field " do

# I Think the path here is _wrong_, since this is not an array field:
# expected_result = {
# "data"=>nil,
# "errors"=>
# [{"message"=>"This is an error message for some error.",
# "locations"=>[{"line"=>1, "column"=>3}],
# "path"=>["multipleErrorsOnNonNullableField", 0]},
# {"message"=>"This is another error message for a different error.",
# "locations"=>[{"line"=>1, "column"=>3}],
# "path"=>["multipleErrorsOnNonNullableField", 1]}]
# }
it "the errors are inserted into the errors key and the data is nil even for a NonNullable field" do
expected_result = {
"data"=>nil,
"errors"=>
Expand All @@ -320,6 +308,22 @@
}
assert_equal(expected_result, result)
end
end

describe "more than one ExecutionError on a field defined to return a list" do
let(:query_string) { %|{ multipleErrorsOnNonNullableListField} |}
it "the errors are inserted into the errors key and the data is nil even for a NonNullable field" do
expected_result = {
"data"=>nil,
"errors"=>
[{"message"=>"The first error message for a field defined to return a list of strings.",
"locations"=>[{"line"=>1, "column"=>3}],
"path"=>["multipleErrorsOnNonNullableListField", 0]},
{"message"=>"The second error message for a field defined to return a list of strings.",
"locations"=>[{"line"=>1, "column"=>3}],
"path"=>["multipleErrorsOnNonNullableListField", 1]}],
}
assert_equal(expected_result, result)
end
end
end
end
1 change: 1 addition & 0 deletions spec/graphql/introspection/schema_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
{"name"=>"maybeNull"},
{"name"=>"milk"},
{"name"=>"multipleErrorsOnNonNullableField"},
{"name"=>"multipleErrorsOnNonNullableListField"},
{"name"=>"root"},
{"name"=>"searchDairy"},
{"name"=>"tracingScalar"},
Expand Down
8 changes: 8 additions & 0 deletions spec/support/dummy/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,14 @@ def multiple_errors_on_non_nullable_field
]
end

field :multiple_errors_on_non_nullable_list_field, [String], null: false
def multiple_errors_on_non_nullable_list_field
[
GraphQL::ExecutionError.new("The first error message for a field defined to return a list of strings."),
GraphQL::ExecutionError.new("The second error message for a field defined to return a list of strings.")
]
end

field :execution_error_with_options, Integer, null: true
def execution_error_with_options
GraphQL::ExecutionError.new("Permission Denied!", options: { "code" => "permission_denied" })
Expand Down

0 comments on commit edfc441

Please sign in to comment.