diff --git a/lib/graphql/schema.rb b/lib/graphql/schema.rb index 6f2fbde557..564d7c43aa 100644 --- a/lib/graphql/schema.rb +++ b/lib/graphql/schema.rb @@ -536,16 +536,17 @@ def dataloader_class attr_writer :dataloader_class def references_to(to_type = nil, from: nil) - @own_references_to ||= Hash.new { |h, k| h[k] = [] } + @own_references_to ||= {} if to_type if !to_type.is_a?(String) to_type = to_type.graphql_name end if from - @own_references_to[to_type] << from + refs = @own_references_to[to_type] ||= [] + refs << from else - get_references_to(to_type) + get_references_to(to_type) || EMPTY_ARRAY end else # `@own_references_to` can be quite large for big schemas, @@ -1407,8 +1408,8 @@ module SubclassGetReferencesTo def get_references_to(type_name) own_refs = @own_references_to[type_name] inherited_refs = superclass.references_to(type_name) - if inherited_refs.any? - if own_refs.any? + if inherited_refs&.any? + if own_refs&.any? own_refs + inherited_refs else inherited_refs diff --git a/lib/graphql/schema/warden.rb b/lib/graphql/schema/warden.rb index 5a3df1f5ff..624fadf278 100644 --- a/lib/graphql/schema/warden.rb +++ b/lib/graphql/schema/warden.rb @@ -106,7 +106,7 @@ def initialize(context:, schema:) @types = @visible_types = @reachable_types = @visible_parent_fields = @visible_possible_types = @visible_fields = @visible_arguments = @visible_enum_arrays = @visible_enum_values = @visible_interfaces = @type_visibility = @type_memberships = - @visible_and_reachable_type = @unions = @unfiltered_interfaces = @references_to = + @visible_and_reachable_type = @unions = @unfiltered_interfaces = @reachable_type_set = nil end diff --git a/spec/graphql/schema_spec.rb b/spec/graphql/schema_spec.rb index 635aa97753..1925912123 100644 --- a/spec/graphql/schema_spec.rb +++ b/spec/graphql/schema_spec.rb @@ -164,7 +164,6 @@ class CustomSubscriptions < GraphQL::Subscriptions::ActionCableSubscriptions METHODS_TO_CACHE = { types: 1, union_memberships: 1, - references_to: 1, possible_types: 5, # The number of types with fields accessed in the query }