Overview
TheRails::GraphQL::Request class processes GraphQL operations including queries, mutations, and subscriptions. It handles document parsing, validation, execution, and response formatting.
Namespace: Rails::GraphQL
Extends: ActiveSupport::Autoload
Constructor
initialize
The schema to execute against
The namespace to use if schema not provided
Class Methods
execute
Shortcut to initialize, set context, and execute a request.The GraphQL query document
The schema to execute against
Namespace if schema not provided
Context data available during execution
Query variables
Alias for variables
Name of operation to execute (if document has multiple)
Response format (
:string, :hash, :json, :object)perform
compile
Compile a GraphQL document to cached format.The GraphQL query document
The schema to compile against
Namespace if schema not provided
Whether to compress the compiled output
String - Serialized compiled document
valid?
Validate a GraphQL document.The GraphQL query document
The schema to validate against
Namespace if schema not provided
Boolean
Instance Attributes
Query variables/arguments
The controller or channel that originated the request
Collection of errors encountered during execution
Named fragments defined in the document
Operations defined in the document
The response collector object
The schema being executed
Execution stack for error reporting
Execution strategy (Query, Mutation, Subscription)
Parsed document token
Name of the operation being executed
Active subscriptions created during execution
arguments→argscontroller→originchannel→origin
Instance Methods
execute
Execute a GraphQL document.The GraphQL query document
Response format (
:string, :hash, :json, :object)Query variables
Alias for variables
Operation name to execute
Originating controller or channel
Whether document is pre-compiled
Cache key for storing/retrieving compiled document
Prepared data for specific fields
perform
compile
Compile a document for caching.The GraphQL query document
Whether to compress the output
String - Serialized compiled document
valid?
Check if a document is valid.The GraphQL query document
Boolean
Context Management
context
Get the request context. Returns:OpenStruct (frozen)
context=
Set the request context.Context data
extensions
Get the extensions hash for adding metadata to response. Returns:Hash
Prepared Data
prepare_data_for
Prepare data for a specific field to avoid N+1 queries.The field to prepare data for
The prepared data
Additional options
import_prepared_data
Import multiple prepared data entries.Hash of field names/objects to prepared data
prepared_data_for
Retrieve prepared data for a field.The field to get data for
nil
prepared_data_for?
Check if prepared data exists for a field.The field to check
Boolean
Error Reporting
report_error
Report an error with the current execution context.Error message
Path to the field (auto-generated from stack if not provided)
Source locations
Additional error metadata
nil
report_node_error
Report an error on a specific AST node.Error message
AST node where error occurred
Additional error metadata
exception_to_error
Convert an exception to a GraphQL error.The exception to convert
AST node where exception occurred
Custom error message (defaults to exception message)
rescue_with_handler
Use schema handlers for caught exceptions.The exception to handle
Additional context data
location_of
Get the source location of an AST node.The node to get location for
Array<Hash> - Start and end locations
Cache Management
cache
Get or initialize a cache entry.Cache key
Initial value if not present
Block to compute initial value
nested_cache
Get or initialize a nested cache entry.Outer cache key
Inner cache key
Block to compute value if not present
valid_cache?
Check if the cached operation is still valid. Returns:Boolean
Stack Management
stacked
Execute a block with an object added to the execution stack.Object to add to stack
Block to execute
stack_to_path
Convert the current execution stack to an error path. Returns:Array
Type Resolution
find_type!
Find a type in the schema.Type name
find_type
Subscriptions
subscriptions?
Check if any subscriptions were added during execution. Returns:Boolean
Query Helpers
action_name
Get the action name from the origin controller. Returns:String or nil