Class: GraphQL::Schema::Timeout
- Inherits:
-
Object
- Object
- GraphQL::Schema::Timeout
- Defined in:
- lib/graphql/schema/timeout.rb
Overview
This plugin will stop resolving new fields after max_seconds
have elapsed.
After the time has passed, any remaining fields will be nil
, with errors added
to the errors
key. Any already-resolved fields will be in the data
key, so
you’ll get a partial response.
You can subclass GraphQL::Schema::Timeout
and override max_seconds
and/or handle_timeout
to provide custom logic when a timeout error occurs.
Note that this will stop a query in between field resolutions, but
it doesn’t interrupt long-running resolve
functions. Be sure to use
timeout options for external connections. For more info, see
www.mikeperham.com/2015/05/08/timeout-rubys-most-dangerous-api/
Defined Under Namespace
Modules: Trace Classes: TimeoutError
Class Method Summary collapse
Instance Method Summary collapse
-
#disable_timeout(query) ⇒ void
Call this method (eg, from #handle_timeout) to disable timeout tracking for the given query.
-
#handle_timeout(error, query) ⇒ Object
Invoked when a query times out.
-
#initialize(max_seconds:) ⇒ Timeout
constructor
A new instance of Timeout.
-
#max_seconds(query) ⇒ Numeric, false
Called at the start of each query.
Constructor Details
#initialize(max_seconds:) ⇒ Timeout
Returns a new instance of Timeout.
41 42 43 |
# File 'lib/graphql/schema/timeout.rb', line 41 def initialize(max_seconds:) @max_seconds = max_seconds end |
Class Method Details
.use(schema, max_seconds: nil) ⇒ Object
36 37 38 39 |
# File 'lib/graphql/schema/timeout.rb', line 36 def self.use(schema, max_seconds: nil) timeout = self.new(max_seconds: max_seconds) schema.trace_with(self::Trace, timeout: timeout) end |
Instance Method Details
#disable_timeout(query) ⇒ void
This method returns an undefined value.
Call this method (eg, from #handle_timeout) to disable timeout tracking for the given query.
117 118 119 120 |
# File 'lib/graphql/schema/timeout.rb', line 117 def disable_timeout(query) query.context.namespace(self)[:state] = false nil end |
#handle_timeout(error, query) ⇒ Object
Invoked when a query times out.
109 110 111 |
# File 'lib/graphql/schema/timeout.rb', line 109 def handle_timeout(error, query) # override to do something interesting end |
#max_seconds(query) ⇒ Numeric, false
Called at the start of each query.
The default implementation returns the max_seconds:
value from installing this plugin.
102 103 104 |
# File 'lib/graphql/schema/timeout.rb', line 102 def max_seconds(query) @max_seconds end |