Class: GraphQL::Analysis::AST::QueryComplexity::ScopedTypeComplexity
- Inherits:
-
Object
- Object
- GraphQL::Analysis::AST::QueryComplexity::ScopedTypeComplexity
- 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
-
#field_definition ⇒ Object
readonly
Returns the value of attribute field_definition.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#response_path ⇒ Object
readonly
Returns the value of attribute response_path.
Instance Method Summary collapse
-
#initialize(node, field_definition, query, response_path) ⇒ ScopedTypeComplexity
constructor
A new instance of ScopedTypeComplexity.
-
#own_complexity(child_complexity) ⇒ Object
-
#scoped_children ⇒ Hash<Hash<Class => ScopedTypeComplexity>]
This value is only calculated when asked for to avoid needless hash allocations.
-
#terminal? ⇒ Boolean
Returns true if this field has no selections, ie, it’s a scalar.
Constructor Details
#initialize(node, field_definition, query, response_path) ⇒ ScopedTypeComplexity
Returns a new instance of ScopedTypeComplexity.
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_definition ⇒ Object (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 |
#query ⇒ Object (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_path ⇒ Object (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, child_complexity) when Numeric defined_complexity + child_complexity else raise("Invalid complexity: #{defined_complexity.inspect} on #{@field_definition.name}") end end |
#scoped_children ⇒ Hash<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?).
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.
40 41 42 |
# File 'lib/graphql/analysis/ast/query_complexity.rb', line 40 def terminal? @scoped_children.nil? end |