Skip to content

Commit 2798005

Browse files
authored
claude cleanups. (#181)
1 parent 60f11b2 commit 2798005

File tree

13 files changed

+179
-99
lines changed

13 files changed

+179
-99
lines changed

lib/graphql/stitching/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def initialize(locations: nil, supergraph: nil, composer_options: {})
2525
raise ArgumentError, "Cannot provide both locations and a supergraph."
2626
elsif supergraph && !supergraph.is_a?(Supergraph)
2727
raise ArgumentError, "Provided supergraph must be a GraphQL::Stitching::Supergraph instance."
28-
elsif supergraph && composer_options.any?
28+
elsif supergraph && !composer_options.empty?
2929
raise ArgumentError, "Cannot provide composer options with a pre-built supergraph."
3030
elsif supergraph
3131
supergraph
@@ -50,7 +50,7 @@ def execute(raw_query = nil, query: nil, variables: nil, operation_name: nil, co
5050

5151
if validate
5252
validation_errors = request.validate
53-
return error_result(request, validation_errors) if validation_errors.any?
53+
return error_result(request, validation_errors) unless validation_errors.empty?
5454
end
5555

5656
load_plan(request)

lib/graphql/stitching/composer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def build_merged_arguments(type_name, members_by_location, owner, field_name: ni
423423
memo[location] = argument.default_value
424424
end
425425

426-
if default_values_by_location.any?
426+
unless default_values_by_location.empty?
427427
kwargs[:default_value] = @default_value_merger.call(default_values_by_location, {
428428
type_name: type_name,
429429
field_name: field_name,

lib/graphql/stitching/composer/type_resolver_config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class TypeResolverConfig
88

99
class << self
1010
def extract_directive_assignments(schema, location, assignments)
11-
return EMPTY_OBJECT unless assignments && assignments.any?
11+
return EMPTY_OBJECT unless assignments && !assignments.empty?
1212

1313
assignments.each_with_object({}) do |kwargs, memo|
1414
type = kwargs[:parent_type_name] ? schema.get_type(kwargs[:parent_type_name]) : schema.query

lib/graphql/stitching/executor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def exec!(next_steps)
7979

8080
def exec_task(task)
8181
next_steps = task.load.tap(&:compact!)
82-
exec!(next_steps) if next_steps.any?
82+
exec!(next_steps) unless next_steps.empty?
8383
end
8484
end
8585
end

lib/graphql/stitching/executor/root_source.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def fetch(ops)
2121
@executor.query_count += 1
2222

2323
if result["data"]
24-
if op.path.any?
24+
unless op.path.empty?
2525
# Nested root scopes must expand their pathed origin set
2626
origin_set = op.path.reduce([@executor.data]) do |set, ns|
2727
set.flat_map { |obj| obj && obj[ns] }.tap(&:compact!)
@@ -44,32 +44,36 @@ def fetch(ops)
4444
# Builds root source documents
4545
# "query MyOperation_1($var:VarType) { rootSelections ... }"
4646
def build_document(op, operation_name = nil, operation_directives = nil)
47-
doc = String.new
48-
doc << op.operation_type
47+
doc_buffer = String.new
48+
doc_buffer << op.operation_type
4949

5050
if operation_name
51-
doc << " #{operation_name}_#{op.step}"
51+
doc_buffer << " " << operation_name << "_" << op.step.to_s
5252
end
5353

54-
if op.variables.any?
55-
variable_defs = op.variables.map { |k, v| "$#{k}:#{v}" }.join(",")
56-
doc << "(#{variable_defs})"
54+
unless op.variables.empty?
55+
doc_buffer << "("
56+
op.variables.each_with_index do |(k, v), i|
57+
doc_buffer << "," unless i.zero?
58+
doc_buffer << "$" << k << ":" << v
59+
end
60+
doc_buffer << ")"
5761
end
5862

5963
if operation_directives
60-
doc << " #{operation_directives} "
64+
doc_buffer << " " << operation_directives << " "
6165
end
6266

63-
doc << op.selections
64-
doc
67+
doc_buffer << op.selections
68+
doc_buffer
6569
end
6670

6771
# Format response errors without a document location (because it won't match the request doc),
6872
# and prepend any insertion path for the scope into error paths.
6973
def format_errors!(errors, path)
7074
errors.each do |err|
7175
err.delete("locations")
72-
err["path"].unshift(*path) if err["path"] && path.any?
76+
err["path"].unshift(*path) if err["path"] && !path.empty?
7377
end
7478
errors
7579
end

lib/graphql/stitching/executor/type_resolver_source.rb

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ def fetch(ops)
2020
origin_set.select! { _1[TypeResolver::TYPENAME_EXPORT_NODE.alias] == op.if_type }
2121
end
2222

23-
memo[op] = origin_set if origin_set.any?
23+
memo[op] = origin_set unless origin_set.empty?
2424
end
2525

26-
if origin_sets_by_operation.any?
26+
unless origin_sets_by_operation.empty?
2727
query_document, variable_names = build_document(
2828
origin_sets_by_operation,
2929
@executor.request.operation_name,
@@ -51,61 +51,76 @@ def fetch(ops)
5151
# }"
5252
def build_document(origin_sets_by_operation, operation_name = nil, operation_directives = nil)
5353
variable_defs = {}
54-
query_fields = origin_sets_by_operation.map.with_index do |(op, origin_set), batch_index|
54+
fields_buffer = String.new
55+
56+
origin_sets_by_operation.each_with_index do |(op, origin_set), batch_index|
5557
variable_defs.merge!(op.variables)
5658
resolver = @executor.request.supergraph.resolvers_by_version[op.resolver]
59+
fields_buffer << " " unless batch_index.zero?
5760

5861
if resolver.list?
59-
arguments = resolver.arguments.map.with_index do |arg, i|
62+
fields_buffer << "_" << batch_index.to_s << "_result: " << resolver.field << "("
63+
64+
resolver.arguments.each_with_index do |arg, i|
65+
fields_buffer << "," unless i.zero?
6066
if arg.key?
6167
variable_name = "_#{batch_index}_key_#{i}".freeze
6268
@variables[variable_name] = origin_set.map { arg.build(_1) }
6369
variable_defs[variable_name] = arg.to_type_signature
64-
"#{arg.name}:$#{variable_name}"
70+
fields_buffer << arg.name << ":$" << variable_name
6571
else
66-
"#{arg.name}:#{arg.value.print}"
72+
fields_buffer << arg.name << ":" << arg.value.print
6773
end
6874
end
6975

70-
"_#{batch_index}_result: #{resolver.field}(#{arguments.join(",")}) #{op.selections}"
76+
fields_buffer << ") " << op.selections
7177
else
72-
origin_set.map.with_index do |origin_obj, index|
73-
arguments = resolver.arguments.map.with_index do |arg, i|
78+
origin_set.each_with_index do |origin_obj, index|
79+
fields_buffer << " " unless index.zero?
80+
fields_buffer << "_" << batch_index.to_s << "_" << index.to_s << "_result: " << resolver.field << "("
81+
82+
resolver.arguments.each_with_index do |arg, i|
83+
fields_buffer << "," unless i.zero?
7484
if arg.key?
7585
variable_name = "_#{batch_index}_#{index}_key_#{i}".freeze
7686
@variables[variable_name] = arg.build(origin_obj)
7787
variable_defs[variable_name] = arg.to_type_signature
78-
"#{arg.name}:$#{variable_name}"
88+
fields_buffer << arg.name << ":$" << variable_name
7989
else
80-
"#{arg.name}:#{arg.value.print}"
90+
fields_buffer << arg.name << ":" << arg.value.print
8191
end
8292
end
8393

84-
"_#{batch_index}_#{index}_result: #{resolver.field}(#{arguments.join(",")}) #{op.selections}"
94+
fields_buffer << ") " << op.selections
8595
end
8696
end
8797
end
8898

89-
doc = String.new(QUERY_OP) # << resolver fulfillment always uses query
99+
doc_buffer = String.new(QUERY_OP) # << resolver fulfillment always uses query
90100

91101
if operation_name
92-
doc << " #{operation_name}"
102+
doc_buffer << " " << operation_name
93103
origin_sets_by_operation.each_key do |op|
94-
doc << "_#{op.step}"
104+
doc_buffer << "_" << op.step.to_s
95105
end
96106
end
97107

98-
if variable_defs.any?
99-
doc << "(#{variable_defs.map { |k, v| "$#{k}:#{v}" }.join(",")})"
108+
unless variable_defs.empty?
109+
doc_buffer << "("
110+
variable_defs.each_with_index do |(k, v), i|
111+
doc_buffer << "," unless i.zero?
112+
doc_buffer << "$" << k << ":" << v
113+
end
114+
doc_buffer << ")"
100115
end
101116

102117
if operation_directives
103-
doc << " #{operation_directives} "
118+
doc_buffer << " " << operation_directives << " "
104119
end
105120

106-
doc << "{ #{query_fields.join(" ")} }"
121+
doc_buffer << "{ " << fields_buffer << " }"
107122

108-
return doc, variable_defs.keys.tap do |names|
123+
return doc_buffer, variable_defs.keys.tap do |names|
109124
names.reject! { @variables.key?(_1) }
110125
end
111126
end
@@ -120,10 +135,11 @@ def merge_results!(origin_sets_by_operation, raw_result)
120135
origin_set.map.with_index { |_, index| raw_result["_#{batch_index}_#{index}_result"] }
121136
end
122137

123-
next unless results&.any?
138+
next if results.nil? || results.empty?
124139

125140
origin_set.each_with_index do |origin_obj, index|
126-
origin_obj.merge!(results[index]) if results[index]
141+
result = results[index]
142+
origin_obj.merge!(result) if result
127143
end
128144
end
129145
end
@@ -132,7 +148,7 @@ def merge_results!(origin_sets_by_operation, raw_result)
132148
def extract_errors!(origin_sets_by_operation, errors)
133149
ops = origin_sets_by_operation.keys
134150
origin_sets = origin_sets_by_operation.values
135-
pathed_errors_by_op_index_and_object_id = {}
151+
pathed_errors_by_op_index_and_object_id = Hash.new { |h, k| h[k] = {} }
136152

137153
errors_result = errors.each_with_object([]) do |err, memo|
138154
err.delete("locations")
@@ -151,8 +167,8 @@ def extract_errors!(origin_sets_by_operation, errors)
151167
end
152168

153169
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] ||= []
170+
pathed_errors_by_op_index = pathed_errors_by_op_index_and_object_id[result_alias[1].to_i]
171+
by_object_id = pathed_errors_by_op_index[origin_obj.object_id] ||= []
156172
by_object_id << err
157173
next
158174
end
@@ -162,9 +178,9 @@ def extract_errors!(origin_sets_by_operation, errors)
162178
memo << err
163179
end
164180

165-
if pathed_errors_by_op_index_and_object_id.any?
181+
unless pathed_errors_by_op_index_and_object_id.empty?
166182
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"))
183+
repath_errors!(pathed_errors_by_object_id, ops[op_index].path)
168184
errors_result.push(*pathed_errors_by_object_id.each_value)
169185
end
170186
end
@@ -180,7 +196,7 @@ def repath_errors!(pathed_errors_by_object_id, forward_path, current_path=[], ro
180196
current_path.push(forward_path.shift)
181197
scope = root[current_path.last]
182198

183-
if forward_path.any? && scope.is_a?(Array)
199+
if !forward_path.empty? && scope.is_a?(Array)
184200
scope.each_with_index do |element, index|
185201
inner_elements = element.is_a?(Array) ? element.flatten : [element]
186202
inner_elements.each do |inner_element|
@@ -190,7 +206,7 @@ def repath_errors!(pathed_errors_by_object_id, forward_path, current_path=[], ro
190206
end
191207
end
192208

193-
elsif forward_path.any?
209+
elsif !forward_path.empty?
194210
repath_errors!(pathed_errors_by_object_id, forward_path, current_path, scope)
195211

196212
elsif scope.is_a?(Array)

lib/graphql/stitching/http_executable.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def extract_multipart_form(request, document, variables)
8080

8181
return if files_by_path.none?
8282

83-
map = {}
83+
map = Hash.new { |h, k| h[k] = [] }
8484
files = files_by_path.values.tap(&:uniq!)
8585
variables_copy = variables.dup
8686

@@ -90,7 +90,6 @@ def extract_multipart_form(request, document, variables)
9090
path.each_with_index do |key, i|
9191
if i == path.length - 1
9292
file_index = files.index(copy[key]).to_s
93-
map[file_index] ||= []
9493
map[file_index] << "variables.#{path.join(".")}"
9594
copy[key] = nil
9695
elsif orig[key].object_id == copy[key].object_id

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)