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
https://www.mikeperham.com/2015/05/08/timeout-rubys-most-dangerous-api/
Examples
Example: Stop resolving fields after 2 seconds
class MySchema < GraphQL::Schema
use GraphQL::Schema::Timeout, max_seconds: 2
end
Example: Notifying Bugsnag and logging a timeout
class MyTimeout < GraphQL::Schema::Timeout
def handle_timeout(error, query)
Rails.logger.warn("GraphQL Timeout: #{error.}: #{query.query_string}")
Bugsnag.notify(error, {query_string: query.query_string})
end
end
class MySchema < GraphQL::Schema
use MyTimeout, max_seconds: 2
end
Defined Under Namespace
Modules: Trace Classes: TimeoutError
Class Method Summary collapse
Instance Method Summary collapse
-
#disable_timeout(query) ⇒ Object
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) ⇒ Object
Called at the start of each query.
Constructor Details
#initialize(max_seconds:) ⇒ Timeout
Returns a new instance of Timeout.
50 51 52 |
# File 'lib/graphql/schema/timeout.rb', line 50 def initialize(max_seconds:) @max_seconds = max_seconds end |
Class Method Details
.use(schema, max_seconds: nil) ⇒ Object
45 46 47 48 |
# File 'lib/graphql/schema/timeout.rb', line 45 def self.use(schema, max_seconds: nil) timeout = self.new(max_seconds: max_seconds) schema.trace_with(const_get(:Trace), timeout: timeout) end |
Instance Method Details
#disable_timeout(query) ⇒ Object
Call this method (eg, from handle timeout) to disable timeout tracking for the given query.
Parameters
query(GraphQL::Query)
Returns
void
:call-seq:
disable_timeout(GraphQL::Query query) -> void
158 159 160 161 |
# File 'lib/graphql/schema/timeout.rb', line 158 def disable_timeout(query) query.context.namespace(self)[:state] = false nil end |
#handle_timeout(error, query) ⇒ Object
Invoked when a query times out.
Parameters
error(GraphQL::Schema::Timeout::TimeoutError)query(GraphQL::Error)
:call-seq:
handle_timeout(GraphQL::Schema::Timeout::TimeoutError error, GraphQL::Error query)
141 142 143 |
# File 'lib/graphql/schema/timeout.rb', line 141 def handle_timeout(error, query) # override to do something interesting end |
#max_seconds(query) ⇒ Object
Called at the start of each query.
The default implementation returns the max_seconds: value from installing this plugin.
Parameters
query(GraphQL::Query) — The query that's about to run
Returns
Numeric, false— The number of seconds after which to interrupt query execution and callhandle_error, orfalseto bypass the timeout.
:call-seq:
max_seconds(GraphQL::Query query) -> Numeric | false
128 129 130 |
# File 'lib/graphql/schema/timeout.rb', line 128 def max_seconds(query) @max_seconds end |