Skip to content

Commit a322aba

Browse files
committed
Distinguish between non-null lists and non-null object fields
1 parent 4248ee6 commit a322aba

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lib/graphql/execution/interpreter/runtime.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ class Runtime
1212
module GraphQLResult
1313
# These methods are private concerns of GraphQL-Ruby,
1414
# they aren't guaranteed to continue working in the future.
15-
attr_accessor :graphql_dead, :graphql_non_null, :graphql_parent, :graphql_result_name
15+
attr_accessor :graphql_dead, :graphql_parent, :graphql_result_name
16+
# Although these are used by only one of the Result classes,
17+
# it's handy to have the methods implemented on both (even though they just return `nil`)
18+
# because it makes it easy to check if anything is assigned.
19+
# @return [nil, Array<String>]
20+
attr_accessor :graphql_non_null_field_names
21+
# @return [nil, true]
22+
attr_accessor :graphql_non_null_list_items
1623
end
1724

1825
class GraphQLResultHash < Hash
@@ -204,7 +211,7 @@ def evaluate_selection(path, result_name, field_ast_nodes_or_ast_node, scoped_co
204211
# the field's return type at this path in order
205212
# to propagate `null`
206213
if return_type.non_null?
207-
(selections_result.graphql_non_null ||= []).push(result_name)
214+
(selections_result.graphql_non_null_field_names ||= []).push(result_name)
208215
end
209216
# Set this before calling `run_with_directives`, so that the directive can have the latest path
210217
set_all_interpreter_context(nil, field_defn, nil, next_path)
@@ -336,8 +343,10 @@ def dead_result?(selection_result)
336343
def set_result(selection_result, result_name, value)
337344
if !dead_result?(selection_result)
338345
if value.nil? &&
339-
(nn = selection_result.graphql_non_null) &&
340-
(nn == true || nn.include?(result_name))
346+
( # there are two conditions under which `nil` is not allowed in the response:
347+
(selection_result.graphql_non_null_list_items) || # this value would be written into a list that doesn't allow nils
348+
((nn = selection_result.graphql_non_null_field_names) && nn.include?(result_name)) # this value would be written into a field that doesn't allow nils
349+
)
341350
# This is an invalid nil that should be propagated
342351
parent = selection_result.graphql_parent
343352
name_in_parent = selection_result.graphql_result_name
@@ -480,7 +489,7 @@ def continue_field(path, value, owner_type, field, current_type, ast_node, next_
480489
when "LIST"
481490
inner_type = current_type.of_type
482491
response_list = GraphQLResultArray.new
483-
response_list.graphql_non_null = inner_type.non_null?
492+
response_list.graphql_non_null_list_items = inner_type.non_null?
484493
response_list.graphql_parent = selection_result
485494
response_list.graphql_result_name = result_name
486495
set_result(selection_result, result_name, response_list)

0 commit comments

Comments
 (0)