Class: Rails::GraphQL::Collectors::HashCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/graphql/collectors/hash_collector.rb

Overview

GraphQL Hash Collector

This collector helps building a JSON response using the hash approach, where the value is kept as an hash and later turn into a string

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ HashCollector

Returns a new instance of HashCollector.



11
12
13
14
15
# File 'lib/rails/graphql/collectors/hash_collector.rb', line 11

def initialize(request)
  @request = request
  @stack = []
  @data = {}
end

Instance Method Details

#add(key, value) ⇒ Object Also known as: safe_add

Add the given value to the given key.



30
31
32
# File 'lib/rails/graphql/collectors/hash_collector.rb', line 30

def add(key, value)
  @data.is_a?(::Array) ? @data << value : @data[key.to_s] = value
end

#append_errors(errors) ⇒ Object

Append to the response all the errors that happened during the request process



55
56
57
58
# File 'lib/rails/graphql/collectors/hash_collector.rb', line 55

def append_errors(errors)
  return if errors.empty?
  @data['errors'] = errors.as_json
end

#append_extensions(extensions) ⇒ Object

Append to the response anything added to the extensions



61
62
63
64
# File 'lib/rails/graphql/collectors/hash_collector.rb', line 61

def append_extensions(extensions)
  return if extensions.empty?
  @data['extensions'] = extensions.as_json
end

#key?(key) ⇒ Boolean

Check if a given key has already been added to the current data

Returns:

  • (Boolean)


35
36
37
# File 'lib/rails/graphql/collectors/hash_collector.rb', line 35

def key?(key)
  !@data.is_a?(::Array) && @data.key?(key)
end

#nextObject

Mark the start of a new element on the array.



47
48
49
50
51
# File 'lib/rails/graphql/collectors/hash_collector.rb', line 47

def next
  return unless @stack.last.is_a?(::Array)
  @stack.last << @data
  @data = {}
end

#serialize(klass, key, value) ⇒ Object

Serialize is a helper to call the correct method on types before add



42
43
44
# File 'lib/rails/graphql/collectors/hash_collector.rb', line 42

def serialize(klass, key, value)
  add(key, klass.as_json(value))
end

#to_hObject Also known as: as_json

Return the generated object



67
68
69
# File 'lib/rails/graphql/collectors/hash_collector.rb', line 67

def to_h
  @data
end

#to_sObject Also known as: to_json

Generate the JSON string result



74
75
76
77
78
# File 'lib/rails/graphql/collectors/hash_collector.rb', line 74

def to_s
  GraphQL.config.encode_with_active_support? \
    ? ::ActiveSupport::JSON.encode(@data) \
    : ::JSON.generate(@data)
end

#with_stack(key, array: false, plain: false) ⇒ Object

Shortcut for starting and ending a stack while execute a block.



18
19
20
21
22
23
24
25
26
27
# File 'lib/rails/graphql/collectors/hash_collector.rb', line 18

def with_stack(key, array: false, plain: false)
  return unless block_given?
  start_stack(array, plain)
  yield
  end_stack(key, array, plain)
rescue
  pop_size = array && !plain ? 2 : 1
  @data = @stack.pop(pop_size).first
  raise
end