Class: GraphQL::Subscriptions
- Inherits:
-
Object
- Object
- GraphQL::Subscriptions
- Defined in:
- lib/graphql/subscriptions.rb,
lib/graphql/subscriptions/event.rb,
lib/graphql/subscriptions/instrumentation.rb,
lib/graphql/subscriptions/action_cable_subscriptions.rb
Direct Known Subclasses
Defined Under Namespace
Classes: ActionCableSubscriptions, Event, Instrumentation
Class Method Summary collapse
Instance Method Summary collapse
-
#deliver(subscription_id, result, context) ⇒ void
A subscription query was re-evaluated, returning
result
. -
#each_subscription_id(event) {|subscription_id| ... } ⇒ void
Get each
subscription_id
subscribed toevent.topic
and yield them. -
#execute(subscription_id, event, object) ⇒ void
event
was triggered onobject
, andsubscription_id
was subscribed, so it should be updated. -
#execute_all(event, object) ⇒ void
Event
event
occurred onobject
, Update all subscribers. -
#initialize(schema:, **rest) ⇒ Subscriptions
constructor
A new instance of Subscriptions.
-
#read_subscription(subscription_id) ⇒ Hash
The system wants to send an update to this subscription.
-
#trigger(event_name, args, object, scope: nil) ⇒ void
Fetch subscriptions matching this field + arguments pair And pass them off to the queue.
-
#write_subscription(query, events) ⇒ void
query
was executed and found subscriptions toevents
.
Constructor Details
#initialize(schema:, **rest) ⇒ Subscriptions
Returns a new instance of Subscriptions.
20 21 22 |
# File 'lib/graphql/subscriptions.rb', line 20 def initialize(kwargs) @schema = kwargs[:schema] end |
Class Method Details
.use(defn, options = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/graphql/subscriptions.rb', line 10 def self.use(defn, = {}) schema = defn.target [:schema] = schema schema.subscriptions = self.new() instrumentation = Subscriptions::Instrumentation.new(schema: schema) defn.instrument(:field, instrumentation) defn.instrument(:query, instrumentation) nil end |
Instance Method Details
#deliver(subscription_id, result, context) ⇒ void
This method returns an undefined value.
A subscription query was re-evaluated, returning result
.
The result should be send to subscription_id
.
116 117 118 |
# File 'lib/graphql/subscriptions.rb', line 116 def deliver(subscription_id, result, context) raise NotImplementedError end |
#each_subscription_id(event) {|subscription_id| ... } ⇒ void
This method returns an undefined value.
Get each subscription_id
subscribed to event.topic
and yield them
98 99 100 |
# File 'lib/graphql/subscriptions.rb', line 98 def each_subscription_id(event) raise NotImplementedError end |
#execute(subscription_id, event, object) ⇒ void
This method returns an undefined value.
event
was triggered on object
, and subscription_id
was subscribed,
so it should be updated.
Load subscription_id
's GraphQL data, re-evaluate the query, and deliver the result.
This is where a queue may be inserted to push updates in the background.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/graphql/subscriptions.rb', line 61 def execute(subscription_id, event, object) # Lookup the saved data for this subscription query_data = read_subscription(subscription_id) # Fetch the required keys from the saved data query_string = query_data.fetch(:query_string) variables = query_data.fetch(:variables) context = query_data.fetch(:context) operation_name = query_data.fetch(:operation_name) # Re-evaluate the saved query result = @schema.execute( { query: query_string, context: context, subscription_topic: event.topic, operation_name: operation_name, variables: variables, root_value: object, } ) deliver(subscription_id, result) end |
#execute_all(event, object) ⇒ void
This method returns an undefined value.
Event event
occurred on object
,
Update all subscribers.
88 89 90 91 92 |
# File 'lib/graphql/subscriptions.rb', line 88 def execute_all(event, object) each_subscription_id(event) do |subscription_id| execute(subscription_id, event, object) end end |
#read_subscription(subscription_id) ⇒ Hash
The system wants to send an update to this subscription. Read its data and return it.
106 107 108 |
# File 'lib/graphql/subscriptions.rb', line 106 def read_subscription(subscription_id) raise NotImplementedError end |
#trigger(event_name, args, object, scope: nil) ⇒ void
This method returns an undefined value.
Fetch subscriptions matching this field + arguments pair And pass them off to the queue.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/graphql/subscriptions.rb', line 31 def trigger(event_name, args, object, scope: nil) field = @schema.get_field("Subscription", event_name) if !field raise "No subscription matching trigger: #{event_name}" end event = Subscriptions::Event.new( name: event_name, arguments: args, field: field, scope: scope, ) execute_all(event, object) end |
#write_subscription(query, events) ⇒ void
This method returns an undefined value.
query
was executed and found subscriptions to events
.
Update the database to reflect this new state.
125 126 127 |
# File 'lib/graphql/subscriptions.rb', line 125 def write_subscription(query, events) raise NotImplementedError end |