ash_graphql/test/subscription_test.exs

77 lines
1.8 KiB
Elixir
Raw Normal View History

2023-10-15 00:08:21 +13:00
defmodule AshGraphql.SubscriptionTest do
use ExUnit.Case
2023-10-16 00:09:30 +13:00
alias AshGraphql.Test.PubSub
alias AshGraphql.Test.Schema
2023-10-15 00:08:21 +13:00
setup_all do
2023-10-16 00:09:30 +13:00
Application.put_env(PubSub, :notifier_test_pid, self())
2023-10-15 00:08:21 +13:00
{:ok, _} = PubSub.start_link()
{:ok, _} = Absinthe.Subscription.start_link(PubSub)
:ok
end
@query """
subscription {
2023-10-28 01:50:51 +13:00
subscribableCreated { id }
2023-10-15 00:08:21 +13:00
}
"""
2023-10-16 00:09:30 +13:00
@tag :wip
2023-10-15 00:08:21 +13:00
test "subscription triggers work" do
id = "1"
assert {:ok, %{"subscribed" => topic}} =
run_subscription(
@query,
Schema,
variables: %{"userId" => id},
2023-10-16 00:09:30 +13:00
context: %{pubsub: PubSub, actor: %{id: id}}
2023-10-15 00:08:21 +13:00
)
2023-10-28 01:50:51 +13:00
PubSub.subscribe("subscribable:created")
2023-10-15 00:08:21 +13:00
mutation = """
2023-10-28 01:50:51 +13:00
mutation CreateSubscribable($input: CreateSubscribableInput) {
createSubscribable(input: $input) {
2023-10-16 00:09:30 +13:00
result{
2023-10-28 01:50:51 +13:00
text
2023-10-16 00:09:30 +13:00
}
errors{
message
}
}
}
2023-10-15 00:08:21 +13:00
"""
2023-10-28 01:50:51 +13:00
assert {:ok, %{data: data}} =
2023-10-15 00:08:21 +13:00
run_subscription(mutation, Schema,
2023-10-28 01:50:51 +13:00
variables: %{"input" => %{"text" => "foo"}},
2023-10-15 00:08:21 +13:00
context: %{pubsub: PubSub}
)
assert_receive({:broadcast, msg})
2023-10-28 01:50:51 +13:00
Absinthe.Subscription.publish(PubSub, data, subscribable_created: nil)
|> IO.inspect(label: :publish)
2023-10-15 00:08:21 +13:00
assert %{
event: "subscription:data",
result: %{data: %{"user" => %{"id" => "1", "name" => "foo"}}},
topic: topic
} == msg
end
2023-10-16 00:09:30 +13:00
defp run_subscription(query, schema, opts) do
2023-10-15 00:08:21 +13:00
opts = Keyword.update(opts, :context, %{pubsub: PubSub}, &Map.put(&1, :pubsub, PubSub))
2023-10-28 01:50:51 +13:00
case Absinthe.run(query, schema, opts) |> IO.inspect(label: :absinthe_run) do
2023-10-15 00:08:21 +13:00
{:ok, %{"subscribed" => topic}} = val ->
PubSub.subscribe(topic)
val
val ->
val
end
end
end