Class: GraphQL::Execution::Finalize

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/execution/finalize.rb

Instance Method Summary collapse

Constructor Details

#initialize(query, data, runner) ⇒ Finalize

Returns a new instance of Finalize.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/graphql/execution/finalize.rb', line 5

def initialize(query, data, runner)
  @query = query
  @data = data
  @static_type_at = runner.static_type_at
  @runner = runner
  @current_exec_path = query.path.dup
  @current_result_path = query.path.dup
  @finalizers = runner.finalizers ? runner.finalizers[query] : {}.compare_by_identity
  @finalizers_count = 0
  @finalizers.each do |key, values|
    values.each do |key2, values2|
      case values2
      when Array
        @finalizers_count += values2.size
      else
        @finalizers_count += 1
      end
    end
  end

  query.context.errors.each do |err|
    err_path = err.path - @current_exec_path
    key = err_path.pop
    targets = [data]
    while (part = err_path.shift)
      targets.map! { |t| t[part] }
      targets.flatten!
    end

    targets.each_with_index do |target, idx|
      if target.is_a?(Hash)
        if target[key].equal?(err)
          tf = @finalizers[target] ||= {}.compare_by_identity
          tf[key] = err
          @finalizers_count += 1
        elsif (arr = target[key]).is_a?(Array)
          arr.each_with_index do |el, idx|
            if el.equal?(err)
              tf = @finalizers[arr] ||= {}.compare_by_identity
              tf[idx] = err
              @finalizers_count += 1
            end
          end
        end
      end
    end
  end
end

Instance Method Details

#runObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/graphql/execution/finalize.rb', line 54

def run
  if (selected_operation = @query.selected_operation) && @data
    if @data.is_a?(Hash)
      check_object_result(@data, @query.root_type, selected_operation.selections)
    elsif @data.is_a?(Array)
      check_list_result(@data, @query.root_type, selected_operation.selections)
    elsif @data.is_a?(Finalizer)
      dummy_data = {}
      dummy_key = "__dummy"
      @data.path = @query.path
      @data.finalize_graphql_result(@query, dummy_data, dummy_key)
      dummy_data[dummy_key]
    else
      raise ArgumentError, "Unexpected @data: #{@data.inspect}"
    end
  else
    @data
  end
end