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

Defined Under Namespace

Classes: SubscriptionRegistrationResolve

Instance Method Summary collapse

Constructor Details

#initialize(schema:) ⇒ Instrumentation

Returns a new instance of Instrumentation



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

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



31
32
33
34
35
36
# File 'lib/graphql/subscriptions/instrumentation.rb', line 31

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



24
25
26
27
28
# File 'lib/graphql/subscriptions/instrumentation.rb', line 24

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

#instrument(type, field) ⇒ Object



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

def instrument(type, field)
  if type == @schema.subscription
    # This is a root field of `subscription`
    subscribing_resolve_proc = SubscriptionRegistrationResolve.new(field.resolve_proc)
    field.redefine(resolve: subscribing_resolve_proc)
  else
    field
  end
end