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 """
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
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.CommonMapStruct
|
||||
alias AshGraphql.Test.SponsoredComment
|
||||
alias AshGraphql.Test.PubSub
|
||||
|
||||
use Ash.Resource,
|
||||
domain: AshGraphql.Test.Domain,
|
||||
data_layer: Ash.DataLayer.Ets,
|
||||
authorizers: [Ash.Policy.Authorizer],
|
||||
extensions: [AshGraphql.Resource, Ash.Notifier.PubSub]
|
||||
extensions: [AshGraphql.Resource]
|
||||
|
||||
require Ash.Query
|
||||
|
||||
|
@ -233,14 +232,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
|
||||
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
|
||||
|
||||
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}
|
||||
|
|
Loading…
Reference in a new issue