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(parent_type, field_definition, query, response_path) ⇒ ScopedTypeComplexity

Returns a new instance of ScopedTypeComplexity.

Parameters:

  • parent_type (Class)

    The owner of field_definition

  • field_definition (GraphQL::Field, GraphQL::Schema::Field)

    Used for getting the .complexity configuration

  • query (GraphQL::Query)

    Used for query.possible_types

  • response_path (Array<String>)

    The path to the response key for the field


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

def initialize(parent_type, field_definition, query, response_path)
  @parent_type = parent_type
  @field_definition = field_definition
  @query = query
  @response_path = response_path
  @scoped_children = nil
  @nodes = []
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

#nodesArray<GraphQL::Language::Nodes::Field> (readonly)


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

def nodes
  @nodes
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


56
57
58
# File 'lib/graphql/analysis/ast/query_complexity.rb', line 56

def own_complexity(child_complexity)
  @field_definition.calculate_complexity(query: @query, nodes: @nodes, child_complexity: child_complexity)
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:


52
53
54
# File 'lib/graphql/analysis/ast/query_complexity.rb', line 52

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)

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

def terminal?
  @scoped_children.nil?
end