Class: GraphQL::Subscriptions::Instrumentation

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/subscriptions/instrumentation.rb

Overview

Wrap the root fields of the subscription type with special logic for: - Registering the subscription during the first execution - Evaluating the triggered portion(s) of the subscription during later execution

Instance Method Summary collapse

Constructor Details

#initialize(schema:) ⇒ Instrumentation

Returns a new instance of Instrumentation.



8
9
10
# File 'lib/graphql/subscriptions/instrumentation.rb', line 8

def initialize(schema:)
  @schema = schema
end

Instance Method Details

#after_query(query) ⇒ Object

After checking the root fields, pass the gathered events to the store



20
21
22
23
24
25
# File 'lib/graphql/subscriptions/instrumentation.rb', line 20

def after_query(query)
  events = query.context.namespace(:subscriptions)[:events]
  if events && events.any?
    @schema.subscriptions.write_subscription(query, events)
  end
end

#before_query(query) ⇒ Object

If needed, prepare to gather events which this query subscribes to



13
14
15
16
17
# File 'lib/graphql/subscriptions/instrumentation.rb', line 13

def before_query(query)
  if query.subscription? && !query.subscription_update?
    query.context.namespace(:subscriptions)[:events] = []
  end
end