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


91
92
93
# File 'lib/graphql/analysis/query_complexity.rb', line 91

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


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/graphql/analysis/query_complexity.rb', line 96

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


116
117
118
# File 'lib/graphql/analysis/query_complexity.rb', line 116

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