Class: GraphQL::Analysis::ReducerState

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reducer, query) ⇒ ReducerState

Returns a new instance of ReducerState



8
9
10
11
12
# File 'lib/graphql/analysis/reducer_state.rb', line 8

def initialize(reducer, query)
  @reducer = reducer
  @memo = initialize_reducer(reducer, query)
  @errors = []
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors



6
7
8
# File 'lib/graphql/analysis/reducer_state.rb', line 6

def errors
  @errors
end

#memoObject

Returns the value of attribute memo



6
7
8
# File 'lib/graphql/analysis/reducer_state.rb', line 6

def memo
  @memo
end

#reducerObject (readonly)

Returns the value of attribute reducer



5
6
7
# File 'lib/graphql/analysis/reducer_state.rb', line 5

def reducer
  @reducer
end

Instance Method Details

#call(visit_type, irep_node) ⇒ Object



14
15
16
17
18
# File 'lib/graphql/analysis/reducer_state.rb', line 14

def call(visit_type, irep_node)
  @memo = @reducer.call(@memo, visit_type, irep_node)
rescue AnalysisError => err
  @errors << err
end

#finalize_reducerAny

Respond with any errors, if found. Otherwise, if the reducer accepts final_value, send it the last memo value. Otherwise, use the last value from the traversal.

Returns:

  • (Any)

    final memo value



24
25
26
27
28
29
30
31
32
# File 'lib/graphql/analysis/reducer_state.rb', line 24

def finalize_reducer
  if @errors.any?
    @errors
  elsif reducer.respond_to?(:final_value)
    reducer.final_value(@memo)
  else
    @memo
  end
end