Overview
TheEvent 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.The name of the event to trigger
The object(s) to trigger the event on, or a block if using block-based triggering
The source object that is triggering the event
Additional data and options for the event. Supports trigger mode options:
all?orstack?: Usetrigger_allmodeobject?: Usetrigger_objectmodesingle?: Usetriggermodefallback_trigger!: Default mode if no other specified (default::trigger)collect?: Collect results from callbacksreverse?: Process callbacks in reverse order
Optional block to execute for the event
The result of the triggered event
Instance Methods
initialize
Creates a new event instance.The event name
The source object triggering the event
Event data accessible to handlers. Special keys:
collect?: Enable result collectionreverse?: Process callbacks in reverse order
same_source?
Checks if the provided object is equal to the event source.The object to compare against the event source
Returns true if the source matches. For directives, checks if the source is using that directive.
parameter
Retrieves event data by name.The parameter name to retrieve
The parameter value from the event data or a method with that name
parameter?
Checks if a parameter exists.The parameter name to check
Returns true if the parameter exists
set_on
Temporarily attaches the event to an instance.The instance to attach the event to
The block to execute with the event attached
The result of the block execution
trigger_all
Triggers the event on multiple objects.The objects to trigger the event on. Can be a flat array or enumerable.
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.The object to trigger the event on
Optional pre-fetched events array (optimization)
The result of the last callback execution
trigger
Triggers a single callback block.The callback block to execute
The result of the block execution
stop
Stops event propagation at a specific layer.Optional result to return when stopping
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.The result of the next callback, or nil if no more callbacks
Attributes
The object that triggered the event
The event data hash containing all parameters
The name of the event
The current object being processed (during trigger_object)
The result of the last callback execution
Constants
Maps trigger mode options to method names:
all?: :trigger_allstack?: :trigger_allobject?: :trigger_objectsingle?: :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
stop(layer: :stack) to stop at a specific layer.