Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove unused data in subscription batcher #227

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading