Skip to content

Commit b8db3be

Browse files
committed
refactor(Typecast) never check the value, only compare types
1 parent 466aa66 commit b8db3be

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

lib/graphql/execution/typecast.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ module GraphQL
22
module Execution
33
# GraphQL object `{value, current_type}` can be cast to `potential_type` when:
44
# - `current_type == potential_type`
5-
# - `current_type` is a union and it resolves `value` to `potential_type`
6-
# - `potential_type` is a union and `current_type` is a member
7-
# - `current_type` is an interface and it resolves `value` to `potential_type`
8-
# - `potential_type` is an interface and `current_type` implements that interface
5+
# - `current_type` is a union and it contains `potential_type`
6+
# - `potential_type` is a union and it contains `current_type`
7+
# - `current_type` is an interface and `potential_type` implements it
8+
# - `potential_type` is an interface and `current_type` implements it
99
module Typecast
1010
# While `value` is exposed by GraphQL as an instance of `current_type`,
1111
# should it _also_ be treated as an instance of `potential_type`?
@@ -17,16 +17,16 @@ module Typecast
1717
# @param [GraphQL::BaseType] can `value` be exposed using this type?
1818
# @param [GraphQL::Query::Context] the context for the current query
1919
# @return [Boolean] true if `value` be evaluated as a `potential_type`
20-
def self.compatible?(value, current_type, potential_type, query_ctx)
20+
def self.compatible?(current_type, potential_type, query_ctx)
2121
if current_type == potential_type
2222
true
2323
elsif current_type.kind.union?
24-
current_type.resolve_type(value, query_ctx) == potential_type
24+
current_type.possible_types.include?(potential_type)
2525
elsif potential_type.kind.union?
2626
potential_type.include?(current_type)
27-
elsif current_type.kind.interface?
28-
current_type.resolve_type(value, query_ctx) == potential_type
29-
elsif potential_type.kind.interface?
27+
elsif current_type.kind.interface? && potential_type.kind.object?
28+
potential_type.interfaces.include?(current_type)
29+
elsif potential_type.kind.interface? && current_type.kind.object?
3030
current_type.interfaces.include?(potential_type)
3131
else
3232
false

lib/graphql/query/serial_execution/selection_resolution.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(target, type, irep_node, execution_context)
1313

1414
def result
1515
irep_node.children.each_with_object({}) do |(name, irep_node), memo|
16-
if GraphQL::Execution::DirectiveChecks.include?(irep_node, execution_context.query) && applies_to_type?(irep_node, type, target)
16+
if GraphQL::Execution::DirectiveChecks.include?(irep_node, execution_context.query) && applies_to_type?(irep_node, type)
1717
field_result = execution_context.strategy.field_resolution.new(
1818
irep_node,
1919
type,
@@ -27,9 +27,9 @@ def result
2727

2828
private
2929

30-
def applies_to_type?(irep_node, current_type, value)
30+
def applies_to_type?(irep_node, current_type)
3131
irep_node.definitions.any? { |potential_type, field_defn|
32-
GraphQL::Execution::Typecast.compatible?(value, current_type, potential_type, execution_context.query.context)
32+
GraphQL::Execution::Typecast.compatible?(current_type, potential_type, execution_context.query.context)
3333
}
3434
end
3535
end

spec/graphql/execution/typecast_spec.rb

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,44 @@
88
let(:context) { GraphQL::Query::Context.new(query: OpenStruct.new(schema: schema), values: nil) }
99

1010
def compatible?(*args)
11-
GraphQL::Execution::Typecast.compatible?(*args)
12-
end
11+
GraphQL::Execution::Typecast.compatible?(*args)
12+
end
13+
1314
it "resolves correctly when both types are the same" do
14-
assert compatible?(milk_value, MilkType, MilkType, context)
15-
assert !compatible?(milk_value, MilkType, CheeseType, context)
15+
assert compatible?(MilkType, MilkType, context)
16+
17+
assert !compatible?(MilkType, CheeseType, context)
1618
end
1719

1820
it "resolves a union type to a matching member" do
19-
assert compatible?(milk_value, DairyProductUnion, MilkType, context)
20-
assert compatible?(cheese_value, DairyProductUnion, CheeseType, context)
21+
assert compatible?(DairyProductUnion, MilkType, context)
22+
assert compatible?(DairyProductUnion, CheeseType, context)
2123

22-
assert !compatible?(cheese_value, DairyProductUnion, MilkType, context)
23-
assert !compatible?(nil, DairyProductUnion, MilkType, context)
24+
assert !compatible?(DairyProductUnion, GraphQL::INT_TYPE, context)
25+
assert !compatible?(DairyProductUnion, HoneyType, context)
2426
end
2527

2628
it "resolves correcty when potential type is UnionType and current type is a member of that union" do
27-
assert compatible?(milk_value, MilkType, DairyProductUnion, context)
28-
assert compatible?(cheese_value, CheeseType, DairyProductUnion, context)
29+
assert compatible?(MilkType, DairyProductUnion, context)
30+
assert compatible?(CheeseType, DairyProductUnion, context)
2931

30-
# assert !compatible?(nil, CheeseType, DairyProductUnion, context)
31-
# assert !compatible?(cheese_value, MilkType, DairyProductUnion, context)
32+
assert !compatible?(QueryType, DairyProductUnion, context)
33+
assert !compatible?(EdibleInterface, DairyProductUnion, context)
3234
end
3335

3436
it "resolves an object type to one of its interfaces" do
35-
assert compatible?(cheese_value, CheeseType, EdibleInterface, context)
36-
assert compatible?(milk_value, MilkType, EdibleInterface, context)
37+
assert compatible?(CheeseType, EdibleInterface, context)
38+
assert compatible?(MilkType, EdibleInterface, context)
3739

38-
# assert !compatible?(nil, MilkType, EdibleInterface, context)
39-
# assert !compatible?(milk_value, CheeseType, EdibleInterface, context)
40+
assert !compatible?(QueryType, EdibleInterface, context)
41+
assert !compatible?(LocalProductInterface, EdibleInterface, context)
4042
end
4143

4244
it "resolves an interface to a matching member" do
43-
assert compatible?(cheese_value, EdibleInterface, CheeseType, context)
44-
assert compatible?(milk_value, EdibleInterface, MilkType, context)
45+
assert compatible?(EdibleInterface, CheeseType, context)
46+
assert compatible?(EdibleInterface, MilkType, context)
4547

46-
assert !compatible?(nil, EdibleInterface, MilkType, context)
47-
assert !compatible?(cheese_value, EdibleInterface, MilkType, context)
48+
assert !compatible?(EdibleInterface, GraphQL::STRING_TYPE, context)
49+
assert !compatible?(EdibleInterface, DairyProductInputType, context)
4850
end
4951
end

0 commit comments

Comments
 (0)