Class: GraphQL::Subscriptions::Event

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, arguments:, field: nil, context: nil, scope: nil) ⇒ Event

Returns a new instance of Event.



22
23
24
25
26
27
28
29
30
# File 'lib/graphql/subscriptions/event.rb', line 22

def initialize(name:, arguments:, field: nil, context: nil, scope: nil)
  @name = name
  @arguments = arguments
  @context = context
  field ||= context.field
  scope_val = scope || (context && field.subscription_scope && context[field.subscription_scope])

  @topic = self.class.serialize(name, arguments, field, scope: scope_val)
end

Instance Attribute Details

#argumentsGraphQL::Query::Arguments (readonly)



14
15
16
# File 'lib/graphql/subscriptions/event.rb', line 14

def arguments
  @arguments
end

#contextGraphQL::Query::Context (readonly)



17
18
19
# File 'lib/graphql/subscriptions/event.rb', line 17

def context
  @context
end

#nameString (readonly)

Returns Corresponds to the Subscription root field name.

Returns:

  • (String)

    Corresponds to the Subscription root field name



11
12
13
# File 'lib/graphql/subscriptions/event.rb', line 11

def name
  @name
end

#topicString (readonly)

Returns An opaque string which identifies this event, derived from name and arguments.

Returns:

  • (String)

    An opaque string which identifies this event, derived from name and arguments



20
21
22
# File 'lib/graphql/subscriptions/event.rb', line 20

def topic
  @topic
end

Class Method Details

.serialize(name, arguments, field, scope:) ⇒ String

Returns an identifier for this unit of subscription.

Returns:

  • (String)

    an identifier for this unit of subscription



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/graphql/subscriptions/event.rb', line 33

def self.serialize(name, arguments, field, scope:)
  normalized_args = case arguments
  when GraphQL::Query::Arguments
    arguments
  when Hash
    if field.is_a?(GraphQL::Schema::Field)
      stringify_args(field, arguments)
    else
      GraphQL::Query::LiteralInput.from_arguments(
        arguments,
        field,
        nil,
      )
    end
  else
    raise ArgumentError, "Unexpected arguments: #{arguments}, must be Hash or GraphQL::Arguments"
  end

  sorted_h = stringify_args(field, normalized_args.to_h)
  Serialize.dump_recursive([scope, name, sorted_h])
end

Instance Method Details

#fingerprintString

Returns a logical identifier for this event. (Stable when the query is broadcastable.).

Returns:

  • (String)

    a logical identifier for this event. (Stable when the query is broadcastable.)



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/graphql/subscriptions/event.rb', line 56

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