diff --git a/lib/graphql/resolver.ex b/lib/graphql/resolver.ex index 4f8fa1ea..786a84a0 100644 --- a/lib/graphql/resolver.ex +++ b/lib/graphql/resolver.ex @@ -13,8 +13,6 @@ defmodule AshGraphql.Graphql.Resolver do %{arguments: arguments, context: context} = resolution, {api, resource, %AshGraphql.Resource.Action{name: query_name, action: action}, input?} ) do - dbg() - arguments = if input? do arguments[:input] || %{} diff --git a/lib/resource/resource.ex b/lib/resource/resource.ex index 67c54433..84c42a7a 100644 --- a/lib/resource/resource.ex +++ b/lib/resource/resource.ex @@ -391,7 +391,8 @@ defmodule AshGraphql.Resource do @transformers [ AshGraphql.Resource.Transformers.RequireKeysetForRelayQueries, AshGraphql.Resource.Transformers.ValidateActions, - AshGraphql.Resource.Transformers.ValidateCompatibleNames + AshGraphql.Resource.Transformers.ValidateCompatibleNames, + AshGraphql.Resource.Transformers.Subscription ] @verifiers [ @@ -980,7 +981,6 @@ defmodule AshGraphql.Resource do __reference__: ref(__ENV__) } end) - |> dbg() end @doc false diff --git a/lib/resource/subscription.ex b/lib/resource/subscription.ex index 8d74d5c0..c9054604 100644 --- a/lib/resource/subscription.ex +++ b/lib/resource/subscription.ex @@ -2,8 +2,11 @@ defmodule AshGraphql.Resource.Subscription do @moduledoc "Represents a configured query on a resource" defstruct [ :name, + # :arg = filter, :config, - :resolve + :read_action + # :topic, fn _, _ -> {:ok, topic} | :error, + # :trigger fn notification -> {:ok, topics} ] @subscription_schema [ diff --git a/lib/resource/subscription/default_resolve.ex b/lib/resource/subscription/default_resolve.ex index 980481d7..bad0a127 100644 --- a/lib/resource/subscription/default_resolve.ex +++ b/lib/resource/subscription/default_resolve.ex @@ -8,8 +8,6 @@ defmodule AshGraphql.Resource.Subscription.DefaultResolve do %{arguments: arguments, context: context} = resolution, {api, resource, %AshGraphql.Resource.Subscription{}, input?} ) do - dbg() - result = AshGraphql.Subscription.query_for_subscription( resource, @@ -19,7 +17,6 @@ defmodule AshGraphql.Resource.Subscription.DefaultResolve do # |> Ash.Query.filter(id == ^args.id) |> Ash.Query.limit(1) |> api.read_one(actor: resolution.context[:current_user]) - |> IO.inspect() resolution |> Absinthe.Resolution.put_result(result) diff --git a/lib/resource/subscription/notifier.ex b/lib/resource/subscription/notifier.ex new file mode 100644 index 00000000..fd5d350f --- /dev/null +++ b/lib/resource/subscription/notifier.ex @@ -0,0 +1,12 @@ +defmodule AshGraphq.Resource.Subscription.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/transformers/subscription.ex b/lib/resource/transformers/subscription.ex new file mode 100644 index 00000000..d43686b5 --- /dev/null +++ b/lib/resource/transformers/subscription.ex @@ -0,0 +1,32 @@ +defmodule AshGraphql.Resource.Transformers.Subscription do + @moduledoc """ + Adds the notifier for Subscriptions to the Resource + """ + + use Spark.Dsl.Transformer + + alias Spark.Dsl.Transformer + + def transform(dsl) do + case dsl + |> Transformer.get_entities([:graphql, :subscriptions]) do + [] -> + {:ok, dsl} + + _ -> + {:ok, + dsl + |> Transformer.set_option( + [:resource], + :simple_notifiers, + [ + AshGraphq.Resource.Subscription.Notifier + ] ++ + Transformer.get_option(dsl, [:resource], :simple_notifiers, []) + ) + |> dbg()} + end + + {:ok, dsl} + end +end diff --git a/test/support/resources/subscribable.ex b/test/support/resources/subscribable.ex index 5b15e404..cd41ac1f 100644 --- a/test/support/resources/subscribable.ex +++ b/test/support/resources/subscribable.ex @@ -6,10 +6,6 @@ defmodule AshGraphql.Test.Subscribable do require Ash.Query - resource do - simple_notifiers([AshGraphql.Resource.Notifier]) - end - graphql do type :subscribable @@ -20,6 +16,13 @@ defmodule AshGraphql.Test.Subscribable do mutations do create :create_subscribable, :create end + + subscriptions do + subscribe(:subscribable_created, fn _, _ -> + IO.inspect("bucket_created") + {:ok, topic: "*"} + end) + end end actions do diff --git a/test/support/schema.ex b/test/support/schema.ex index 61c8e65a..3b656ae6 100644 --- a/test/support/schema.ex +++ b/test/support/schema.ex @@ -31,24 +31,4 @@ defmodule AshGraphql.Test.Schema do value(:open, description: "The post is open") value(:closed, description: "The post is closed") end - - subscription do - field :subscribable_created, :subscribable do - config(fn - _args, _info -> - {:ok, topic: "*"} - end) - - resolve(fn args, _, resolution -> - # loads all the data you need - AshGraphql.Subscription.query_for_subscription( - Post, - Api, - resolution - ) - |> Ash.Query.filter(id == ^args.id) - |> Api.read(actor: resolution.context.current_user) - end) - end - end end