Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabasJ committed Oct 27, 2023
1 parent e9376f8 commit bb1615b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 27 deletions.
20 changes: 12 additions & 8 deletions test/subscription_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule AshGraphql.SubscriptionTest do

@query """
subscription {
post_created { id }
subscribableCreated { id }
}
"""
@tag :wip
Expand All @@ -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
Expand All @@ -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"}}},
Expand All @@ -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
Expand Down
19 changes: 12 additions & 7 deletions test/support/pub_sub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,34 @@ 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})
end)
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})
Expand All @@ -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
1 change: 1 addition & 0 deletions test/support/registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 1 addition & 10 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
43 changes: 43 additions & 0 deletions test/support/resources/subscribable.ex
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions test/support/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit bb1615b

Please sign in to comment.