mirror of
https://github.com/ash-project/ash_graphql.git
synced 2024-09-20 21:32:59 +12:00
wip
This commit is contained in:
parent
57d87cfe92
commit
0683d25b79
6 changed files with 99 additions and 27 deletions
|
@ -13,7 +13,7 @@ defmodule AshGraphql.SubscriptionTest do
|
||||||
|
|
||||||
@query """
|
@query """
|
||||||
subscription {
|
subscription {
|
||||||
post_created { id }
|
subscribableCreated { id }
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
@tag :wip
|
@tag :wip
|
||||||
|
@ -28,12 +28,13 @@ defmodule AshGraphql.SubscriptionTest do
|
||||||
context: %{pubsub: PubSub, actor: %{id: id}}
|
context: %{pubsub: PubSub, actor: %{id: id}}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
PubSub.subscribe("subscribable:created")
|
||||||
|
|
||||||
mutation = """
|
mutation = """
|
||||||
mutation SimpleCreatePost($input: SimpleCreatePostInput) {
|
mutation CreateSubscribable($input: CreateSubscribableInput) {
|
||||||
simpleCreatePost(input: $input) {
|
createSubscribable(input: $input) {
|
||||||
result{
|
result{
|
||||||
text1
|
text
|
||||||
integerAsStringInApi
|
|
||||||
}
|
}
|
||||||
errors{
|
errors{
|
||||||
message
|
message
|
||||||
|
@ -42,14 +43,17 @@ defmodule AshGraphql.SubscriptionTest do
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
assert {:ok, %{data: _}} =
|
assert {:ok, %{data: data}} =
|
||||||
run_subscription(mutation, Schema,
|
run_subscription(mutation, Schema,
|
||||||
variables: %{"input" => %{"text1" => "foo", "integerAsStringInApi" => "1"}},
|
variables: %{"input" => %{"text" => "foo"}},
|
||||||
context: %{pubsub: PubSub}
|
context: %{pubsub: PubSub}
|
||||||
)
|
)
|
||||||
|
|
||||||
assert_receive({:broadcast, msg})
|
assert_receive({:broadcast, msg})
|
||||||
|
|
||||||
|
Absinthe.Subscription.publish(PubSub, data, subscribable_created: nil)
|
||||||
|
|> IO.inspect(label: :publish)
|
||||||
|
|
||||||
assert %{
|
assert %{
|
||||||
event: "subscription:data",
|
event: "subscription:data",
|
||||||
result: %{data: %{"user" => %{"id" => "1", "name" => "foo"}}},
|
result: %{data: %{"user" => %{"id" => "1", "name" => "foo"}}},
|
||||||
|
@ -60,7 +64,7 @@ defmodule AshGraphql.SubscriptionTest do
|
||||||
defp run_subscription(query, schema, opts) do
|
defp run_subscription(query, schema, opts) do
|
||||||
opts = Keyword.update(opts, :context, %{pubsub: PubSub}, &Map.put(&1, :pubsub, PubSub))
|
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 ->
|
{:ok, %{"subscribed" => topic}} = val ->
|
||||||
PubSub.subscribe(topic)
|
PubSub.subscribe(topic)
|
||||||
val
|
val
|
||||||
|
|
|
@ -10,16 +10,19 @@ defmodule AshGraphql.Test.PubSub do
|
||||||
end
|
end
|
||||||
|
|
||||||
def subscribe(topic) do
|
def subscribe(topic) do
|
||||||
Registry.register(__MODULE__, topic, [])
|
IO.inspect([topic: topic], label: "subscribe")
|
||||||
|
Registry.register(__MODULE__, topic, [self()])
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
def publish_subscription(topic, data) do
|
def publish_subscription(topic, data) do
|
||||||
message = %{
|
message =
|
||||||
|
%{
|
||||||
topic: topic,
|
topic: topic,
|
||||||
event: "subscription:data",
|
event: "subscription:data",
|
||||||
result: data
|
result: data
|
||||||
}
|
}
|
||||||
|
|> IO.inspect(label: :publish_subscription)
|
||||||
|
|
||||||
Registry.dispatch(__MODULE__, topic, fn entries ->
|
Registry.dispatch(__MODULE__, topic, fn entries ->
|
||||||
for {pid, _} <- entries, do: send(pid, {:broadcast, message})
|
for {pid, _} <- entries, do: send(pid, {:broadcast, message})
|
||||||
|
@ -27,13 +30,14 @@ defmodule AshGraphql.Test.PubSub do
|
||||||
end
|
end
|
||||||
|
|
||||||
def broadcast(topic, event, notification) do
|
def broadcast(topic, event, notification) do
|
||||||
|
IO.inspect([topic: topic, event: event, notification: notification], label: "broadcast")
|
||||||
|
|
||||||
message =
|
message =
|
||||||
%{
|
%{
|
||||||
topic: topic,
|
topic: topic,
|
||||||
event: event,
|
event: event,
|
||||||
result: notification
|
result: notification
|
||||||
}
|
}
|
||||||
|> IO.inspect(label: :message)
|
|
||||||
|
|
||||||
Registry.dispatch(__MODULE__, topic, fn entries ->
|
Registry.dispatch(__MODULE__, topic, fn entries ->
|
||||||
for {pid, _} <- entries, do: send(pid, {:broadcast, message})
|
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
|
def publish_mutation(_proxy_topic, _mutation_result, _subscribed_fields) do
|
||||||
# this pubsub is local and doesn't support clusters
|
# this pubsub is local and doesn't support clusters
|
||||||
|
IO.inspect("publish mutation")
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
29
test/support/registry.ex
Normal file
29
test/support/registry.ex
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
defmodule AshGraphql.Test.Registry do
|
||||||
|
@moduledoc false
|
||||||
|
use Ash.Registry
|
||||||
|
|
||||||
|
entries do
|
||||||
|
entry(AshGraphql.Test.Comment)
|
||||||
|
entry(AshGraphql.Test.CompositePrimaryKey)
|
||||||
|
entry(AshGraphql.Test.CompositePrimaryKeyNotEncoded)
|
||||||
|
entry(AshGraphql.Test.DoubleRelRecursive)
|
||||||
|
entry(AshGraphql.Test.DoubleRelToRecursiveParentOfEmbed)
|
||||||
|
entry(AshGraphql.Test.MapTypes)
|
||||||
|
entry(AshGraphql.Test.MultitenantPostTag)
|
||||||
|
entry(AshGraphql.Test.MultitenantTag)
|
||||||
|
entry(AshGraphql.Test.NoObject)
|
||||||
|
entry(AshGraphql.Test.NonIdPrimaryKey)
|
||||||
|
entry(AshGraphql.Test.Post)
|
||||||
|
entry(AshGraphql.Test.PostTag)
|
||||||
|
entry(AshGraphql.Test.RelayPostTag)
|
||||||
|
entry(AshGraphql.Test.RelayTag)
|
||||||
|
entry(AshGraphql.Test.SponsoredComment)
|
||||||
|
entry(AshGraphql.Test.Subscribable)
|
||||||
|
entry(AshGraphql.Test.Tag)
|
||||||
|
entry(AshGraphql.Test.User)
|
||||||
|
entry(AshGraphql.Test.Channel)
|
||||||
|
entry(AshGraphql.Test.Message)
|
||||||
|
entry(AshGraphql.Test.TextMessage)
|
||||||
|
entry(AshGraphql.Test.ImageMessage)
|
||||||
|
end
|
||||||
|
end
|
|
@ -131,13 +131,12 @@ defmodule AshGraphql.Test.Post do
|
||||||
alias AshGraphql.Test.CommonMap
|
alias AshGraphql.Test.CommonMap
|
||||||
alias AshGraphql.Test.CommonMapStruct
|
alias AshGraphql.Test.CommonMapStruct
|
||||||
alias AshGraphql.Test.SponsoredComment
|
alias AshGraphql.Test.SponsoredComment
|
||||||
alias AshGraphql.Test.PubSub
|
|
||||||
|
|
||||||
use Ash.Resource,
|
use Ash.Resource,
|
||||||
domain: AshGraphql.Test.Domain,
|
domain: AshGraphql.Test.Domain,
|
||||||
data_layer: Ash.DataLayer.Ets,
|
data_layer: Ash.DataLayer.Ets,
|
||||||
authorizers: [Ash.Policy.Authorizer],
|
authorizers: [Ash.Policy.Authorizer],
|
||||||
extensions: [AshGraphql.Resource, Ash.Notifier.PubSub]
|
extensions: [AshGraphql.Resource]
|
||||||
|
|
||||||
require Ash.Query
|
require Ash.Query
|
||||||
|
|
||||||
|
@ -233,14 +232,6 @@ defmodule AshGraphql.Test.Post do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
pub_sub do
|
|
||||||
module(PubSub)
|
|
||||||
prefix("post")
|
|
||||||
broadcast_type(:notification)
|
|
||||||
|
|
||||||
publish_all(:create, "created")
|
|
||||||
end
|
|
||||||
|
|
||||||
actions do
|
actions do
|
||||||
default_accept(:*)
|
default_accept(:*)
|
||||||
|
|
||||||
|
|
43
test/support/resources/subscribable.ex
Normal file
43
test/support/resources/subscribable.ex
Normal file
|
@ -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
|
|
@ -33,10 +33,10 @@ defmodule AshGraphql.Test.Schema do
|
||||||
end
|
end
|
||||||
|
|
||||||
subscription do
|
subscription do
|
||||||
field :post_created, :post do
|
field :subscribable_created, :subscribable do
|
||||||
config(fn
|
config(fn
|
||||||
_args, %{context: %{actor: %{id: user_id}}} ->
|
_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 ->
|
_args, _context ->
|
||||||
{:error, :unauthorized}
|
{:error, :unauthorized}
|
||||||
|
|
Loading…
Reference in a new issue