Class: GraphQL::Streaming::ActionCableSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/streaming/action_cable_subscriber.rb

Overview

A subscriber for a channel-query combo.

Examples:

Subscribe to a query in an ActionCable action, re-evaluating it when things change

# Initialize `context` ahead of time so it can be closed over in the subscription block
context = {}

context[:subscriber] = GraphQLSubscriber.new(self, query_id) do
  # Tell the schema how to re-fetch results:
  context[:current_user].reload
  MySchema.execute(query_string, context: context, variables: variables)
end

# Run the query
MySchema.execute(query_string, context: context, variables: variables)

# detect whether any subscriptions were added
context[:subscriber].subscribed?

Constant Summary collapse

CHANNEL_PREFIX =
"graphql_subscription_"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel, query_id) { ... } ⇒ ActionCableSubscriber

Returns a new instance of ActionCableSubscriber.

Parameters:

  • The (ActionCable::Channel::Base)

    channel to push updates to

  • The (Object)

    query who the updates belong to (probably provided by the client)

Yields:

  • Reruns the query



27
28
29
30
31
32
33
# File 'lib/graphql/streaming/action_cable_subscriber.rb', line 27

def initialize(channel, query_id, &query_exec)
  @channel = channel
  @query_id = query_id
  @query_exec = query_exec
  @subscribed = false
  @own_streams = []
end

Class Method Details

.trigger(subscription_handle, trigger_options = {}) ⇒ Object

Trigger an event with arguments

Examples:

Trigger post_changed

# First, subscribe with "{ post_changed(id: 1) { title } }"
# Then, trigger the event:
GraphQL::Streaming::ActionCableSubscriber.trigger(:post_changed, {id: 1})

Parameters:

  • The (Symbol)

    subscription name to trigger

  • Arguments (Hash)

    to send with the subscription



44
45
46
# File 'lib/graphql/streaming/action_cable_subscriber.rb', line 44

def self.trigger(subscription_handle, trigger_options = {})
  ActionCable.server.broadcast("#{CHANNEL_PREFIX}#{subscription_handle}", trigger_options)
end

Instance Method Details

#closevoid

This method returns an undefined value.

Tell this subscriber to stop sending patches



72
73
74
75
76
77
# File 'lib/graphql/streaming/action_cable_subscriber.rb', line 72

def close
  @channel.stop_specific_streams(@own_streams)
  @own_streams.clear
  @subscribed = false
  nil
end

#register(subscription_handle, arguments) ⇒ Object

Subscribe to event named ‘subscription_handle`, but only when called with arguments `arguments`

Parameters:

  • the (String)

    event name to subscribe to

  • the (Hash)

    arguments to subscribe to



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/graphql/streaming/action_cable_subscriber.rb', line 52

def register(subscription_handle, arguments)
  @subscribed = true
  handle = "#{CHANNEL_PREFIX}#{subscription_handle}"
  original_args = stringify_hash(arguments)

  @own_streams << @channel.stream_from(handle) do |trigger_json|
    trigger_args = JSON.parse(trigger_json)
    if original_args == trigger_args
      reevaluate_query
    end
  end
end

#subscribed?Boolean

Returns True if this subscriber registered any subscriptions.

Returns:

  • (Boolean)

    True if this subscriber registered any subscriptions



66
67
68
# File 'lib/graphql/streaming/action_cable_subscriber.rb', line 66

def subscribed?
  @subscribed
end