Class: GraphQL::Subscriptions::DefaultSubscriptionResolveExtension

Inherits:
GraphQL::Schema::FieldExtension show all
Defined in:
lib/graphql/subscriptions/default_subscription_resolve_extension.rb

Instance Attribute Summary

Attributes inherited from GraphQL::Schema::FieldExtension

#added_default_arguments, #added_extras, #field, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GraphQL::Schema::FieldExtension

#after_define, #after_define_apply, #apply, default_argument, default_argument_configurations, extras, #initialize

Constructor Details

This class inherits a constructor from GraphQL::Schema::FieldExtension

Class Method Details

.write_subscription(field, value, arguments, context) ⇒ Object



44
45
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
77
78
79
80
81
# File 'lib/graphql/subscriptions/default_subscription_resolve_extension.rb', line 44

def self.write_subscription(field, value, arguments, context)
  if value.is_a?(GraphQL::ExecutionError)
    value
  elsif field.resolver&.method_defined?(:subscription_written?) &&
    (subscription_namespace = context.namespace(:subscriptions)) &&
    (subscriptions_by_path = subscription_namespace[:subscriptions])
    (subscription_instance = subscriptions_by_path[context.current_path])
    # If it was already written, don't append this event to be written later
    if !subscription_instance.subscription_written?
      events = context.namespace(:subscriptions)[:events]
      events << subscription_instance.event
    end
    value
  elsif (events = context.namespace(:subscriptions)[:events])
    # This is the first execution, so gather an Event
    # for the backend to register:
    event = Subscriptions::Event.new(
      name: field.name,
      arguments: arguments,
      context: context,
      field: field,
    )
    events << event
    value
  elsif context.query.subscription_topic == Subscriptions::Event.serialize(
      field.name,
      arguments,
      field,
      scope: (field.subscription_scope ? context[field.subscription_scope] : nil),
    )
    # This is a subscription update. The resolver returned `skip` if it should be skipped,
    # or else it returned an object to resolve the update.
    value
  else
    # This is a subscription update, but this event wasn't triggered.
    context.skip
  end
end

Instance Method Details

#after_resolve(value:, context:, object:, arguments:, **rest) ⇒ Object



34
35
36
# File 'lib/graphql/subscriptions/default_subscription_resolve_extension.rb', line 34

def after_resolve(value:, context:, object:, arguments:, **rest)
  self.class.write_subscription(@field, value, arguments, context)
end

#after_resolve_next(values:, context:, objects:, arguments:, **rest) ⇒ Object



38
39
40
41
42
# File 'lib/graphql/subscriptions/default_subscription_resolve_extension.rb', line 38

def after_resolve_next(values:, context:, objects:, arguments:, **rest)
  values.map do |value|
    self.class.write_subscription(@field, value, arguments, context)
  end
end

#resolve(context:, object:, arguments:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/graphql/subscriptions/default_subscription_resolve_extension.rb', line 5

def resolve(context:, object:, arguments:)
  has_override_implementation = @field.resolver ||
    object.respond_to?(@field.resolver_method)

  if !has_override_implementation
    if context.query.subscription_update?
      object.object
    else
      context.skip
    end
  else
    yield(object, arguments)
  end
end

#resolve_next(context:, objects:, arguments:) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/graphql/subscriptions/default_subscription_resolve_extension.rb', line 20

def resolve_next(context:, objects:, arguments:)
  has_override_implementation = @field.execution_next_mode != :direct_send

  if !has_override_implementation
    if context.query.subscription_update?
      objects
    else
      objects.map { |o| context.skip }
    end
  else
    yield(objects, arguments)
  end
end