Class: Rails::GraphQL::Request::Component::Operation::Subscription
- Inherits:
-
Rails::GraphQL::Request::Component::Operation
- Object
- Rails::GraphQL::Request::Component
- Rails::GraphQL::Request::Component::Operation
- Rails::GraphQL::Request::Component::Operation::Subscription
- 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
Instance Attribute Summary
Attributes inherited from Rails::GraphQL::Request::Component::Operation
#arguments, #name, #request, #variables
Attributes included from SelectionSet
Instance Method Summary collapse
-
#broadcastable? ⇒ Boolean
Check if the operation can be broadcasted.
-
#broadcasting? ⇒ Boolean
Check if the current operation is running under broadcasting mode.
-
#cache_dump(initial = true) ⇒ Object
Build the cache object.
-
#cache_load(data) ⇒ Object
Organize from cache data.
-
#initialize ⇒ Subscription
constructor
A new instance of Subscription.
-
#no_update! ⇒ Object
Either throw back an empty result or skip the operation.
-
#subscribing? ⇒ Boolean
Check if the operation is running in its first iteration, which will produce a subscription to the field.
-
#subscription ⇒ Object
Fetch the subscription only when necessary.
-
#unsubscribe! ⇒ Object
If unsubscribe is called during an update, skip and return a proper result.
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
Methods included from Preparable
#prepare!, #prepared_data!, #prepared_data?
Methods included from Organizable
Constructor Details
#initialize ⇒ Subscription
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
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
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
31 32 33 |
# File 'lib/rails/graphql/request/component/operation/subscription.rb', line 31 def subscribing? @initial end |
#subscription ⇒ Object
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 |