Class: GraphQL::Subscriptions::Event
- Inherits:
-
Object
- Object
- GraphQL::Subscriptions::Event
- Defined in:
- lib/graphql/subscriptions/event.rb
Overview
This thing can be:
- Subscribed to by
subscription { ... } - Triggered by
MySchema.subscriber.trigger(name, arguments, obj)
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns.
-
#context ⇒ Object
readonly
Returns.
-
#name ⇒ Object
readonly
Returns.
-
#topic ⇒ Object
readonly
Returns.
Class Method Summary collapse
- .arguments_without_field_extras(arguments:, field:) ⇒ Object
-
.serialize(_name, arguments, field, scope:, context: GraphQL::Query::NullContext.instance) ⇒ Object
Returns.
Instance Method Summary collapse
-
#fingerprint ⇒ Object
Returns.
-
#initialize(name:, arguments:, field: nil, context: nil, scope: nil) ⇒ Event
constructor
A new instance of Event.
Constructor Details
#initialize(name:, arguments:, field: nil, context: nil, scope: nil) ⇒ Event
Returns a new instance of Event.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/graphql/subscriptions/event.rb', line 31 def initialize(name:, arguments:, field: nil, context: nil, scope: nil) @name = name @arguments = self.class.arguments_without_field_extras(arguments: arguments, field: field) @context = context field ||= context.field scope_key = field.subscription_scope scope_val = scope || (context && scope_key && context[scope_key]) if scope_key && (subscription = field.resolver) && (subscription.respond_to?(:subscription_scope_optional?)) && !subscription.subscription_scope_optional? && scope_val.nil? raise Subscriptions::SubscriptionScopeMissingError, "#{field.path} (#{subscription}) requires a `scope:` value to trigger updates (Set `subscription_scope ..., optional: true` to disable this requirement)" end @topic = self.class.serialize(name, arguments, field, scope: scope_val, context: context) end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns
GraphQL::Execution::Interpreter::Arguments
19 20 21 |
# File 'lib/graphql/subscriptions/event.rb', line 19 def arguments @arguments end |
#context ⇒ Object (readonly)
Returns
GraphQL::Query::Context
24 25 26 |
# File 'lib/graphql/subscriptions/event.rb', line 24 def context @context end |
#name ⇒ Object (readonly)
Returns
String— Corresponds to the Subscription root field name
14 15 16 |
# File 'lib/graphql/subscriptions/event.rb', line 14 def name @name end |
#topic ⇒ Object (readonly)
Returns
String— An opaque string which identifies this event, derived fromnameandarguments
29 30 31 |
# File 'lib/graphql/subscriptions/event.rb', line 29 def topic @topic end |
Class Method Details
.arguments_without_field_extras(arguments:, field:) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/graphql/subscriptions/event.rb', line 84 def arguments_without_field_extras(arguments:, field:) if !field.extras.empty? arguments = arguments.dup field.extras.each do |extra_key| arguments.delete(extra_key) end end arguments end |
.serialize(_name, arguments, field, scope:, context: GraphQL::Query::NullContext.instance) ⇒ Object
Returns
String— an identifier for this unit of subscription
:call-seq:
serialize(_name, arguments, field, scope:, context:) -> String
55 56 57 58 59 60 |
# File 'lib/graphql/subscriptions/event.rb', line 55 def self.serialize(_name, arguments, field, scope:, context: GraphQL::Query::NullContext.instance) subscription = field.resolver || GraphQL::Schema::Subscription arguments = arguments_without_field_extras(field: field, arguments: arguments) normalized_args = stringify_args(field, arguments.to_h, context) subscription.topic_for(arguments: normalized_args, field: field, scope: scope) end |
Instance Method Details
#fingerprint ⇒ Object
Returns
String— a logical identifier for this event. (Stable when the query is broadcastable.)
:call-seq:
fingerprint() -> String
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/graphql/subscriptions/event.rb', line 68 def fingerprint @fingerprint ||= begin # When this query has been flagged as broadcastable, # use a generalized, stable fingerprint so that # duplicate subscriptions can be evaluated and distributed in bulk. # (`@topic` includes field, args, and subscription scope already.) if @context.namespace(:subscriptions)[:subscription_broadcastable] "#{@topic}/#{@context.query.fingerprint}" else # not broadcastable, build a unique ID for this event @context.schema.subscriptions.build_id end end end |