Skip to content

Commit dae4bea

Browse files
committed
cleanups.
1 parent b10fcdc commit dae4bea

2 files changed

Lines changed: 63 additions & 25 deletions

File tree

lib/graphql/stitching/executor/type_resolver_source.rb

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,26 @@ def build_document(origin_sets_by_operation, operation_name = nil, operation_dir
8989
doc = String.new(QUERY_OP) # << resolver fulfillment always uses query
9090

9191
if operation_name
92-
doc << " #{operation_name}"
92+
doc << " " << operation_name
9393
origin_sets_by_operation.each_key do |op|
94-
doc << "_#{op.step}"
94+
doc << "_" << op.step.to_s
9595
end
9696
end
9797

9898
if variable_defs.any?
99-
doc << "(#{variable_defs.map { |k, v| "$#{k}:#{v}" }.join(",")})"
99+
doc << "("
100+
variable_defs.each_with_index do |(k, v), i|
101+
doc << "," unless i.zero?
102+
doc << "$" << k << ":" << v
103+
end
104+
doc << ")"
100105
end
101106

102107
if operation_directives
103-
doc << " #{operation_directives} "
108+
doc << " " << operation_directives << " "
104109
end
105110

106-
doc << "{ #{query_fields.join(" ")} }"
111+
doc << "{ " << query_fields.join(" ") << " }"
107112

108113
return doc, variable_defs.keys.tap do |names|
109114
names.reject! { @variables.key?(_1) }
@@ -132,7 +137,7 @@ def merge_results!(origin_sets_by_operation, raw_result)
132137
def extract_errors!(origin_sets_by_operation, errors)
133138
ops = origin_sets_by_operation.keys
134139
origin_sets = origin_sets_by_operation.values
135-
pathed_errors_by_op_index_and_object_id = {}
140+
pathed_errors_by_op_index_and_object_id = Hash.new { |h, k| h[k] = {} }
136141

137142
errors_result = errors.each_with_object([]) do |err, memo|
138143
err.delete("locations")
@@ -151,8 +156,8 @@ def extract_errors!(origin_sets_by_operation, errors)
151156
end
152157

153158
if origin_obj
154-
by_op_index = pathed_errors_by_op_index_and_object_id[result_alias[1].to_i] ||= {}
155-
by_object_id = by_op_index[origin_obj.object_id] ||= []
159+
pathed_errors_by_op_index = pathed_errors_by_op_index_and_object_id[result_alias[1].to_i]
160+
by_object_id = pathed_errors_by_op_index[origin_obj.object_id] ||= []
156161
by_object_id << err
157162
next
158163
end
@@ -162,9 +167,9 @@ def extract_errors!(origin_sets_by_operation, errors)
162167
memo << err
163168
end
164169

165-
if pathed_errors_by_op_index_and_object_id.any?
170+
unless pathed_errors_by_op_index_and_object_id.empty?
166171
pathed_errors_by_op_index_and_object_id.each do |op_index, pathed_errors_by_object_id|
167-
repath_errors!(pathed_errors_by_object_id, ops.dig(op_index, "path"))
172+
repath_errors!(pathed_errors_by_object_id, ops[op_index].path)
168173
errors_result.push(*pathed_errors_by_object_id.each_value)
169174
end
170175
end
@@ -180,7 +185,7 @@ def repath_errors!(pathed_errors_by_object_id, forward_path, current_path=[], ro
180185
current_path.push(forward_path.shift)
181186
scope = root[current_path.last]
182187

183-
if forward_path.any? && scope.is_a?(Array)
188+
if !forward_path.empty? && scope.is_a?(Array)
184189
scope.each_with_index do |element, index|
185190
inner_elements = element.is_a?(Array) ? element.flatten : [element]
186191
inner_elements.each do |inner_element|
@@ -190,7 +195,7 @@ def repath_errors!(pathed_errors_by_object_id, forward_path, current_path=[], ro
190195
end
191196
end
192197

193-
elsif forward_path.any?
198+
elsif !forward_path.empty?
194199
repath_errors!(pathed_errors_by_object_id, forward_path, current_path, scope)
195200

196201
elsif scope.is_a?(Array)

lib/graphql/stitching/plan.rb

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,42 @@
22

33
module GraphQL
44
module Stitching
5-
# Immutable-ish structures representing a query plan.
5+
# Immutable structures representing a query plan.
66
# May serialize to/from JSON.
77
class Plan
8-
Op = Struct.new(
9-
:step,
10-
:after,
11-
:location,
12-
:operation_type,
13-
:selections,
14-
:variables,
15-
:path,
16-
:if_type,
17-
:resolver,
18-
keyword_init: true
19-
) do
8+
class Op
9+
attr_reader :step
10+
attr_reader :after
11+
attr_reader :location
12+
attr_reader :operation_type
13+
attr_reader :selections
14+
attr_reader :variables
15+
attr_reader :path
16+
attr_reader :if_type
17+
attr_reader :resolver
18+
19+
def initialize(
20+
step:,
21+
after:,
22+
location:,
23+
operation_type:,
24+
selections:,
25+
variables: nil,
26+
path: nil,
27+
if_type: nil,
28+
resolver: nil
29+
)
30+
@step = step
31+
@after = after
32+
@location = location
33+
@operation_type = operation_type
34+
@selections = selections
35+
@variables = variables
36+
@path = path
37+
@if_type = if_type
38+
@resolver = resolver
39+
end
40+
2041
def as_json
2142
{
2243
step: step,
@@ -30,6 +51,18 @@ def as_json
3051
resolver: resolver
3152
}.tap(&:compact!)
3253
end
54+
55+
def ==(other)
56+
step == other.step &&
57+
after == other.after &&
58+
location == other.location &&
59+
operation_type == other.operation_type &&
60+
selections == other.selections &&
61+
variables == other.variables &&
62+
path == other.path &&
63+
if_type == other.if_type &&
64+
resolver == other.resolver
65+
end
3366
end
3467

3568
class << self

0 commit comments

Comments
 (0)