Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabasJ committed Feb 6, 2024
1 parent 6546e69 commit 82f3d90
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 32 deletions.
2 changes: 0 additions & 2 deletions lib/graphql/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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] || %{}
Expand Down
4 changes: 2 additions & 2 deletions lib/resource/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -980,7 +981,6 @@ defmodule AshGraphql.Resource do
__reference__: ref(__ENV__)
}
end)
|> dbg()
end

@doc false
Expand Down
5 changes: 4 additions & 1 deletion lib/resource/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
3 changes: 0 additions & 3 deletions lib/resource/subscription/default_resolve.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions lib/resource/subscription/notifier.ex
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions lib/resource/transformers/subscription.ex
Original file line number Diff line number Diff line change
@@ -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
11 changes: 7 additions & 4 deletions test/support/resources/subscribable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ defmodule AshGraphql.Test.Subscribable do

require Ash.Query

resource do
simple_notifiers([AshGraphql.Resource.Notifier])
end

graphql do
type :subscribable

Expand All @@ -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
Expand Down
20 changes: 0 additions & 20 deletions test/support/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 82f3d90

Please sign in to comment.