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.



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



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

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



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

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

#instrument(type, field) ⇒ Object



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

def instrument(type, field)
  if type == @schema.subscription.graphql_definition
    # 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