Class: Rails::GraphQL::Collectors::JsonCollector

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

Overview

GraphQL JSON Collector

This collector helps building a JSON response using the string approach, which has better performance, since all the encoding is performed up front. The drawback is that it can’t return a hash.

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ JsonCollector

Returns a new instance of JsonCollector.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rails/graphql/collectors/json_collector.rb', line 12

def initialize(request)
  @request = request

  @current_keys = Set.new
  @stack_keys = []

  @current_value = StringIO.new
  @stack_value = []

  @current_array = false
  @stack_array = []

  @current_plain_array = false
  @stack_plain_array = []
end

Instance Method Details

#add(key, value) ⇒ Object

Add the given value to the given key. Ensure to encode the value before calling this function.



57
58
59
60
61
62
63
64
65
66
# File 'lib/rails/graphql/collectors/json_collector.rb', line 57

def add(key, value)
  (@current_value << ',') if @current_value.pos > 0
  @current_keys << key

  if @current_array
    @current_value << value
  else
    @current_value << '"' << +key.to_s << '"' << ':' << +value.to_s
  end
end

#append_errors(errors) ⇒ Object

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



44
45
46
47
# File 'lib/rails/graphql/collectors/json_collector.rb', line 44

def append_errors(errors)
  return if errors.empty?
  add('errors', errors.to_json)
end

#append_extensions(extensions) ⇒ Object

Append to the response anything added to the extensions



50
51
52
53
# File 'lib/rails/graphql/collectors/json_collector.rb', line 50

def append_extensions(extensions)
  return if extensions.empty?
  add('extensions', extensions.to_json)
end

#key?(key) ⇒ Boolean

Check if the given key has been added already

Returns:

  • (Boolean)


69
70
71
# File 'lib/rails/graphql/collectors/json_collector.rb', line 69

def key?(key)
  @current_keys.include?(key)
end

#nextObject

Mark the start of a new element on the array.



84
85
86
87
88
89
90
91
# File 'lib/rails/graphql/collectors/json_collector.rb', line 84

def next
  return unless @stack_array.last === :complex
  (@stack_value.last << ',') if @stack_value.last.pos > 0
  @stack_value.last << to_s

  @current_value = StringIO.new
  @current_keys = Set.new
end

#safe_add(key, value) ⇒ Object

Same as add but this always encode the value beforehand.



74
75
76
# File 'lib/rails/graphql/collectors/json_collector.rb', line 74

def safe_add(key, value)
  add(key, value.nil? ? 'null' : value.to_json)
end

#serialize(klass, key, value) ⇒ Object

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



79
80
81
# File 'lib/rails/graphql/collectors/json_collector.rb', line 79

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

#to_sObject Also known as: to_json

Get the current result



94
95
96
97
98
99
100
# File 'lib/rails/graphql/collectors/json_collector.rb', line 94

def to_s
  if @current_array
    +'[' << @current_value.string << ']'
  else
    +'{' << @current_value.string << '}'
  end
end

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

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



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rails/graphql/collectors/json_collector.rb', line 29

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
  @current_value = @stack_value.pop(pop_size).first
  @current_array = @stack_array.pop(pop_size).first
  @current_keys = @stack_keys.pop
  raise
end