diff --git a/test/subscription_test.exs b/test/subscription_test.exs index f4f4b566..add660ca 100644 --- a/test/subscription_test.exs +++ b/test/subscription_test.exs @@ -13,7 +13,7 @@ defmodule AshGraphql.SubscriptionTest do @query """ subscription { - post_created { id } + subscribableCreated { id } } """ @tag :wip @@ -28,12 +28,13 @@ defmodule AshGraphql.SubscriptionTest do context: %{pubsub: PubSub, actor: %{id: id}} ) + PubSub.subscribe("subscribable:created") + mutation = """ - mutation SimpleCreatePost($input: SimpleCreatePostInput) { - simpleCreatePost(input: $input) { + mutation CreateSubscribable($input: CreateSubscribableInput) { + createSubscribable(input: $input) { result{ - text1 - integerAsStringInApi + text } errors{ message @@ -42,14 +43,17 @@ defmodule AshGraphql.SubscriptionTest do } """ - assert {:ok, %{data: _}} = + assert {:ok, %{data: data}} = run_subscription(mutation, Schema, - variables: %{"input" => %{"text1" => "foo", "integerAsStringInApi" => "1"}}, + variables: %{"input" => %{"text" => "foo"}}, context: %{pubsub: PubSub} ) assert_receive({:broadcast, msg}) + Absinthe.Subscription.publish(PubSub, data, subscribable_created: nil) + |> IO.inspect(label: :publish) + assert %{ event: "subscription:data", result: %{data: %{"user" => %{"id" => "1", "name" => "foo"}}}, @@ -60,7 +64,7 @@ defmodule AshGraphql.SubscriptionTest do defp run_subscription(query, schema, opts) do opts = Keyword.update(opts, :context, %{pubsub: PubSub}, &Map.put(&1, :pubsub, PubSub)) - case Absinthe.run(query, schema, opts) do + case Absinthe.run(query, schema, opts) |> IO.inspect(label: :absinthe_run) do {:ok, %{"subscribed" => topic}} = val -> PubSub.subscribe(topic) val diff --git a/test/support/pub_sub.ex b/test/support/pub_sub.ex index aad95a4d..f33a6572 100644 --- a/test/support/pub_sub.ex +++ b/test/support/pub_sub.ex @@ -10,16 +10,19 @@ defmodule AshGraphql.Test.PubSub do end def subscribe(topic) do - Registry.register(__MODULE__, topic, []) + IO.inspect([topic: topic], label: "subscribe") + Registry.register(__MODULE__, topic, [self()]) :ok end def publish_subscription(topic, data) do - message = %{ - topic: topic, - event: "subscription:data", - result: data - } + message = + %{ + topic: topic, + event: "subscription:data", + result: data + } + |> IO.inspect(label: :publish_subscription) Registry.dispatch(__MODULE__, topic, fn entries -> for {pid, _} <- entries, do: send(pid, {:broadcast, message}) @@ -27,13 +30,14 @@ defmodule AshGraphql.Test.PubSub do end def broadcast(topic, event, notification) do + IO.inspect([topic: topic, event: event, notification: notification], label: "broadcast") + message = %{ topic: topic, event: event, result: notification } - |> IO.inspect(label: :message) Registry.dispatch(__MODULE__, topic, fn entries -> for {pid, _} <- entries, do: send(pid, {:broadcast, message}) @@ -42,6 +46,7 @@ defmodule AshGraphql.Test.PubSub do def publish_mutation(_proxy_topic, _mutation_result, _subscribed_fields) do # this pubsub is local and doesn't support clusters + IO.inspect("publish mutation") :ok end end diff --git a/test/support/registry.ex b/test/support/registry.ex index 9649b591..813172b5 100644 --- a/test/support/registry.ex +++ b/test/support/registry.ex @@ -18,6 +18,7 @@ defmodule AshGraphql.Test.Registry do entry(AshGraphql.Test.RelayPostTag) entry(AshGraphql.Test.RelayTag) entry(AshGraphql.Test.SponsoredComment) + entry(AshGraphql.Test.Subscribable) entry(AshGraphql.Test.Tag) entry(AshGraphql.Test.User) end diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index f1299a0e..c76f85b9 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -91,11 +91,10 @@ defmodule AshGraphql.Test.Post do @moduledoc false alias AshGraphql.Test.Comment alias AshGraphql.Test.SponsoredComment - alias AshGraphql.Test.PubSub use Ash.Resource, data_layer: Ash.DataLayer.Ets, - extensions: [AshGraphql.Resource, Ash.Notifier.PubSub] + extensions: [AshGraphql.Resource] require Ash.Query @@ -157,14 +156,6 @@ defmodule AshGraphql.Test.Post do end end - pub_sub do - module(PubSub) - prefix("post") - broadcast_type(:notification) - - publish_all(:create, "created") - end - actions do create :create do primary?(true) diff --git a/test/support/resources/subscribable.ex b/test/support/resources/subscribable.ex new file mode 100644 index 00000000..d5cdc407 --- /dev/null +++ b/test/support/resources/subscribable.ex @@ -0,0 +1,43 @@ +defmodule AshGraphql.Test.Subscribable do + @moduledoc false + alias AshGraphql.Test.PubSub + + use Ash.Resource, + data_layer: Ash.DataLayer.Ets, + notifiers: [Ash.Notifier.PubSub], + extensions: [AshGraphql.Resource] + + require Ash.Query + + graphql do + type :subscribable + + queries do + get :get_subscribable, :read + end + + mutations do + create :create_subscribable, :create + end + end + + pub_sub do + module(PubSub) + prefix("subscribable") + broadcast_type(:notification) + + publish_all(:create, "created") + end + + actions do + defaults([:create, :read, :update, :destroy]) + end + + attributes do + uuid_primary_key(:id) + + attribute(:text, :string) + create_timestamp(:created_at) + update_timestamp(:updated_at) + end +end diff --git a/test/support/schema.ex b/test/support/schema.ex index 758389c0..60ac9394 100644 --- a/test/support/schema.ex +++ b/test/support/schema.ex @@ -33,10 +33,10 @@ defmodule AshGraphql.Test.Schema do end subscription do - field :post_created, :post do + field :subscribable_created, :subscribable do config(fn _args, %{context: %{actor: %{id: user_id}}} -> - {:ok, topic: user_id, context_id: "user/#{user_id}"} + {:ok, topic: "subscribable:created", context_id: "user/#{user_id}"} _args, _context -> {:error, :unauthorized}