Skip to content

Commit

Permalink
handle destroys
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabasJ committed Aug 15, 2024
1 parent 02c6682 commit 6bd432b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 37 deletions.
86 changes: 50 additions & 36 deletions lib/graphql/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -537,56 +537,70 @@ defmodule AshGraphql.Graphql.Resolver do
tenant: Map.get(context, :tenant)
]

data = notification.data
cond do
notification.action.type in [:create, :update] ->
data = notification.data

read_action =
read_action || Ash.Resource.Info.primary_action!(resource, :read).name
read_action =
read_action || Ash.Resource.Info.primary_action!(resource, :read).name

query =
Ash.Resource.Info.primary_key(resource)
|> Enum.reduce(resource, fn key, query ->
value = Map.get(data, key)
Ash.Query.filter(query, ^ref(key) == ^value)
end)
query =
Ash.Resource.Info.primary_key(resource)
|> Enum.reduce(resource, fn key, query ->
value = Map.get(data, key)
Ash.Query.filter(query, ^ref(key) == ^value)
end)

query =
Ash.Query.do_filter(
query,
massage_filter(query.resource, Map.get(args, :filter))
)
query =
Ash.Query.do_filter(
query,
massage_filter(query.resource, Map.get(args, :filter))
)

query =
AshGraphql.Subscription.query_for_subscription(
query
|> Ash.Query.for_read(read_action, %{}, opts),
domain,
resolution,
subscription_result_type(name),
[subcription_field_from_action_type(notification.action.type)]
)
query =
AshGraphql.Subscription.query_for_subscription(
query
|> Ash.Query.for_read(read_action, %{}, opts),
domain,
resolution,
subscription_result_type(name),
[subcription_field_from_action_type(notification.action.type)]
)

case query |> Ash.read_one() do
# should only happen if a resource is created/updated and the subscribed user is not allowed to see it
{:ok, nil} ->
resolution
|> Absinthe.Resolution.put_result(
{:error, to_errors([Ash.Error.Query.NotFound.exception()], context, domain)}
)
case query |> Ash.read_one() do
# should only happen if a resource is created/updated and the subscribed user is not allowed to see it
{:ok, nil} ->
resolution
|> Absinthe.Resolution.put_result(
{:error, to_errors([Ash.Error.Query.NotFound.exception()], context, domain)}
)

{:ok, result} ->
resolution
|> Absinthe.Resolution.put_result(
{:ok,
%{
String.to_existing_atom(
subcription_field_from_action_type(notification.action.type)
) => result
}}
)

{:ok, result} ->
{:error, error} ->
resolution
|> Absinthe.Resolution.put_result({:error, to_errors([error], context, domain)})
end

notification.action.type in [:destroy] ->
resolution
|> Absinthe.Resolution.put_result(
{:ok,
%{
String.to_existing_atom(
subcription_field_from_action_type(notification.action.type)
) => result
) => AshGraphql.Resource.encode_id(notification.data, false)
}}
)

{:error, error} ->
resolution
|> Absinthe.Resolution.put_result({:error, to_errors([error], context, domain)})
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/resource/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ defmodule AshGraphql.Resource do
identifier: :destroyed,
module: schema,
name: "destroyed",
type: resource_type
type: :id
}
],
__reference__: ref(__ENV__)
Expand Down

0 comments on commit 6bd432b

Please sign in to comment.