Class: GraphQL::Execution::Interpreter::HashResponse

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

Overview

This response class handles #write by accumulating values into a Hash.

Instance Method Summary collapse

Constructor Details

#initializeHashResponse

Returns a new instance of HashResponse.


9
10
11
# File 'lib/graphql/execution/interpreter/hash_response.rb', line 9

def initialize
  @result = {}
end

Instance Method Details

#final_valueObject


13
14
15
# File 'lib/graphql/execution/interpreter/hash_response.rb', line 13

def final_value
  @result
end

#inspectObject


17
18
19
# File 'lib/graphql/execution/interpreter/hash_response.rb', line 17

def inspect
  "#<#{self.class.name} result=#{@result.inspect}>"
end

#write(path, value) ⇒ void

This method returns an undefined value.

Add value at path.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/graphql/execution/interpreter/hash_response.rb', line 23

def write(path, value)
  if path.empty?
    @result = value
  elsif (write_target = @result)
    i = 0
    prefinal_steps = path.size - 1
    # Use `while` to avoid a closure
    while i < prefinal_steps
      path_part = path[i]
      i += 1
      write_target = write_target[path_part]
    end
    path_part = path[i]
    write_target[path_part] = value
  else
    # The response is completely nulled out
  end

  nil
end