Skip to content

Commit ad138fd

Browse files
committed
Skip argument resolution when there are no arguments
1 parent 34e8164 commit ad138fd

File tree

1 file changed

+59
-48
lines changed

1 file changed

+59
-48
lines changed

lib/graphql/execution/interpreter/runtime.rb

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,13 @@ def evaluate_selection(result_name, field_ast_nodes_or_ast_node, owner_object, o
447447
total_args_count = field_defn.arguments(context).size
448448
if total_args_count == 0
449449
resolved_arguments = GraphQL::Execution::Interpreter::Arguments::EMPTY
450-
evaluate_selection_with_args(resolved_arguments, field_defn, ast_node, field_ast_nodes, owner_type, object, is_eager_field, result_name, selections_result, parent_object, return_type)
450+
if field_defn.extras.size == 0
451+
evaluate_selection_with_resolved_keyword_args(
452+
NO_ARGS, resolved_arguments, field_defn, ast_node, field_ast_nodes, owner_type, object, is_eager_field, result_name, selections_result, parent_object, return_type,
453+
)
454+
else
455+
evaluate_selection_with_args(resolved_arguments, field_defn, ast_node, field_ast_nodes, owner_type, object, is_eager_field, result_name, selections_result, parent_object, return_type)
456+
end
451457
else
452458
# TODO remove all arguments(...) usages?
453459
@query.arguments_cache.dataload_for(ast_node, field_defn, object) do |resolved_arguments|
@@ -504,60 +510,65 @@ def evaluate_selection_with_args(arguments, field_defn, ast_node, field_ast_node
504510
resolved_arguments.keyword_arguments
505511
end
506512

507-
st = get_current_runtime_state
508-
st.current_field = field_defn
509-
st.current_object = object
510-
st.current_arguments = resolved_arguments
511-
st.current_result_name = result_name
512-
st.current_result = selection_result
513-
# Optimize for the case that field is selected only once
514-
if field_ast_nodes.nil? || field_ast_nodes.size == 1
515-
next_selections = ast_node.selections
516-
directives = ast_node.directives
517-
else
518-
next_selections = []
519-
directives = []
520-
field_ast_nodes.each { |f|
521-
next_selections.concat(f.selections)
522-
directives.concat(f.directives)
523-
}
524-
end
525-
526-
field_result = call_method_on_directives(:resolve, object, directives) do
527-
# Actually call the field resolver and capture the result
528-
app_result = begin
529-
query.current_trace.execute_field(field: field_defn, ast_node: ast_node, query: query, object: object, arguments: kwarg_arguments) do
530-
field_defn.resolve(object, kwarg_arguments, context)
531-
end
532-
rescue GraphQL::ExecutionError => err
533-
err
534-
rescue StandardError => err
535-
begin
536-
query.handle_or_reraise(err)
537-
rescue GraphQL::ExecutionError => ex_err
538-
ex_err
539-
end
513+
evaluate_selection_with_resolved_keyword_args(kwarg_arguments, arguments, field_defn, ast_node, field_ast_nodes, owner_type, object, is_eager_field, result_name, selection_result, parent_object, return_type)
514+
end
515+
end
516+
517+
def evaluate_selection_with_resolved_keyword_args(kwarg_arguments, resolved_arguments, field_defn, ast_node, field_ast_nodes, owner_type, object, is_eager_field, result_name, selection_result, parent_object, return_type) # rubocop:disable Metrics/ParameterLists
518+
st = get_current_runtime_state
519+
st.current_field = field_defn
520+
st.current_object = object
521+
st.current_arguments = resolved_arguments
522+
st.current_result_name = result_name
523+
st.current_result = selection_result
524+
# Optimize for the case that field is selected only once
525+
if field_ast_nodes.nil? || field_ast_nodes.size == 1
526+
next_selections = ast_node.selections
527+
directives = ast_node.directives
528+
else
529+
next_selections = []
530+
directives = []
531+
field_ast_nodes.each { |f|
532+
next_selections.concat(f.selections)
533+
directives.concat(f.directives)
534+
}
535+
end
536+
537+
field_result = call_method_on_directives(:resolve, object, directives) do
538+
# Actually call the field resolver and capture the result
539+
app_result = begin
540+
query.current_trace.execute_field(field: field_defn, ast_node: ast_node, query: query, object: object, arguments: kwarg_arguments) do
541+
field_defn.resolve(object, kwarg_arguments, context)
540542
end
541-
after_lazy(app_result, owner: owner_type, field: field_defn, ast_node: ast_node, owner_object: object, arguments: resolved_arguments, result_name: result_name, result: selection_result) do |inner_result|
542-
continue_value = continue_value(inner_result, owner_type, field_defn, return_type.non_null?, ast_node, result_name, selection_result)
543-
if HALT != continue_value
544-
continue_field(continue_value, owner_type, field_defn, return_type, ast_node, next_selections, false, object, resolved_arguments, result_name, selection_result)
545-
end
543+
rescue GraphQL::ExecutionError => err
544+
err
545+
rescue StandardError => err
546+
begin
547+
query.handle_or_reraise(err)
548+
rescue GraphQL::ExecutionError => ex_err
549+
ex_err
546550
end
547551
end
548-
549-
# If this field is a root mutation field, immediately resolve
550-
# all of its child fields before moving on to the next root mutation field.
551-
# (Subselections of this mutation will still be resolved level-by-level.)
552-
if is_eager_field
553-
Interpreter::Resolve.resolve_all([field_result], @dataloader)
554-
else
555-
# Return this from `after_lazy` because it might be another lazy that needs to be resolved
556-
field_result
552+
after_lazy(app_result, owner: owner_type, field: field_defn, ast_node: ast_node, owner_object: object, arguments: resolved_arguments, result_name: result_name, result: selection_result) do |inner_result|
553+
continue_value = continue_value(inner_result, owner_type, field_defn, return_type.non_null?, ast_node, result_name, selection_result)
554+
if HALT != continue_value
555+
continue_field(continue_value, owner_type, field_defn, return_type, ast_node, next_selections, false, object, resolved_arguments, result_name, selection_result)
556+
end
557557
end
558558
end
559+
560+
# If this field is a root mutation field, immediately resolve
561+
# all of its child fields before moving on to the next root mutation field.
562+
# (Subselections of this mutation will still be resolved level-by-level.)
563+
if is_eager_field
564+
Interpreter::Resolve.resolve_all([field_result], @dataloader)
565+
else
566+
# Return this from `after_lazy` because it might be another lazy that needs to be resolved
567+
field_result
568+
end
559569
end
560570

571+
561572
def dead_result?(selection_result)
562573
selection_result.graphql_dead || ((parent = selection_result.graphql_parent) && parent.graphql_dead)
563574
end

0 commit comments

Comments
 (0)