Class: NulogyMessageBusProducer::Subscriptions::PostgresTransport

Inherits:
GraphQL::Subscriptions
  • Object
show all
Defined in:
lib/nulogy_message_bus_producer/subscriptions/postgres_transport.rb

Overview

Subscription class to use when developing Message Bus-backed subscriptions For example,

class SomeSchema < GraphQL::Schema
  ...
  use NulogyMessageBusProducer::PostgresPublicSubscriptions
end

It expects that schema to already be registered, or will raise an error:

NulogyMessageBusProducer.register_schema(“some_schema”, “SomeSchema”)

Instance Method Summary collapse

Constructor Details

#initialize(schema:, **_rest) ⇒ PostgresTransport

Returns a new instance of PostgresTransport.



15
16
17
18
# File 'lib/nulogy_message_bus_producer/subscriptions/postgres_transport.rb', line 15

def initialize(schema:, **_rest)
  super
  @schema_key = NulogyMessageBusProducer.resolve_schema_key(schema)
end

Instance Method Details

#delete_subscription(subscription_id) ⇒ Object

TODO: how is this invoked?



65
66
67
# File 'lib/nulogy_message_bus_producer/subscriptions/postgres_transport.rb', line 65

def delete_subscription(subscription_id)
  SelfServeSubscription.find_by(id: subscription_id).destroy
end

#deliver(subscription_id, result) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nulogy_message_bus_producer/subscriptions/postgres_transport.rb', line 38

def deliver(subscription_id, result)
  if result["errors"]&.any?
    NulogyMessageBusProducer.config.handle_event_generation_error(
      subscription_id: subscription_id,
      context: result.query.context,
      variables: result.query.provided_variables,
      result: result
    )
  else
    create_event(subscription_id, result)
  end
end

#execute_all(event, object) ⇒ Object



20
21
22
23
24
# File 'lib/nulogy_message_bus_producer/subscriptions/postgres_transport.rb', line 20

def execute_all(event, object)
  subscription_id = event.arguments.fetch("subscriptionId")

  execute(subscription_id, event, object)
end

#read_subscription(subscription_id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/nulogy_message_bus_producer/subscriptions/postgres_transport.rb', line 26

def read_subscription(subscription_id)
  subscription = find_subscription(subscription_id)
  context = NulogyMessageBusProducer.context_for_subscription(subscription)

  {
    context: context,
    operation_name: nil,
    query_string: subscription.query,
    variables: {}
  }
end

#write_subscription(query, events) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nulogy_message_bus_producer/subscriptions/postgres_transport.rb', line 51

def write_subscription(query, events)
  events.each do |event|
    SelfServeSubscription.create_or_update(
      id: event.arguments.fetch(:subscription_id),
      subscription_group_id: event.arguments.fetch(:subscription_group_id),
      event_type: event.name,
      schema_key: @schema_key,
      query: query.query_string,
      topic_name: event.arguments.fetch(:topic_name)
    )
  end
end