Skip to content

Commit 60f11b2

Browse files
authored
clean up resolver version testing. (#180)
1 parent 6ca7aab commit 60f11b2

File tree

6 files changed

+44
-32
lines changed

6 files changed

+44
-32
lines changed

lib/graphql/stitching/type_resolver.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ class TypeResolver
1010
extend ArgumentsParser
1111
extend KeysParser
1212

13+
class << self
14+
# only intended for testing...
15+
def use_static_version?
16+
@use_static_version ||= false
17+
end
18+
end
19+
1320
# location name providing the resolver query.
1421
attr_reader :location
1522

@@ -47,7 +54,11 @@ def list?
4754
end
4855

4956
def version
50-
@version ||= Stitching.digest.call("#{Stitching::VERSION}/#{as_json.to_json}")
57+
@version ||= if self.class.use_static_version?
58+
[location, field, key.to_definition, type_name].join(".")
59+
else
60+
Stitching.digest.call("#{Stitching::VERSION}/#{as_json.to_json}")
61+
end
5162
end
5263

5364
def ==(other)

test/graphql/stitching/planner/plan_abstracts_test.rb

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ def setup
3838
end
3939

4040
def test_expands_interface_selections_for_target_location
41-
plan = GraphQL::Stitching::Request.new(
42-
@supergraph,
43-
%|{ buyable(id:"1") { id name price } }|,
44-
).plan
41+
plan = with_static_resolver_version do
42+
GraphQL::Stitching::Request.new(@supergraph, %|{ buyable(id:"1") { id name price } }|).plan
43+
end
4544

4645
expected_root_selection = %|
4746
{
@@ -77,11 +76,7 @@ def test_expands_interface_selections_for_target_location
7776
selections: "{ name price }",
7877
path: ["buyable"],
7978
if_type: "Product",
80-
resolver: resolver_version("Product", {
81-
location: "a",
82-
field: "products",
83-
key: "id",
84-
}),
79+
resolver: "a.products.id.Product",
8580
}
8681
end
8782

@@ -224,7 +219,9 @@ def test_plan_merged_union_types
224219

225220
@supergraph = compose_definitions({ "a" => a, "b" => b, "c" => c })
226221

227-
plan = GraphQL::Stitching::Request.new(@supergraph, document).plan
222+
plan = with_static_resolver_version do
223+
GraphQL::Stitching::Request.new(@supergraph, document).plan
224+
end
228225

229226
expected_root_selection = %|
230227
{
@@ -260,10 +257,7 @@ def test_plan_merged_union_types
260257
selections: "{ b }",
261258
path: ["fruit"],
262259
if_type: "Apple",
263-
resolver: resolver_version("Apple", {
264-
location: "b",
265-
key: "id",
266-
}),
260+
resolver: "b.apple.id.Apple",
267261
}
268262

269263
assert_keys plan.ops[2].as_json, {
@@ -272,10 +266,7 @@ def test_plan_merged_union_types
272266
selections: "{ c }",
273267
path: ["fruit"],
274268
if_type: "Apple",
275-
resolver: resolver_version("Apple", {
276-
location: "c",
277-
key: "id",
278-
}),
269+
resolver: "c.apple.id.Apple",
279270
}
280271

281272
assert_keys plan.ops[3].as_json, {
@@ -284,19 +275,7 @@ def test_plan_merged_union_types
284275
selections: "{ b }",
285276
path: ["fruit"],
286277
if_type: "Banana",
287-
resolver: resolver_version("Banana", {
288-
location: "b",
289-
key: "id",
290-
}),
278+
resolver: "b.banana.id.Banana",
291279
}
292280
end
293-
294-
private
295-
296-
def resolver_version(type_name, criteria)
297-
@supergraph.resolvers[type_name].find do |resolver|
298-
json = resolver.as_json
299-
criteria.all? { |k, v| json[k] == v }
300-
end.version
301-
end
302281
end

test/test_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ def extract_types_of_kind(schema, kind)
135135
schema.types.values.select { _1.kind.object? && !_1.graphql_name.start_with?("__") }
136136
end
137137

138+
def with_static_resolver_version
139+
GraphQL::Stitching::TypeResolver.instance_variable_set(:@use_static_version, true)
140+
yield
141+
ensure
142+
GraphQL::Stitching::TypeResolver.instance_variable_set(:@use_static_version, false)
143+
end
144+
138145
def assert_error(pattern, klass=nil)
139146
begin
140147
yield

test/test_helper_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,19 @@ def test_sorted_selection_matcher_matches_fragment_spreads
7777
refute matcher.match?(%|{ ...A ...B ...C }|)
7878
refute matcher.match?(%|{ ...A ... C }|)
7979
end
80+
81+
def test_use_static_version_is_false_by_default
82+
assert_equal false, GraphQL::Stitching::TypeResolver.use_static_version?
83+
end
84+
85+
def test_use_static_version_is_true_in_helper_block
86+
begin
87+
with_static_resolver_version do
88+
assert_equal true, GraphQL::Stitching::TypeResolver.use_static_version?
89+
raise "block interrupt"
90+
end
91+
rescue
92+
assert_equal false, GraphQL::Stitching::TypeResolver.use_static_version?
93+
end
94+
end
8095
end

0 commit comments

Comments
 (0)