Skip to main content

Overview

The Callback class is a central component of the Rails GraphQL event system. It wraps blocks or symbols as callbacks that can be triggered by events, with support for filtering, argument injection, and context management.

Class Methods

set_context

Contextualizes a callback with a given instance.
Callback.set_context(item, context)
item
Proc
The callback proc to contextualize
context
Object
The context instance to bind to the callback
return
Lambda
Returns a lambda that injects the context as _callback_context

Instance Methods

initialize

Creates a new callback instance.
Callback.new(target, event_name, *args, **xargs, &block)
target
Object
required
The target object that owns this callback
event_name
Symbol
required
The name of the event this callback responds to
args
Array
Optional arguments to prepend when calling the callback
xargs
Hash
Keyword arguments including filters and options. Special option exclusive_callback controls exclusivity.
block
Proc
The callback block or a symbol representing a method name

call

Executes the callback if the event matches and filters pass.
callback.call(event, *args, _callback_context: nil, **xargs)
event
Event
required
The event being triggered
args
Array
Arguments to pass to the callback
_callback_context
Object
Optional context for the callback execution
xargs
Hash
Keyword arguments to pass to the callback
return
Object
The result of the callback execution, or nil if the callback doesn’t run

exclusive?

Checks if the callback is exclusive to its source.
callback.exclusive?
return
Boolean
Returns true if only the original source can trigger this callback

source_location

Returns the source location of the callback for debugging.
callback.source_location
return
Array
Returns [file_path, line_number] for proc callbacks, or a descriptive array for symbol callbacks

to_proc

Converts the callback to a proc.
callback.to_proc
return
Proc
Returns a proc that can be used with the & operator

Attributes

event_name
Symbol
The name of the event this callback responds to
block
Proc | Symbol
The callback implementation (proc or method symbol)
target
Object
The object that owns this callback
filters
Hash
The filters that determine when this callback can run

Constants

EXCLUSIVE_ARGUMENT
Symbol
The keyword argument key used to control exclusivity: :exclusive_callback

Argument Injection

Callbacks support automatic argument injection based on configuration:
  • Named arguments: When callback_inject_named_arguments is enabled, keyword parameters are automatically injected from the event data
  • Positional arguments: When callback_inject_arguments is enabled, positional parameters are injected by name from the event

Filters

Callbacks can be filtered using event filters. All filters must pass for the callback to execute. Filters are evaluated using instance_exec on the target with the filter options and event.

Build docs developers (and LLMs) love