Skip to content

Commit

Permalink
subscribed and recieved data in playground
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabasJ committed Feb 20, 2024
1 parent 1bc09a5 commit 3b6bbde
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 27 deletions.
4 changes: 4 additions & 0 deletions lib/resource/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 0 additions & 12 deletions lib/resource/notifier.ex

This file was deleted.

13 changes: 11 additions & 2 deletions lib/resource/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
""",
Expand Down Expand Up @@ -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 ++
Expand Down
3 changes: 2 additions & 1 deletion lib/resource/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
3 changes: 3 additions & 0 deletions lib/resource/subscription/default_config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule AshGraphql.Resource.Subscription.DefaultConfig do
def config(_, _), do: dbg({:ok, topic: "*"})
end
11 changes: 6 additions & 5 deletions lib/resource/subscription/notifier.ex
Original file line number Diff line number Diff line change
@@ -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
10 changes: 3 additions & 7 deletions lib/resource/transformers/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -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

0 comments on commit 3b6bbde

Please sign in to comment.