Skip to content

Commit c3e3919

Browse files
committed
Merge run_eager and run_partials_eager
1 parent 09df6e3 commit c3e3919

File tree

4 files changed

+55
-75
lines changed

4 files changed

+55
-75
lines changed

lib/graphql/execution/interpreter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def run_partials(schema, partials, context:)
160160
runtime = Runtime.new(query: partial, lazies_at_depth: lazies_at_depth)
161161
partial.context.namespace(:interpreter_runtime)[:runtime] = runtime
162162
partial.current_trace.execute_query(query: partial) do
163-
runtime.run_partial_eager
163+
runtime.run_eager
164164
end
165165
}
166166
end

lib/graphql/execution/interpreter/runtime.rb

+47-69
Original file line numberDiff line numberDiff line change
@@ -64,89 +64,67 @@ def inspect
6464
"#<#{self.class.name} response=#{@response.inspect}>"
6565
end
6666

67-
# This _begins_ the execution. Some deferred work
68-
# might be stored up in lazies.
6967
# @return [void]
7068
def run_eager
71-
root_operation = query.selected_operation
72-
root_op_type = root_operation.operation_type || "query"
73-
root_type = schema.root_type_for_operation(root_op_type)
74-
runtime_object = root_type.wrap(query.root_value, context)
75-
runtime_object = schema.sync_lazy(runtime_object)
76-
is_eager = root_op_type == "mutation"
77-
@response = GraphQLResultHash.new(nil, root_type, runtime_object, nil, false, root_operation.selections, is_eager, root_operation, nil, nil)
78-
st = get_current_runtime_state
79-
st.current_result = @response
80-
81-
if runtime_object.nil?
82-
# Root .authorized? returned false.
83-
@response = nil
69+
root_type = query.root_type
70+
case query
71+
when GraphQL::Query
72+
ast_node = query.selected_operation
73+
selections = ast_node.selections
74+
object = query.root_value
75+
is_eager = ast_node.operation_type == "mutation"
76+
when GraphQL::Query::Partial
77+
ast_node = query.ast_nodes.first
78+
selections = query.ast_nodes.map(&:selections).inject(&:+)
79+
object = partial.object
80+
is_eager = false
8481
else
85-
call_method_on_directives(:resolve, runtime_object, root_operation.directives) do # execute query level directives
86-
each_gathered_selections(@response) do |selections, is_selection_array, ordered_result_keys|
87-
@response.ordered_result_keys ||= ordered_result_keys
88-
if is_selection_array
89-
selection_response = GraphQLResultHash.new(nil, root_type, runtime_object, nil, false, selections, is_eager, root_operation, nil, nil)
90-
selection_response.ordered_result_keys = ordered_result_keys
91-
final_response = @response
92-
else
93-
selection_response = @response
94-
final_response = nil
95-
end
96-
97-
@dataloader.append_job {
98-
evaluate_selections(
99-
selections,
100-
selection_response,
101-
final_response,
102-
nil,
103-
)
104-
}
105-
end
106-
end
82+
raise ArgumentError, "Unexpected Runnable, can't execute: #{query.class} (#{query.inspect})"
10783
end
108-
nil
109-
end
110-
111-
# @return [void]
112-
def run_partial_eager
113-
# `query` is actually a GraphQL::Query::Partial
114-
partial = query
115-
root_type = partial.root_type
116-
object = partial.object
117-
selections = partial.ast_nodes.map(&:selections).inject(&:+)
11884
runtime_state = get_current_runtime_state
11985
case root_type.kind.name
12086
when "OBJECT"
12187
object_proxy = root_type.wrap(object, context)
12288
object_proxy = schema.sync_lazy(object_proxy)
123-
@response = GraphQLResultHash.new(nil, root_type, object_proxy, nil, false, selections, false, partial.ast_nodes.first, nil, nil)
124-
each_gathered_selections(@response) do |selections, is_selection_array, ordered_result_keys|
125-
@response.ordered_result_keys ||= ordered_result_keys
126-
if is_selection_array == true
127-
raise "This isn't supported yet"
128-
end
89+
if object_proxy.nil?
90+
@response = nil
91+
else
92+
@response = GraphQLResultHash.new(nil, root_type, object_proxy, nil, false, selections, is_eager, ast_node, nil, nil)
93+
runtime_state.current_result = @response
94+
call_method_on_directives(:resolve, object, ast_node.directives) do
95+
each_gathered_selections(@response) do |selections, is_selection_array, ordered_result_keys|
96+
@response.ordered_result_keys ||= ordered_result_keys
97+
if is_selection_array
98+
selection_response = GraphQLResultHash.new(nil, root_type, object_proxy, nil, false, selections, is_eager, ast_node, nil, nil)
99+
selection_response.ordered_result_keys = ordered_result_keys
100+
final_response = @response
101+
else
102+
selection_response = @response
103+
final_response = nil
104+
end
129105

130-
@dataloader.append_job {
131-
evaluate_selections(
132-
selections,
133-
@response,
134-
nil,
135-
runtime_state,
136-
)
137-
}
106+
@dataloader.append_job {
107+
evaluate_selections(
108+
selections,
109+
selection_response,
110+
final_response,
111+
nil,
112+
)
113+
}
114+
end
115+
end
138116
end
139117
when "LIST"
140118
inner_type = root_type.unwrap
141119
case inner_type.kind.name
142120
when "SCALAR", "ENUM"
143-
parent_object_proxy = partial.parent_type.wrap(object, context)
121+
parent_object_proxy = query.parent_type.wrap(object, context)
144122
parent_object_proxy = schema.sync_lazy(parent_object_proxy)
145-
field_node = partial.ast_nodes.first
123+
field_node = query.ast_nodes.first
146124
result_name = field_node.alias || field_node.name
147-
@response = GraphQLResultHash.new(nil, partial.parent_type, parent_object_proxy, nil, false, nil, false, field_node, nil, nil)
125+
@response = GraphQLResultHash.new(nil, query.parent_type, parent_object_proxy, nil, false, nil, false, field_node, nil, nil)
148126
@response.ordered_result_keys = [result_name]
149-
evaluate_selection(result_name, partial.ast_nodes, @response)
127+
evaluate_selection(result_name, query.ast_nodes, @response)
150128
else
151129
@response = GraphQLResultArray.new(nil, root_type, nil, nil, false, selections, false, field_node, nil, nil)
152130
idx = nil
@@ -165,23 +143,23 @@ def run_partial_eager
165143
end
166144
end
167145
when "SCALAR", "ENUM"
168-
parent_type = partial.parent_type
146+
parent_type = query.parent_type
169147
# TODO what if not object type? Maybe returns a lazy here.
170148
parent_object_type, object = resolve_type(parent_type, object)
171149
parent_object_proxy = parent_object_type.wrap(object, context)
172150
parent_object_proxy = schema.sync_lazy(parent_object_proxy)
173-
field_node = partial.ast_nodes.first
151+
field_node = query.ast_nodes.first
174152
result_name = field_node.alias || field_node.name
175153
@response = GraphQLResultHash.new(nil, parent_object_type, parent_object_proxy, nil, false, selections, false, field_node, nil, nil)
176154
@response.ordered_result_keys = [result_name]
177155
@dataloader.append_job do
178-
evaluate_selection(result_name, partial.ast_nodes, @response)
156+
evaluate_selection(result_name, query.ast_nodes, @response)
179157
end
180158
when "UNION", "INTERFACE"
181159
resolved_type, _resolved_obj = resolve_type(root_type, object) # TODO lazy, errors
182160
object_proxy = resolved_type.wrap(object, context)
183161
object_proxy = schema.sync_lazy(object_proxy)
184-
@response = GraphQLResultHash.new(nil, resolved_type, object_proxy, nil, false, selections, false, partial.ast_nodes.first, nil, nil)
162+
@response = GraphQLResultHash.new(nil, resolved_type, object_proxy, nil, false, selections, false, query.ast_nodes.first, nil, nil)
185163
each_gathered_selections(@response) do |selections, is_selection_array, ordered_result_keys|
186164
@response.ordered_result_keys ||= ordered_result_keys
187165
if is_selection_array == true

lib/graphql/query.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,10 @@ def subscription_update?
233233
# @return [GraphQL::Execution::Lookahead]
234234
def lookahead
235235
@lookahead ||= begin
236-
ast_node = selected_operation
237-
if ast_node.nil?
236+
if selected_operation.nil?
238237
GraphQL::Execution::Lookahead::NULL_LOOKAHEAD
239238
else
240-
root_type = root_type_for_operation(ast_node.operation_type)
241-
GraphQL::Execution::Lookahead.new(query: self, root_type: root_type, ast_nodes: [ast_node])
239+
GraphQL::Execution::Lookahead.new(query: self, root_type: root_type, ast_nodes: [selected_operation])
242240
end
243241
end
244242
end
@@ -395,6 +393,10 @@ def root_type_for_operation(op_type)
395393
end
396394
end
397395

396+
def root_type
397+
root_type_for_operation(selected_operation.operation_type)
398+
end
399+
398400
def types
399401
@visibility_profile || warden.visibility_profile
400402
end

lib/graphql/query/partial.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def initialize(path:, object:, query:, context: nil)
3232
@result_values = nil
3333
@result = nil
3434
selections = [@query.selected_operation]
35-
type = @query.root_type_for_operation(@query.selected_operation.operation_type)
35+
type = @query.root_type
3636
parent_type = nil
3737
field_defn = nil
3838
@path.each do |name_in_doc|

0 commit comments

Comments
 (0)