ash_graphql/test/support/pub_sub.ex

53 lines
1.2 KiB
Elixir
Raw Normal View History

2023-10-16 00:09:30 +13:00
defmodule AshGraphql.Test.PubSub do
@behaviour Absinthe.Subscription.Pubsub
def start_link() do
Registry.start_link(keys: :duplicate, name: __MODULE__)
end
def node_name() do
node()
end
def subscribe(topic) do
2023-10-28 01:50:51 +13:00
IO.inspect([topic: topic], label: "subscribe")
Registry.register(__MODULE__, topic, [self()])
2023-10-16 00:09:30 +13:00
:ok
end
def publish_subscription(topic, data) do
2023-10-28 01:50:51 +13:00
message =
%{
topic: topic,
event: "subscription:data",
result: data
}
|> IO.inspect(label: :publish_subscription)
2023-10-16 00:09:30 +13:00
Registry.dispatch(__MODULE__, topic, fn entries ->
for {pid, _} <- entries, do: send(pid, {:broadcast, message})
end)
end
def broadcast(topic, event, notification) do
2023-10-28 01:50:51 +13:00
IO.inspect([topic: topic, event: event, notification: notification], label: "broadcast")
2023-10-16 00:09:30 +13:00
message =
%{
topic: topic,
event: event,
result: notification
}
Registry.dispatch(__MODULE__, topic, fn entries ->
for {pid, _} <- entries, do: send(pid, {:broadcast, message})
end)
end
def publish_mutation(_proxy_topic, _mutation_result, _subscribed_fields) do
# this pubsub is local and doesn't support clusters
2023-10-28 01:50:51 +13:00
IO.inspect("publish mutation")
2023-10-16 00:09:30 +13:00
:ok
end
end