New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array containing only errors is nullified #3193
Comments
Hey, thanks for the detailed writeup! I think the "bug" here is because GraphQL-Ruby technically supports returning multiple errors from fields (this isn't allowed by the spec, but GraphQL-Ruby had it first 😖 ). The code for this is here: graphql-ruby/lib/graphql/execution/interpreter/runtime.rb Lines 265 to 276 in af617b9
If you want to take a crack at improving it, you might add a I think if you skip that clause for list fields, the errors would get handled later on, as in your example of mixed error/non-error results. Want to give it a shot? |
Thanks for the info! I timeboxed exploration to an hour and it looks like this is a bit contentious given that tests already verify the current behavior for list types: https://github.com/rmosolgo/graphql-ruby/blob/master/spec/graphql/execution_error_spec.rb#L310-L324. Also, it looks like the problem may be a bit more involved than just opting list types out of the all-errors clause. Adding in the {"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]}]} Now it looks like it's only reporting a single error for the whole array field, and still nullifying the result. In the meantime, I've worked around this with an array helper in my own app code that manually maps arrays errors: def map_array_errors(context, array, path: nil)
array.each_with_index.map do |item, idx|
item = yield(item, idx) if block_given?
if item.is_a?(GraphQL::ExecutionError)
item.path = [path.presence || context[:current_path], idx].flatten
context.add_error(item)
nil
else
item
end
end
end While it's not ideal to always pipe arrays through that to match the implementation in Node, it's not the end of the world and it avoids breaking changes here. A good approach could be to put this old behavior on a setting that is disabled by default – then legacy users could opt into the old behavior, while GraphQL Ruby would follow the GraphQL spec by default. I leave it to your discretion if this is something worth adjusting in a major future release, of if it's best just to let the old implementation roll and close this out. |
This will be released in 1.13.0! #3656 |
Describe the bug
When resolving an array, the entire field is nullified when it contains only errors. I believe it should return an array with only the error positions nullified. For example, I believe this current behavior is incorrect:
I would expect this to return a valid array with two empty positions as the
testArray
data value:Versions
graphql
version: 1.11, schema built usingGraphQL::Schema.from_definition
Steps to reproduce
testArray
, return the following:Expected behavior
I would expect this to result in:
Actual behavior
While the errors are reported correctly, the
testArray
field is nullified rather than returning an array with empty positions.Additional context
Everything works correctly as long as the array contains at least one valid result in addition to other errors. For example, returning
nil
for one array position produces the expected results:This outcome is as I would expect:
The text was updated successfully, but these errors were encountered: