Skip to main content

Overview

The Event class is the core mechanism for triggering callbacks in Rails GraphQL. It carries data between event sources and handlers, supports multiple trigger modes, and provides utilities for stopping event propagation.

Class Methods

trigger

A shortcut method that can perform any mode of event triggering.
Event.trigger(event_name, object, source, **xargs, &block)
event_name
Symbol
required
The name of the event to trigger
object
Object
required
The object(s) to trigger the event on, or a block if using block-based triggering
source
Object
required
The source object that is triggering the event
xargs
Hash
Additional data and options for the event. Supports trigger mode options:
  • all? or stack?: Use trigger_all mode
  • object?: Use trigger_object mode
  • single?: Use trigger mode
  • fallback_trigger!: Default mode if no other specified (default: :trigger)
  • collect?: Collect results from callbacks
  • reverse?: Process callbacks in reverse order
block
Proc
Optional block to execute for the event
return
Object
The result of the triggered event

Instance Methods

initialize

Creates a new event instance.
Event.new(name, source, **data)
name
Symbol
required
The event name
source
Object
required
The source object triggering the event
data
Hash
Event data accessible to handlers. Special keys:
  • collect?: Enable result collection
  • reverse?: Process callbacks in reverse order

same_source?

Checks if the provided object is equal to the event source.
event.same_source?(other)
other
Object
required
The object to compare against the event source
return
Boolean
Returns true if the source matches. For directives, checks if the source is using that directive.

parameter

Retrieves event data by name.
event.parameter(name)
event[name]  # alias
name
Symbol
required
The parameter name to retrieve
return
Object
The parameter value from the event data or a method with that name

parameter?

Checks if a parameter exists.
event.parameter?(name)
event.key?(name)  # alias
name
Symbol
required
The parameter name to check
return
Boolean
Returns true if the parameter exists

set_on

Temporarily attaches the event to an instance.
event.set_on(instance, &block)
event.on_instance(instance, &block)  # alias
instance
Object
required
The instance to attach the event to
block
Proc
required
The block to execute with the event attached
return
Object
The result of the block execution

trigger_all

Triggers the event on multiple objects.
event.trigger_all(*objects)
objects
Array
required
The objects to trigger the event on. Can be a flat array or enumerable.
return
Array | nil
If collect? was set, returns an array of results. Otherwise returns nil.

trigger_object

Triggers the event on a single object by running its callbacks.
event.trigger_object(object, events = nil)
object
Object
required
The object to trigger the event on
events
Array
Optional pre-fetched events array (optimization)
return
Object
The result of the last callback execution

trigger

Triggers a single callback block.
event.trigger(block)
block
Proc
required
The callback block to execute
return
Object
The result of the block execution

stop

Stops event propagation at a specific layer.
event.stop(*result, layer: nil)
result
Object
Optional result to return when stopping
layer
Symbol | Integer
The layer to stop at. Can be a numeric index or nil for the current layer.

call_next

Calls the next callback in the queue.
event.call_next
return
Object
The result of the next callback, or nil if no more callbacks

Attributes

source
Object
The object that triggered the event
data
Hash
The event data hash containing all parameters
event_name
Symbol
The name of the event
object
Object
The current object being processed (during trigger_object)
last_result
Object
The result of the last callback execution

Constants

TRIGGER_TYPES
Hash
Maps trigger mode options to method names:
  • all?: :trigger_all
  • stack?: :trigger_all
  • object?: :trigger_object
  • single?: :trigger

Event Layers

Events use a layered catch/throw system for control flow:
  • :stack - The outer layer when processing multiple objects
  • :object - The layer when processing a single object’s callbacks
  • :item - The layer for individual callback execution
Use stop(layer: :stack) to stop at a specific layer.

Build docs developers (and LLMs) love