Skip to content

Commit 2b77ee2

Browse files
author
Robert Mosolgo
authored
Merge pull request #3124 from grcooper/update-schema-class-possible-types
Update possible types on schema class for interfaces
2 parents 68b893a + 89915e7 commit 2b77ee2

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

lib/graphql/schema.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,16 @@ def possible_types(type = nil, context = GraphQL::Query::NullContext)
11121112
if type.kind.union?
11131113
type.possible_types(context: context)
11141114
else
1115-
own_possible_types[type.graphql_name] ||
1115+
stored_possible_types = own_possible_types[type.graphql_name]
1116+
visible_possible_types = stored_possible_types.select do |possible_type|
1117+
next true unless type.kind.interface?
1118+
next true unless possible_type.kind.object?
1119+
1120+
# Use `.graphql_name` comparison to match legacy vs class-based types.
1121+
# When we don't need to support legacy `.define` types, use `.include?(type)` instead.
1122+
possible_type.interfaces(context).any? { |interface| interface.graphql_name == type.graphql_name }
1123+
end if stored_possible_types
1124+
visible_possible_types ||
11161125
introspection_system.possible_types[type.graphql_name] ||
11171126
(
11181127
superclass.respond_to?(:possible_types) ?

lib/graphql/schema/interface.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def visible?(context)
3030

3131
# The interface is accessible if any of its possible types are accessible
3232
def accessible?(context)
33-
context.schema.possible_types(self).each do |type|
33+
context.schema.possible_types(self, context).each do |type|
3434
if context.schema.accessible?(type, context)
3535
return true
3636
end

spec/graphql/schema_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,16 @@ class Subscription < GraphQL::Schema::Object
395395
unknown_union = Class.new(GraphQL::Schema::Union) { graphql_name("Unknown") }
396396
assert_equal [], Dummy::Schema.possible_types(unknown_union)
397397
end
398+
399+
it "returns correct types for interfaces based on the context" do
400+
assert_equal [], Jazz::Schema.possible_types(Jazz::PrivateNameEntity, { private: false })
401+
assert_equal [Jazz::Ensemble], Jazz::Schema.possible_types(Jazz::PrivateNameEntity, { private: true })
402+
end
403+
404+
it "returns correct types for unions based on the context" do
405+
assert_equal [Jazz::Musician], Jazz::Schema.possible_types(Jazz::PerformingAct, { hide_ensemble: true })
406+
assert_equal [Jazz::Musician, Jazz::Ensemble], Jazz::Schema.possible_types(Jazz::PerformingAct, { hide_ensemble: false })
407+
end
398408
end
399409

400410
describe "duplicate type names" do

0 commit comments

Comments
 (0)