Module: GraphQL::Schema::Timeout::Trace

Defined in:
lib/graphql/schema/timeout.rb

Instance Method Summary collapse

Instance Method Details

#execute_field(query:, field:, **_rest) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/graphql/schema/timeout.rb', line 71

def execute_field(query:, field:, **_rest)
  timeout_state = query.context.namespace(@timeout).fetch(:state)
  # If the `:state` is `false`, then `max_seconds(query)` opted out of timeout for this query.
  if timeout_state != false && Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) > timeout_state.fetch(:timeout_at)
    error = GraphQL::Schema::Timeout::TimeoutError.new(field)
    # Only invoke the timeout callback for the first timeout
    if !timeout_state[:timed_out]
      timeout_state[:timed_out] = true
      @timeout.handle_timeout(error, query)
    end

    error
  else
    super
  end
end

#execute_multiplex(multiplex:) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/graphql/schema/timeout.rb', line 52

def execute_multiplex(multiplex:)
  multiplex.queries.each do |query|
    timeout_duration_s = @timeout.max_seconds(query)
    timeout_state = if timeout_duration_s == false
      # if the method returns `false`, don't apply a timeout
      false
    else
      now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
      timeout_at = now + (timeout_duration_s * 1000)
      {
        timeout_at: timeout_at,
        timed_out: false
      }
    end
    query.context.namespace(@timeout)[:state] = timeout_state
  end
  super
end

#initialize(timeout:, **rest) ⇒ Object

Parameters:

  • max_seconds (Numeric)

    how many seconds the query should be allowed to resolve new fields



47
48
49
50
# File 'lib/graphql/schema/timeout.rb', line 47

def initialize(timeout:, **rest)
  @timeout = timeout
  super
end