Skip to content

Commit

Permalink
improvement: remove unused data in subscription batcher (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabasJ authored Oct 10, 2024
1 parent d5aff24 commit a5b3d6b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
12 changes: 6 additions & 6 deletions lib/graphql/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ defmodule AshGraphql.Graphql.Resolver do

subscription_events =
notifications
|> Enum.group_by(& &1.action.type)
|> Enum.group_by(& &1.action_type)
|> Enum.map(fn {type, notifications} ->
subscription_field = subcription_field_from_action_type(type)
key = String.to_existing_atom(subscription_field)
Expand Down Expand Up @@ -698,7 +698,7 @@ defmodule AshGraphql.Graphql.Resolver do
]

cond do
notification.action.type in [:create, :update] ->
notification.action_type in [:create, :update] ->
data = notification.data
{filter, args} = Map.pop(args, :filter)

Expand All @@ -725,7 +725,7 @@ defmodule AshGraphql.Graphql.Resolver do
domain,
resolution,
subscription_result_type(name),
[subcription_field_from_action_type(notification.action.type)]
[subcription_field_from_action_type(notification.action_type)]
)

result =
Expand Down Expand Up @@ -763,7 +763,7 @@ defmodule AshGraphql.Graphql.Resolver do
{:ok,
%{
String.to_existing_atom(
subcription_field_from_action_type(notification.action.type)
subcription_field_from_action_type(notification.action_type)
) => result
}}
)
Expand All @@ -773,13 +773,13 @@ defmodule AshGraphql.Graphql.Resolver do
|> Absinthe.Resolution.put_result({:error, to_errors([error], context, domain)})
end

notification.action.type in [:destroy] ->
notification.action_type in [:destroy] ->
resolution
|> Absinthe.Resolution.put_result(
{:ok,
%{
String.to_existing_atom(
subcription_field_from_action_type(notification.action.type)
subcription_field_from_action_type(notification.action_type)
) => AshGraphql.Resource.encode_id(notification.data, false)
}}
)
Expand Down
6 changes: 6 additions & 0 deletions lib/subscription/batcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ defmodule AshGraphql.Subscription.Batcher do
end
end

defmodule Notification do
@moduledoc false

defstruct [:action_type, :data]
end

def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, opts, name: opts[:name] || __MODULE__)
end
Expand Down
12 changes: 9 additions & 3 deletions lib/subscription/notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ defmodule AshGraphql.Subscription.Notifier do
@moduledoc """
AshNotifier that triggers absinthe if subscriptions are listening
"""
alias AshGraphql.Resource.Info
use Ash.Notifier

alias AshGraphql.Resource.Info
alias AshGraphql.Subscription.Batcher.Notification

@impl Ash.Notifier
def notify(notification) do
def notify(%Ash.Notifier.Notification{} = notification) do
pub_sub = Info.subscription_pubsub(notification.resource)

for subscription <-
AshGraphql.Resource.Info.subscriptions(notification.resource, notification.domain) do
if notification.action.name in List.wrap(subscription.actions) or
notification.action.type in List.wrap(subscription.action_types) do
Absinthe.Subscription.publish(pub_sub, notification, [{subscription.name, "*"}])
Absinthe.Subscription.publish(
pub_sub,
%Notification{action_type: notification.action.type, data: notification.data},
[{subscription.name, "*"}]
)
end
end

Expand Down

0 comments on commit a5b3d6b

Please sign in to comment.