lib/rails/graphql/collectors/json_collector.rb in rails-graphql-1.0.0.beta vs lib/rails/graphql/collectors/json_collector.rb in rails-graphql-1.0.0.rc1
- old
+ new
@@ -10,10 +10,13 @@
# front. The drawback is that it can't return a hash.
class JsonCollector
def initialize(request)
@request = request
+ @current_keys = Set.new
+ @stack_keys = []
+
@current_value = StringIO.new
@stack_value = []
@current_array = false
@stack_array = []
@@ -30,32 +33,45 @@
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
# Append to the response data all the errors that happened during the
# request process.
def append_errors(errors)
return if errors.empty?
add('errors', errors.to_json)
end
+ # Append to the response anything added to the extensions
+ def append_extensions(extensions)
+ return if extensions.empty?
+ add('extensions', extensions.to_json)
+ end
+
# Add the given +value+ to the given +key+. Ensure to encode the value
# before calling this function.
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
+ # Check if the given +key+ has been added already
+ def key?(key)
+ @current_keys.include?(key)
+ end
+
# Same as +add+ but this always encode the +value+ beforehand.
def safe_add(key, value)
add(key, value.nil? ? 'null' : value.to_json)
end
@@ -67,11 +83,13 @@
# Mark the start of a new element on the array.
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
# Get the current result
def to_s
if @current_array
@@ -88,19 +106,21 @@
# Start a new part of the collector. When set +as_array+, the result
# of the stack will be enclosed by +[]+.
def start_stack(as_array = false, plain_array = false)
@stack_value << @current_value
@stack_array << @current_array
+ @stack_keys << @current_keys
if as_array && !plain_array
@stack_value << StringIO.new
@stack_array << :complex
as_array = false
end
@current_value = StringIO.new
@current_array = as_array
+ @current_keys = Set.new
end
# Finalize a stack and set the result on the given +key+.
def end_stack(key, as_array = false, plain_array = false)
if as_array && !plain_array
@@ -109,9 +129,10 @@
end
result = to_s
@current_value = @stack_value.pop
@current_array = @stack_array.pop
+ @current_keys = @stack_keys.pop
add(key, result)
end
end
end
end