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


85
86
87
# File 'lib/graphql/analysis/query_complexity.rb', line 85

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


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

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(type_defn, complexity) ⇒ Object

Store the complexity for the branch on type_defn. Later we will see if this is the max complexity among branches.


111
112
113
# File 'lib/graphql/analysis/query_complexity.rb', line 111

def merge(type_defn, complexity)
  @types[type_defn] += complexity
end