Skip to content

Commit

Permalink
Make arrays only when a reference is added, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Oct 13, 2023
1 parent 80d4bee commit 3ef57c9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
11 changes: 6 additions & 5 deletions lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/schema/warden.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion spec/graphql/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 3ef57c9

Please sign in to comment.