diff --git a/lib/resource/info.ex b/lib/resource/info.ex index 8bc8e31a..b27c88cf 100644 --- a/lib/resource/info.ex +++ b/lib/resource/info.ex @@ -18,6 +18,10 @@ defmodule AshGraphql.Resource.Info do Extension.get_entities(resource, [:graphql, :subscriptions]) || [] end + def subscription_pubsub(resource) do + Extension.get_opt(resource, [:graphql, :subscriptions], :pubsub) + end + @doc "Wether or not to encode the primary key as a single `id` field when reading and getting" def encode_primary_key?(resource) do Extension.get_opt(resource, [:graphql], :encode_primary_key?, true) diff --git a/lib/resource/notifier.ex b/lib/resource/notifier.ex deleted file mode 100644 index 96f41644..00000000 --- a/lib/resource/notifier.ex +++ /dev/null @@ -1,12 +0,0 @@ -defmodule AshGraphql.Resource.Notifier do - use Ash.Notifier - - @impl Ash.Notifier - def notify(notification) do - IO.inspect(notification, label: :Notifier) - - Absinthe.Subscription.publish(AshGraphql.Test.PubSub, notification.data, - subscrible_created: "*" - ) - end -end diff --git a/lib/resource/resource.ex b/lib/resource/resource.ex index 800e796e..1e3afa8c 100644 --- a/lib/resource/resource.ex +++ b/lib/resource/resource.ex @@ -251,7 +251,7 @@ defmodule AshGraphql.Resource do @subscribe %Spark.Dsl.Entity{ name: :subscribe, - args: [:name, :config], + args: [:name], describe: "A query to fetch a record by primary key", examples: [ "get :get_post, :read" @@ -262,6 +262,13 @@ defmodule AshGraphql.Resource do @subscriptions %Spark.Dsl.Section{ name: :subscriptions, + schema: [ + pubsub: [ + type: :module, + required: true, + doc: "The pubsub module to use for the subscription" + ] + ], describe: """ Subscriptions (notifications) to expose for the resource. """, @@ -1000,10 +1007,12 @@ defmodule AshGraphql.Resource do resource |> subscriptions() |> Enum.map(fn %Subscription{name: name, config: config} = subscription -> + dbg(config) + %Absinthe.Blueprint.Schema.FieldDefinition{ identifier: name, name: to_string(name), - config: config, + config: &AshGraphql.Resource.Subscription.DefaultConfig.config/2, module: schema, middleware: action_middleware ++ diff --git a/lib/resource/subscription.ex b/lib/resource/subscription.ex index c9054604..79d38fd3 100644 --- a/lib/resource/subscription.ex +++ b/lib/resource/subscription.ex @@ -18,7 +18,8 @@ defmodule AshGraphql.Resource.Subscription do type: {:mfa_or_fun, 2}, doc: """ Function that creates the config for the subscription - """ + """, + default: AshGraphql.Resource.Subscription.DefaultConfig ], resolve: [ type: {:mfa_or_fun, 3}, diff --git a/lib/resource/subscription/default_config.ex b/lib/resource/subscription/default_config.ex new file mode 100644 index 00000000..7af5e67e --- /dev/null +++ b/lib/resource/subscription/default_config.ex @@ -0,0 +1,3 @@ +defmodule AshGraphql.Resource.Subscription.DefaultConfig do + def config(_, _), do: dbg({:ok, topic: "*"}) +end diff --git a/lib/resource/subscription/notifier.ex b/lib/resource/subscription/notifier.ex index fd5d350f..1404eecc 100644 --- a/lib/resource/subscription/notifier.ex +++ b/lib/resource/subscription/notifier.ex @@ -1,12 +1,13 @@ -defmodule AshGraphq.Resource.Subscription.Notifier do +defmodule AshGraphql.Resource.Subscription.Notifier do + alias AshGraphql.Resource.Info use Ash.Notifier @impl Ash.Notifier def notify(notification) do - IO.inspect(notification, label: :Notifier) + pub_sub = Info.subscription_pubsub(notification.resource) - Absinthe.Subscription.publish(AshGraphql.Test.PubSub, notification.data, - subscrible_created: "*" - ) + for subscription <- AshGraphql.Resource.Info.subscriptions(notification.resource) do + Absinthe.Subscription.publish(pub_sub, notification.data, [{subscription.name, "*"}]) + end end end diff --git a/lib/resource/transformers/subscription.ex b/lib/resource/transformers/subscription.ex index d43686b5..2577002c 100644 --- a/lib/resource/transformers/subscription.ex +++ b/lib/resource/transformers/subscription.ex @@ -8,8 +8,7 @@ defmodule AshGraphql.Resource.Transformers.Subscription do alias Spark.Dsl.Transformer def transform(dsl) do - case dsl - |> Transformer.get_entities([:graphql, :subscriptions]) do + case dsl |> Transformer.get_entities([:graphql, :subscriptions]) do [] -> {:ok, dsl} @@ -20,13 +19,10 @@ defmodule AshGraphql.Resource.Transformers.Subscription do [:resource], :simple_notifiers, [ - AshGraphq.Resource.Subscription.Notifier + AshGraphql.Resource.Subscription.Notifier ] ++ Transformer.get_option(dsl, [:resource], :simple_notifiers, []) - ) - |> dbg()} + )} end - - {:ok, dsl} end end