Class: GraphQL::Analysis::QueryComplexity::TypeComplexity

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/analysis/query_complexity.rb

Overview

Selections on an object may apply differently depending on what is actually returned by the resolve function. Find the maximum possible complexity among those combinations.

Instance Method Summary collapse

Constructor Details

#initializeTypeComplexity

Returns a new instance of TypeComplexity.



82
83
84
# File 'lib/graphql/analysis/query_complexity.rb', line 82

def initialize
  @types = Hash.new { |h, k| h[k] = 0 }
end

Instance Method Details

#max_possible_complexityObject

Return the max possible complexity for types in this selection



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/graphql/analysis/query_complexity.rb', line 87

def max_possible_complexity
  max_complexity = 0

  @types.each do |type_defn, own_complexity|
    type_complexity = @types.reduce(0) do |memo, (other_type, other_complexity)|
      if types_overlap?(type_defn, other_type)
        memo + other_complexity
      else
        memo
      end
    end

    if type_complexity > max_complexity
      max_complexity = type_complexity
    end
  end
  max_complexity
end

#merge(definitions, complexity) ⇒ Object

Store the complexity score for each of ‘types`



107
108
109
# File 'lib/graphql/analysis/query_complexity.rb', line 107

def merge(definitions, complexity)
  definitions.each { |type_defn, field_defn| @types[type_defn] += complexity }
end