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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, field_definition, query, response_path) ⇒ ScopedTypeComplexity

Returns a new instance of ScopedTypeComplexity.

Parameters:



30
31
32
33
34
35
36
# File 'lib/graphql/analysis/ast/query_complexity.rb', line 30

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

Instance Attribute Details

#field_definitionObject (readonly)

Returns the value of attribute field_definition.



24
25
26
# File 'lib/graphql/analysis/ast/query_complexity.rb', line 24

def field_definition
  @field_definition
end

#queryObject (readonly)

Returns the value of attribute query.



24
25
26
# File 'lib/graphql/analysis/ast/query_complexity.rb', line 24

def query
  @query
end

#response_pathObject (readonly)

Returns the value of attribute response_path.



24
25
26
# File 'lib/graphql/analysis/ast/query_complexity.rb', line 24

def response_path
  @response_path
end

Instance Method Details

#own_complexity(child_complexity) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/graphql/analysis/ast/query_complexity.rb', line 52

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.keyword_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:



48
49
50
# File 'lib/graphql/analysis/ast/query_complexity.rb', line 48

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)


40
41
42
# File 'lib/graphql/analysis/ast/query_complexity.rb', line 40

def terminal?
  @scoped_children.nil?
end