Class: Rails::GraphQL::Request::Component::Operation::Subscription

Inherits:
Rails::GraphQL::Request::Component::Operation show all
Defined in:
lib/rails/graphql/request/component/operation/subscription.rb

Overview

GraphQL Request Component Subscription Operation

Handles a subscription operation inside a request.

Constant Summary collapse

UNSUBSCRIBED_PAYLOAD =
{ 'more' => false }.freeze
UNSUBSCRIBED_RESULT =
Object.new

Constants inherited from Rails::GraphQL::Request::Component::Operation

Mutation, Query

Instance Attribute Summary

Attributes inherited from Rails::GraphQL::Request::Component::Operation

#arguments, #name, #request, #variables

Attributes included from SelectionSet

#selection

Instance Method Summary collapse

Methods inherited from Rails::GraphQL::Request::Component::Operation

build, #fields_source, #hash, kind, #log_source, #memo, mutation?, query?, #resolve_invalid, subscription?, type, #type_klass, #typename, #used_fragments, #used_variables

Methods included from Directives

#directive_events, #directive_listeners, #using?

Methods inherited from Rails::GraphQL::Request::Component

#assignable?, #hash, #invalid?, #invalidate!, kind, #skip!, #skipped?, #unresolvable?

Methods included from Resolvable

#resolve!

Methods included from Preparable

#prepare!, #prepared_data!, #prepared_data?

Methods included from Organizable

#organize!

Constructor Details

#initializeSubscription

Returns a new instance of Subscription.



16
17
18
19
20
# File 'lib/rails/graphql/request/component/operation/subscription.rb', line 16

def initialize(*)
  @initial = true

  super
end

Instance Method Details

#broadcastable?Boolean

Check if the operation can be broadcasted

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/rails/graphql/request/component/operation/subscription.rb', line 41

def broadcastable?
  return @broadcastable if defined?(@broadcastable)
  @broadcastable = selection.each_value.all?(&:broadcastable?)
end

#broadcasting?Boolean

Check if the current operation is running under broadcasting mode

Returns:

  • (Boolean)


36
37
38
# File 'lib/rails/graphql/request/component/operation/subscription.rb', line 36

def broadcasting?
  request.context.broadcasting == true
end

#cache_dump(initial = true) ⇒ Object

Build the cache object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rails/graphql/request/component/operation/subscription.rb', line 62

def cache_dump(initial = true)
  hash = super()
  hash[:initial] = initial
  hash[:broadcastable] = broadcastable?

  unless initial
    hash[:sid] = @subscription&.sid
    hash[:variables] = @variables
  end

  hash
end

#cache_load(data) ⇒ Object

Organize from cache data



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rails/graphql/request/component/operation/subscription.rb', line 76

def cache_load(data)
  @initial = data[:initial]
  @broadcastable = data[:broadcastable]

  unless subscribing?
    @variables = data[:variables]
    @subscription = data[:sid]
  end

  super
end

#no_update!Object

Either throw back an empty result or skip the operation



47
48
49
# File 'lib/rails/graphql/request/component/operation/subscription.rb', line 47

def no_update!
  subscribing? ? skip! : throw(:skip_subscription_update, EMPTY_HASH)
end

#subscribing?Boolean

Check if the operation is running in its first iteration, which will produce a subscription to the field

Returns:

  • (Boolean)


31
32
33
# File 'lib/rails/graphql/request/component/operation/subscription.rb', line 31

def subscribing?
  @initial
end

#subscriptionObject

Fetch the subscription only when necessary



23
24
25
26
27
# File 'lib/rails/graphql/request/component/operation/subscription.rb', line 23

def subscription
  return unless defined?(@subscription)
  return @subscription if @subscription.is_a?(Request::Subscription)
  @subscription = schema.subscription_provider.fetch(@subscription)
end

#unsubscribe!Object

If unsubscribe is called during an update, skip and return a proper result



53
54
55
56
57
58
59
# File 'lib/rails/graphql/request/component/operation/subscription.rb', line 53

def unsubscribe!
  if subscribing?
    schema.remove_subscriptions(subscription.sid) if subscription.present?
  else
    throw(:skip_subscription_update, UNSUBSCRIBED_RESULT)
  end
end