Class: GraphQL::Subscriptions::BroadcastAnalyzer

Inherits:
Analysis::Analyzer show all
Defined in:
lib/graphql/subscriptions/broadcast_analyzer.rb

Overview

Detect whether the current operation:

  • Is a subscription operation
  • Is completely broadcastable

Assign the result to context.namespace(:subscriptions)[:subscription_broadcastable] See Subscriptions#broadcastable? for a public API

Instance Method Summary collapse

Methods inherited from Analysis::Analyzer

#visit?

Constructor Details

#initialize(subject) ⇒ BroadcastAnalyzer

:nodoc:



14
15
16
17
18
19
# File 'lib/graphql/subscriptions/broadcast_analyzer.rb', line 14

def initialize(subject)
  super
  @default_broadcastable = subject.schema.subscriptions.default_broadcastable
  # Maybe this will get set to false while analyzing
  @subscription_broadcastable = true
end

Instance Method Details

#analyze? ⇒ Boolean

Only analyze subscription operations

Returns:

  • (Boolean)


22
23
24
# File 'lib/graphql/subscriptions/broadcast_analyzer.rb', line 22

def analyze?
  @query.subscription?
end

#on_enter_field(node, parent, visitor) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/graphql/subscriptions/broadcast_analyzer.rb', line 26

def on_enter_field(node, parent, visitor)
  if (@subscription_broadcastable == false) || visitor.skipping?
    return
  end

  current_field = visitor.field_definition
  current_type = visitor.parent_type_definition
  apply_broadcastable(current_type, current_field)
  if current_type.kind.interface?
    pt = @query.possible_types(current_type)
    pt.each do |object_type|
      ot_field = @query.get_field(object_type, current_field.graphql_name)
      # Inherited fields would be exactly the same object;
      # only check fields that are overrides of the inherited one
      if ot_field && ot_field != current_field
        apply_broadcastable(object_type, ot_field)
      end
    end
  end
end

#result ⇒ Object

Assign the result to context. (This method is allowed to return an error, but we don't need to)

Returns

  • void

:call-seq:

result() -> void


56
57
58
59
# File 'lib/graphql/subscriptions/broadcast_analyzer.rb', line 56

def result
  query.context.namespace(:subscriptions)[:subscription_broadcastable] = @subscription_broadcastable
  nil
end