Class: GraphQL::Subscriptions::ActionCableSubscriptions
- Inherits:
-
GraphQL::Subscriptions
- Object
- GraphQL::Subscriptions
- GraphQL::Subscriptions::ActionCableSubscriptions
- Defined in:
- lib/graphql/subscriptions/action_cable_subscriptions.rb
Overview
A subscriptions implementation that sends data as ActionCable broadcastings.
Experimental, some things to keep in mind:
- No queueing system; ActiveJob should be added
- Take care to reload context when re-delivering the subscription. (see Query#subscription_update?)
Constant Summary collapse
- SUBSCRIPTION_PREFIX =
"graphql-subscription:"
- EVENT_PREFIX =
"graphql-event:"
Instance Method Summary collapse
-
#delete_subscription(subscription_id) ⇒ Object
The channel was closed, forget about it.
-
#deliver(subscription_id, result) ⇒ Object
This subscription was re-evaluated.
-
#execute_all(event, object) ⇒ Object
An event was triggered; Push the data over ActionCable.
-
#initialize(**rest) ⇒ ActionCableSubscriptions
constructor
A new instance of ActionCableSubscriptions.
-
#read_subscription(subscription_id) ⇒ Object
Return the query from "storage" (in memory).
-
#write_subscription(query, events) ⇒ Object
A query was run where these events were subscribed to.
Methods inherited from GraphQL::Subscriptions
#each_subscription_id, #execute, #trigger, use
Constructor Details
#initialize(**rest) ⇒ ActionCableSubscriptions
Returns a new instance of ActionCableSubscriptions.
67 68 69 70 71 72 |
# File 'lib/graphql/subscriptions/action_cable_subscriptions.rb', line 67 def initialize(**rest) # A per-process map of subscriptions to deliver. # This is provided by Rails, so let's use it @subscriptions = Concurrent::Map.new super end |
Instance Method Details
#delete_subscription(subscription_id) ⇒ Object
The channel was closed, forget about it.
117 118 119 |
# File 'lib/graphql/subscriptions/action_cable_subscriptions.rb', line 117 def delete_subscription(subscription_id) @subscriptions.delete(subscription_id) end |
#deliver(subscription_id, result) ⇒ Object
This subscription was re-evaluated. Send it to the specific stream where this client was waiting.
83 84 85 86 |
# File 'lib/graphql/subscriptions/action_cable_subscriptions.rb', line 83 def deliver(subscription_id, result) payload = { result: result.to_h, more: true } ActionCable.server.broadcast(SUBSCRIPTION_PREFIX + subscription_id, payload) end |
#execute_all(event, object) ⇒ Object
An event was triggered; Push the data over ActionCable. Subscribers will re-evaluate locally. TODO: this method name is a smell
77 78 79 |
# File 'lib/graphql/subscriptions/action_cable_subscriptions.rb', line 77 def execute_all(event, object) ActionCable.server.broadcast(EVENT_PREFIX + event.topic, object) end |
#read_subscription(subscription_id) ⇒ Object
Return the query from "storage" (in memory)
106 107 108 109 110 111 112 113 114 |
# File 'lib/graphql/subscriptions/action_cable_subscriptions.rb', line 106 def read_subscription(subscription_id) query = @subscriptions[subscription_id] { query_string: query.query_string, variables: query.provided_variables, context: query.context.to_h, operation_name: query.operation_name, } end |
#write_subscription(query, events) ⇒ Object
A query was run where these events were subscribed to. Store them in memory in this ActionCable frontend. It will receive notifications when events come in and re-evaluate the query locally.
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/graphql/subscriptions/action_cable_subscriptions.rb', line 92 def write_subscription(query, events) channel = query.context[:channel] subscription_id = query.context[:subscription_id] ||= SecureRandom.uuid channel.stream_from(SUBSCRIPTION_PREFIX + subscription_id) @subscriptions[subscription_id] = query events.each do |event| channel.stream_from(EVENT_PREFIX + event.topic, coder: ActiveSupport::JSON) do || execute(subscription_id, event, ) nil end end end |