Skip to content

Commit a6c069a

Browse files
committed
Apply some extracted components in Execution to SerialExecution
1 parent 397e804 commit a6c069a

File tree

6 files changed

+21
-151
lines changed

6 files changed

+21
-151
lines changed

lib/graphql/execution/deferred_execution.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def resolve_selections(scope, thread, outer_frame)
271271
scope,
272272
outer_frame.value,
273273
outer_frame.type,
274-
outer_frame.node,
274+
outer_frame.node.selections,
275275
)
276276

277277
resolved_selections = merged_selections.each_with_object({}) do |ast_selection, memo|

lib/graphql/execution/selection_on_type.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module SelectionOnType
88
# - Check if fragments apply to `value`
99
# - dedup any same-named fields
1010
# @return [Array<GraphQL::Language::Nodes::Field] flattened selections
11-
def flatten(exec_context, value, type_defn, ast_node)
12-
merged_selections = flatten_selections(exec_context, value, type_defn, ast_node)
11+
def flatten(exec_context, value, type_defn, ast_selection_nodes)
12+
merged_selections = flatten_selections(exec_context, value, type_defn, ast_selection_nodes)
1313
merged_selections.values
1414
end
1515

@@ -18,10 +18,8 @@ def flatten(exec_context, value, type_defn, ast_node)
1818
module_function
1919
# Flatten selections on `ast_node`
2020
# @return [Hash<String, GraphQL::Language::Nodes::Field>] name-field pairs for flattened selections
21-
def flatten_selections(exec_context, value, type_defn, ast_node)
22-
selections = ast_node.selections
23-
24-
merged_selections = selections.reduce({}) do |result, ast_selection|
21+
def flatten_selections(exec_context, value, type_defn, ast_selection_nodes)
22+
merged_selections = ast_selection_nodes.reduce({}) do |result, ast_selection|
2523
flattened_selections = flatten_selection(exec_context, value, type_defn, ast_selection)
2624
flattened_selections.each do |name, selection|
2725
result[name] = if result.key?(name) && selection.selections.any?
@@ -75,7 +73,7 @@ def flatten_fragment(exec_context, value, type_defn, ast_fragment)
7573
end
7674

7775
if can_apply
78-
flatten_selections(exec_context, value, type_defn, ast_fragment)
76+
flatten_selections(exec_context, value, type_defn, ast_fragment.selections)
7977
else
8078
{}
8179
end

lib/graphql/query.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
require "graphql/query/arguments"
22
require "graphql/query/context"
3-
require "graphql/query/directive_resolution"
43
require "graphql/query/executor"
54
require "graphql/query/literal_input"
65
require "graphql/query/serial_execution"
7-
require "graphql/query/type_resolver"
86
require "graphql/query/variables"
97
require "graphql/query/input_validation_result"
108
require "graphql/query/variable_validation_error"

lib/graphql/query/serial_execution/selection_resolution.rb

Lines changed: 15 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -12,123 +12,30 @@ def initialize(target, type, selections, execution_context)
1212
end
1313

1414
def result
15-
flatten_and_merge_selections(selections)
16-
.values
17-
.reduce({}) { |result, ast_node|
18-
result.merge(resolve_field(ast_node))
19-
}
15+
flattened_selections = GraphQL::Execution::SelectionOnType.flatten(execution_context, target, type, selections)
16+
flattened_selections.reduce({}) do |result, ast_node|
17+
field_result = if GraphQL::Execution::DirectiveChecks.skip?(ast_node, execution_context.query)
18+
{}
19+
else
20+
execution_context.strategy.field_resolution.new(
21+
ast_node,
22+
type,
23+
target,
24+
execution_context
25+
).result
26+
end
27+
28+
result.merge(field_result)
29+
end
2030
rescue GraphQL::InvalidNullError => err
2131
err.parent_error? || execution_context.add_error(err)
2232
nil
2333
end
2434

2535
private
2636

27-
def flatten_selection(ast_node)
28-
strategy_method = STRATEGIES[ast_node.class]
29-
send(strategy_method, ast_node)
30-
end
31-
32-
STRATEGIES = {
33-
GraphQL::Language::Nodes::Field => :flatten_field,
34-
GraphQL::Language::Nodes::InlineFragment => :flatten_inline_fragment,
35-
GraphQL::Language::Nodes::FragmentSpread => :flatten_fragment_spread,
36-
}
37-
38-
def flatten_field(ast_node)
39-
result_name = ast_node.alias || ast_node.name
40-
{ result_name => ast_node }
41-
end
42-
43-
def flatten_inline_fragment(ast_node)
44-
if GraphQL::Execution::DirectiveChecks.skip?(ast_node, execution_context.query)
45-
{}
46-
else
47-
flatten_fragment(ast_node)
48-
end
49-
end
50-
51-
def flatten_fragment_spread(ast_node)
52-
if GraphQL::Execution::DirectiveChecks.skip?(ast_node, execution_context.query)
53-
{}
54-
else
55-
ast_fragment_defn = execution_context.get_fragment(ast_node.name)
56-
flatten_fragment(ast_fragment_defn)
57-
end
58-
end
59-
60-
def flatten_fragment(ast_fragment)
61-
if fragment_type_can_apply?(ast_fragment)
62-
flatten_and_merge_selections(ast_fragment.selections)
63-
else
64-
{}
65-
end
66-
end
67-
68-
def fragment_type_can_apply?(ast_fragment)
69-
if ast_fragment.type.nil?
70-
true
71-
else
72-
child_type = execution_context.get_type(ast_fragment.type)
73-
resolved_type = GraphQL::Query::TypeResolver.new(target, child_type, type, execution_context.query.context).type
74-
!resolved_type.nil?
75-
end
76-
end
77-
78-
def merge_fields(field1, field2)
79-
field_type = execution_context.get_field(type, field2.name).type.unwrap
80-
81-
if field_type.kind.fields?
82-
# create a new ast field node merging selections from each field.
83-
# Because of static validation, we can assume that name, alias,
84-
# arguments, and directives are exactly the same for fields 1 and 2.
85-
GraphQL::Language::Nodes::Field.new(
86-
name: field2.name,
87-
alias: field2.alias,
88-
arguments: field2.arguments,
89-
directives: field2.directives,
90-
selections: field1.selections + field2.selections
91-
)
92-
else
93-
field2
94-
end
95-
end
96-
9737
def resolve_field(ast_node)
98-
if GraphQL::Execution::DirectiveChecks.skip?(ast_node, execution_context.query)
99-
{}
100-
else
101-
execution_context.strategy.field_resolution.new(
102-
ast_node,
103-
type,
104-
target,
105-
execution_context
106-
).result
107-
end
108-
end
109-
110-
def merge_into_result(memo, selection)
111-
name = if selection.respond_to?(:alias)
112-
selection.alias || selection.name
113-
else
114-
selection.name
115-
end
116-
117-
memo[name] = if memo.key?(name)
118-
merge_fields(memo[name], selection)
119-
else
120-
selection
121-
end
122-
end
12338

124-
def flatten_and_merge_selections(selections)
125-
selections.reduce({}) do |result, ast_node|
126-
flattened_selections = flatten_selection(ast_node)
127-
flattened_selections.each do |name, selection|
128-
merge_into_result(result, selection)
129-
end
130-
result
131-
end
13239
end
13340
end
13441
end

lib/graphql/query/type_resolver.rb

Lines changed: 0 additions & 25 deletions
This file was deleted.

spec/graphql/query/type_resolver_spec.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)