Class: GraphQL::Analysis::AST::QueryComplexity::ScopedTypeComplexity

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

Constant Summary collapse

HASH_CHILDREN =

A single proc for #scoped_children hashes. Use this to avoid repeated allocations, since the lexical binding isn’t important.

->(h, k) { h[k] = {} }

Instance Method Summary collapse

Constructor Details

#initialize(node, field_definition, query) ⇒ ScopedTypeComplexity

Returns a new instance of ScopedTypeComplexity.

Parameters:



27
28
29
30
31
32
# File 'lib/graphql/analysis/ast/query_complexity.rb', line 27

def initialize(node, field_definition, query)
  @field_definition = field_definition
  @query = query
  @node = node
  @scoped_children = nil
end

Instance Method Details

#own_complexity(child_complexity) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/graphql/analysis/ast/query_complexity.rb', line 48

def own_complexity(child_complexity)
  defined_complexity = @field_definition.complexity
  case defined_complexity
  when Proc
    arguments = @query.arguments_for(@node, @field_definition)
    defined_complexity.call(@query.context, arguments, child_complexity)
  when Numeric
    defined_complexity + child_complexity
  else
    raise("Invalid complexity: #{defined_complexity.inspect} on #{@field_definition.name}")
  end
end

#scoped_childrenHash<Hash<Class => ScopedTypeComplexity>]

This value is only calculated when asked for to avoid needless hash allocations. Also, if it’s never asked for, we determine that this scope complexity is a scalar field (#terminal?).

Returns:



44
45
46
# File 'lib/graphql/analysis/ast/query_complexity.rb', line 44

def scoped_children
  @scoped_children ||= Hash.new(&HASH_CHILDREN)
end

#terminal?Boolean

Returns true if this field has no selections, ie, it’s a scalar. We need a quick way to check whether we should continue traversing.

Returns:

  • (Boolean)


36
37
38
# File 'lib/graphql/analysis/ast/query_complexity.rb', line 36

def terminal?
  @scoped_children.nil?
end