Skip to main content

Overview

The Collectors 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

HashCollector.new(request)
request
Request
required
The GraphQL request object

with_stack

Shortcut for starting and ending a stack while executing a block.
collector.with_stack(key, array: false, plain: false, &block)
key
String | Symbol
required
The key to assign the stack result to
array
Boolean
default:"false"
Whether the stack should collect as an array
plain
Boolean
default:"false"
Whether to use a plain array (single stack level)

add

Adds a value to the current collection.
collector.add(key, value)
collector.safe_add(key, value)  # alias
key
String | Symbol
required
The key for the value (ignored if current collection is an array)
value
Object
required
The value to add

serialize

Helper to call the correct serialization method on types before adding.
collector.serialize(klass, key, value)
klass
Class
required
The class that implements as_json
key
String | Symbol
required
The key to add the serialized value to
value
Object
required
The value to serialize

key?

Checks if a key has already been added.
collector.key?(key)
key
String | Symbol
required
The key to check
return
Boolean
Returns true if the key exists (always false for array collections)

next

Marks the start of a new element in an array collection.
collector.next

append_errors

Appends errors to the response.
collector.append_errors(errors)
errors
Array
required
The errors collection to append

append_extensions

Appends extensions to the response.
collector.append_extensions(extensions)
extensions
Hash
required
The extensions hash to append

to_h / as_json

Returns the generated hash object.
collector.to_h
collector.as_json  # alias
return
Hash
The collected data as a hash

to_s / to_json

Generates the JSON string result.
collector.to_s
collector.to_json  # alias
return
String
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

JsonCollector.new(request)
request
Request
required
The GraphQL request object

with_stack

Shortcut for starting and ending a stack while executing a block.
collector.with_stack(key, array: false, plain: false, &block)
key
String | Symbol
required
The key to assign the stack result to
array
Boolean
default:"false"
Whether the stack should collect as an array
plain
Boolean
default:"false"
Whether to use a plain array (single stack level)

add

Adds an already-encoded value to the current collection.
collector.add(key, value)
key
String | Symbol
required
The key for the value (ignored if current collection is an array)
value
String
required
The pre-encoded JSON value string

safe_add

Adds a value and automatically encodes it.
collector.safe_add(key, value)
key
String | Symbol
required
The key for the value
value
Object
required
The value to encode and add

serialize

Helper to call the correct serialization method on types before adding.
collector.serialize(klass, key, value)
klass
Class
required
The class that implements to_json
key
String | Symbol
required
The key to add the serialized value to
value
Object
required
The value to serialize

key?

Checks if a key has already been added.
collector.key?(key)
key
String | Symbol
required
The key to check
return
Boolean
Returns true if the key exists in the current collection

next

Marks the start of a new element in an array collection.
collector.next

append_errors

Appends errors to the response.
collector.append_errors(errors)
errors
Array
required
The errors collection to append

append_extensions

Appends extensions to the response.
collector.append_extensions(extensions)
extensions
Hash
required
The extensions hash to append

to_s / to_json

Returns the generated JSON string.
collector.to_s
collector.to_json  # alias
return
String
The complete JSON string enclosed in braces or brackets

IdentedCollector

Builds indented string output, primarily used for displaying GraphQL schemas.

initialize

IdentedCollector.new(initial = 0, size = 2, auto_eol: true)
initial
Integer
default:"0"
Initial indentation level
size
Integer
default:"2"
Number of spaces per indentation level
auto_eol
Boolean
default:"true"
Automatically add end-of-line after indented blocks

indented

Creates an indented block.
collector.indented(start = nil, finish = nil, auto_eol = @auto_eol, &block)
start
String
Optional string to add before indenting
finish
String
Optional string to add after unindenting
auto_eol
Boolean
Whether to add a line break after the block
return
IdentedCollector
Returns self for chaining

puts

Adds a string and moves to the next line.
collector.puts(str)
str
String
required
The string to add

<< operator

Appends a string to the current line.
collector << str
str
String
required
The string to append
return
IdentedCollector
Returns self for chaining

eol

Adds an end-of-line marker.
collector.eol
return
IdentedCollector
Returns self for chaining

indent

Increases indentation level.
collector.indent
return
IdentedCollector
Returns self for chaining

unindent

Decreases indentation level.
collector.unindent
return
IdentedCollector
Returns self for chaining

value

Returns the final indented string.
collector.value
return
String
The complete indented text with all formatting applied

Collector Interface

All collectors implement a common interface:
  • initialize(request) - Create a new collector
  • with_stack(key, array:, plain:) - Manage nested structures
  • add(key, value) - Add data to the collection
  • serialize(klass, key, value) - Serialize and add data
  • key?(key) - Check if a key exists
  • next - Move to the next array element
  • append_errors(errors) - Add errors to the response
  • append_extensions(extensions) - Add extensions to the response
Choose the collector based on your needs:
  • 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)

Build docs developers (and LLMs) love