Skip to content

Commit bd1ea9d

Browse files
authored
lib cleanups. (#177)
1 parent 1eb008d commit bd1ea9d

9 files changed

Lines changed: 36 additions & 69 deletions

File tree

lib/graphql/stitching/executor/root_source.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def fetch(ops)
1616
@executor.request.operation_name,
1717
@executor.request.operation_directives,
1818
)
19-
query_variables = @executor.request.variables.slice(*op.variables.keys)
19+
query_variables = @executor.request.variables.slice(*op.variables.each_key)
2020
result = @executor.request.supergraph.execute_at_location(op.location, query_document, query_variables, @executor.request)
2121
@executor.query_count += 1
2222

lib/graphql/stitching/executor/type_resolver_source.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def extract_errors!(origin_sets_by_operation, errors)
165165
if pathed_errors_by_op_index_and_object_id.any?
166166
pathed_errors_by_op_index_and_object_id.each do |op_index, pathed_errors_by_object_id|
167167
repath_errors!(pathed_errors_by_object_id, ops.dig(op_index, "path"))
168-
errors_result.concat(pathed_errors_by_object_id.values)
168+
errors_result.push(*pathed_errors_by_object_id.each_value)
169169
end
170170
end
171171

lib/graphql/stitching/planner.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def perform
2424
end
2525

2626
def steps
27-
@steps_by_entrypoint.values.sort_by!(&:index)
27+
@steps_by_entrypoint.each_value.select { _1.selections.any? }.sort_by!(&:index)
2828
end
2929

3030
private
@@ -238,6 +238,7 @@ def extract_locale_selections(
238238
selection_set = extract_locale_selections(current_location, field_type, parent_index, node.selections, path, locale_variables)
239239
path.pop
240240

241+
next if selection_set.empty?
241242
locale_selections << node.merge(selections: selection_set)
242243
end
243244

@@ -250,6 +251,7 @@ def extract_locale_selections(
250251
extract_locale_selections(current_location, fragment_type, parent_index, node.selections, path, locale_variables, selection_set)
251252

252253
unless is_same_scope
254+
next if selection_set.empty?
253255
locale_selections << node.merge(selections: selection_set)
254256
requires_typename = true
255257
end
@@ -265,6 +267,7 @@ def extract_locale_selections(
265267
extract_locale_selections(current_location, fragment_type, parent_index, fragment.selections, path, locale_variables, selection_set)
266268

267269
unless is_same_scope
270+
next if selection_set.empty?
268271
locale_selections << GraphQL::Language::Nodes::InlineFragment.new(type: fragment.type, selections: selection_set)
269272
end
270273

@@ -284,7 +287,7 @@ def extract_locale_selections(
284287
remote_selections_by_location = delegate_remote_selections(parent_type, remote_selections)
285288

286289
# D) Create paths routing to new entrypoint locations via resolver queries.
287-
routes = @supergraph.route_type_to_locations(parent_type.graphql_name, current_location, remote_selections_by_location.keys)
290+
routes = @supergraph.route_type_to_locations(parent_type.graphql_name, current_location, remote_selections_by_location.each_key)
288291

289292
# E) Translate resolver pathways into new entrypoints.
290293
routes.each_value do |route|

lib/graphql/stitching/request.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ def validate
147147
@query.static_errors
148148
end
149149

150+
# @return [Boolean] is the request valid?
151+
def valid?
152+
validate.empty?
153+
end
154+
150155
# Gets and sets the query plan for the request. Assigned query plans may pull from a cache,
151156
# which is useful for redundant GraphQL documents (commonly sent by frontend clients).
152157
# ```ruby

lib/graphql/stitching/supergraph.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def initialize(schema:, fields: {}, resolvers: {}, visibility_profiles: [], exec
3838
@locations_by_type_and_field = @memoized_introspection_types.each_with_object(fields) do |(type_name, type), memo|
3939
next unless type.kind.fields?
4040

41-
memo[type_name] = type.fields.keys.each_with_object({}) do |field_name, m|
41+
memo[type_name] = type.fields.each_key.each_with_object({}) do |field_name, m|
4242
m[field_name] = [SUPERGRAPH_LOCATION]
4343
end
4444
end.freeze
@@ -75,7 +75,7 @@ def fields
7575
end
7676

7777
def locations
78-
@executables.keys.reject { _1 == SUPERGRAPH_LOCATION }
78+
@executables.each_key.reject { _1 == SUPERGRAPH_LOCATION }
7979
end
8080

8181
def memoized_schema_fields(type_name)
@@ -130,7 +130,7 @@ def fields_by_type_and_location
130130
# "Type" => ["location1", "location2", ...]
131131
def locations_by_type
132132
@locations_by_type ||= @locations_by_type_and_field.each_with_object({}) do |(type_name, fields), memo|
133-
memo[type_name] = fields.values.flatten.uniq
133+
memo[type_name] = fields.values.tap(&:flatten!).tap(&:uniq!)
134134
end
135135
end
136136

lib/graphql/stitching/supergraph/from_definition.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ def from_definition(schema, executables:)
2121
field_map = {}
2222
resolver_map = {}
2323
possible_locations = {}
24-
visibility_profiles = if (visibility_def = schema.directives[GraphQL::Stitching.visibility_directive])
25-
visibility_def.get_argument("profiles").default_value
26-
else
27-
[]
28-
end
24+
visibility_definition = schema.directives[GraphQL::Stitching.visibility_directive]
25+
visibility_profiles = visibility_definition&.get_argument("profiles")&.default_value || EMPTY_ARRAY
2926

3027
schema.types.each do |type_name, type|
3128
next if type.introspection?
@@ -75,7 +72,7 @@ def from_definition(schema, executables:)
7572
end
7673
end
7774

78-
executables = possible_locations.keys.each_with_object({}) do |location, memo|
75+
executables = possible_locations.each_key.each_with_object({}) do |location, memo|
7976
executable = executables[location] || executables[location.to_sym]
8077
if validate_executable!(location, executable)
8178
memo[location] = executable

test/graphql/stitching/integration/introspection_test.rb

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -85,60 +85,4 @@ def test_performs_type_introspection_with_other_stitching
8585

8686
assert_equal expected, result
8787
end
88-
89-
def test_handles_introspection_type_inline_fragments
90-
result = plan_and_execute(@supergraph, %|
91-
{
92-
__typename
93-
...on __Directive { __typename }
94-
...on __EnumValue { __typename }
95-
...on __InputValue { __typename }
96-
...on __Field { __typename }
97-
...on __Schema { __typename }
98-
...on __Type { __typename }
99-
product(upc: "1") {
100-
...on __Directive { __typename }
101-
...on __EnumValue { __typename }
102-
...on __InputValue { __typename }
103-
...on __Field { __typename }
104-
...on __Schema { __typename }
105-
...on __Type { __typename }
106-
}
107-
}
108-
|)
109-
110-
expected = { "data" => { "__typename" => "Query", "product" => {} } }
111-
assert_equal expected, result
112-
end
113-
114-
def test_handles_introspection_type_fragment_spreads
115-
result = plan_and_execute(@supergraph, %|
116-
fragment DirectiveAttrs on __Directive { __typename }
117-
fragment EnumValueAttrs on __EnumValue { __typename }
118-
fragment InputValueAttrs on __InputValue { __typename }
119-
fragment FieldAttrs on __Field { __typename }
120-
fragment SchemaAttrs on __Schema { __typename }
121-
fragment TypeAttrs on __Type { __typename }
122-
{
123-
__typename
124-
...DirectiveAttrs
125-
...EnumValueAttrs
126-
...InputValueAttrs
127-
...FieldAttrs
128-
...SchemaAttrs
129-
...TypeAttrs
130-
product(upc: "1") {
131-
...DirectiveAttrs
132-
...EnumValueAttrs
133-
...InputValueAttrs
134-
...FieldAttrs
135-
...SchemaAttrs
136-
...TypeAttrs
137-
}
138-
}
139-
|)
140-
141-
expected = { "data" => { "__typename" => "Query", "product" => {} } }
142-
assert_equal expected, result
143-
end
14488
end

test/graphql/stitching_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ def test_digest_gets_and_sets_hashing_implementation
1919
end
2020
end
2121

22+
def test_gets_and_sets_library_directive_names
23+
stitch_name = GraphQL::Stitching.stitch_directive
24+
visibility_name = GraphQL::Stitching.visibility_directive
25+
26+
begin
27+
GraphQL::Stitching.stitch_directive = "test"
28+
assert_equal "test", GraphQL::Stitching.stitch_directive
29+
30+
GraphQL::Stitching.visibility_directive = "test"
31+
assert_equal "test", GraphQL::Stitching.visibility_directive
32+
ensure
33+
GraphQL::Stitching.stitch_directive = stitch_name
34+
GraphQL::Stitching.visibility_directive = visibility_name
35+
end
36+
end
37+
2238
private
2339

2440
def new_type_resolver

test/test_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# ignore warnings from auto-generated GraphQL lib code.
88
Warning.ignore(/.*mismatched indentations.*/)
99
Warning.ignore(/.*lib\/graphql\/language\/nodes.rb:.*/)
10+
Warning.ignore(/Composer option `[^`]+` is deprecated.*/)
1011
end
1112

1213
require 'bundler/setup'
@@ -121,6 +122,7 @@ def plan_and_execute(supergraph, query, variables={}, raw: false)
121122
variables: variables,
122123
)
123124

125+
assert request.valid?, "Expected request to be valid: #{request.validate.map(&:message)}"
124126
plan = request.plan
125127
executor = GraphQL::Stitching::Executor.new(request)
126128
result = executor.perform(raw: raw)

0 commit comments

Comments
 (0)