Overview
TheCollectors module provides different strategies for building GraphQL responses. Each collector supports the same interface but optimizes for different use cases: hash-based JSON, string-based JSON, or indented text output.
HashCollector
Builds JSON responses using the hash approach, keeping values as a hash that can be converted to a string later.initialize
The GraphQL request object
with_stack
Shortcut for starting and ending a stack while executing a block.The key to assign the stack result to
Whether the stack should collect as an array
Whether to use a plain array (single stack level)
add
Adds a value to the current collection.The key for the value (ignored if current collection is an array)
The value to add
serialize
Helper to call the correct serialization method on types before adding.The class that implements
as_jsonThe key to add the serialized value to
The value to serialize
key?
Checks if a key has already been added.The key to check
Returns true if the key exists (always false for array collections)
next
Marks the start of a new element in an array collection.append_errors
Appends errors to the response.The errors collection to append
append_extensions
Appends extensions to the response.The extensions hash to append
to_h / as_json
Returns the generated hash object.The collected data as a hash
to_s / to_json
Generates the JSON string result.The JSON-encoded string (uses ActiveSupport::JSON or JSON based on configuration)
JsonCollector
Builds JSON responses using the string approach with better performance. All encoding is performed up front, but cannot return a hash.initialize
The GraphQL request object
with_stack
Shortcut for starting and ending a stack while executing a block.The key to assign the stack result to
Whether the stack should collect as an array
Whether to use a plain array (single stack level)
add
Adds an already-encoded value to the current collection.The key for the value (ignored if current collection is an array)
The pre-encoded JSON value string
safe_add
Adds a value and automatically encodes it.The key for the value
The value to encode and add
serialize
Helper to call the correct serialization method on types before adding.The class that implements
to_jsonThe key to add the serialized value to
The value to serialize
key?
Checks if a key has already been added.The key to check
Returns true if the key exists in the current collection
next
Marks the start of a new element in an array collection.append_errors
Appends errors to the response.The errors collection to append
append_extensions
Appends extensions to the response.The extensions hash to append
to_s / to_json
Returns the generated JSON string.The complete JSON string enclosed in braces or brackets
IdentedCollector
Builds indented string output, primarily used for displaying GraphQL schemas.initialize
Initial indentation level
Number of spaces per indentation level
Automatically add end-of-line after indented blocks
indented
Creates an indented block.Optional string to add before indenting
Optional string to add after unindenting
Whether to add a line break after the block
Returns self for chaining
puts
Adds a string and moves to the next line.The string to add
<< operator
Appends a string to the current line.
The string to append
Returns self for chaining
eol
Adds an end-of-line marker.Returns self for chaining
indent
Increases indentation level.Returns self for chaining
unindent
Decreases indentation level.Returns self for chaining
value
Returns the final indented string.The complete indented text with all formatting applied
Collector Interface
All collectors implement a common interface:initialize(request)- Create a new collectorwith_stack(key, array:, plain:)- Manage nested structuresadd(key, value)- Add data to the collectionserialize(klass, key, value)- Serialize and add datakey?(key)- Check if a key existsnext- Move to the next array elementappend_errors(errors)- Add errors to the responseappend_extensions(extensions)- Add extensions to the response
- HashCollector: When you need both hash and JSON output
- JsonCollector: When you only need JSON and want maximum performance
- IdentedCollector: When you need human-readable formatted text (e.g., schema introspection)