Class: GraphQL::Subscriptions::Instrumentation::SubscriptionRegistrationResolve

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

Instance Method Summary collapse

Constructor Details

#initialize(inner_proc) ⇒ SubscriptionRegistrationResolve

Returns a new instance of SubscriptionRegistrationResolve.


41
42
43
# File 'lib/graphql/subscriptions/instrumentation.rb', line 41

def initialize(inner_proc)
  @inner_proc = inner_proc
end

Instance Method Details

#call(obj, args, ctx) ⇒ Object

Wrap the proc with subscription registration logic


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/graphql/subscriptions/instrumentation.rb', line 46

def call(obj, args, ctx)
  result = nil
  if @inner_proc && !@inner_proc.is_a?(GraphQL::Field::Resolve::BuiltInResolve)
    result = @inner_proc.call(obj, args, ctx)
  end

  events = ctx.namespace(:subscriptions)[:events]

  if events
    # This is the first execution, so gather an Event
    # for the backend to register:
    events << Subscriptions::Event.new(
      name: ctx.field.name,
      arguments: args,
      context: ctx,
    )
    result
  elsif ctx.irep_node.subscription_topic == ctx.query.subscription_topic
    if !result.nil?
      result
    elsif obj.is_a?(GraphQL::Schema::Object)
      # The root object is _already_ the subscription update:
      obj.object
    else
      obj
    end
  else
    # This is a subscription update, but this event wasn't triggered.
    ctx.skip
  end
end