class GraphQL::Subscriptions::Instrumentation

Wrap the root fields of the subscription type with special logic for:

Public Class Methods

new(schema:) click to toggle source
# File lib/graphql/subscriptions/instrumentation.rb, line 8
def initialize(schema))
  @schema = schema
end

Public Instance Methods

after_query(query) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source
# File lib/graphql/subscriptions/instrumentation.rb, line 12
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