Timeout

You can apply a timeout to query execution with TimeoutMiddleware. For example:

MySchema.middleware << GraphQL::Schema::TimeoutMiddleware.new(max_seconds: 2)

After max_seconds, no new fields will be resolved. Instead, errors will be added to the errors key for fields that weren’t resolved.

Note that this does not interrupt field execution (doing so is buggy). If you’re making external calls (eg, HTTP requests or database queries), make sure to use a library-specific timeout for that operation (eg, Redis timeout, Net::HTTP’s ssl_timeout, open_timeout, and read_timeout).

To log the error, pass a block to the middleware:

MySchema.middleware << GraphQL::Schema::TimeoutMiddleware.new(max_seconds: 2) do |err, query|
  Rails.logger.info("GraphQL Timeout: #{query.query_string}")
end