From 6bd432b059208e646a0080bd3d1ea9dfad0721b9 Mon Sep 17 00:00:00 2001 From: Barnabas Jovanovics Date: Thu, 15 Aug 2024 16:04:56 +0200 Subject: [PATCH] handle destroys --- lib/graphql/resolver.ex | 86 +++++++++++++++++++++++----------------- lib/resource/resource.ex | 2 +- 2 files changed, 51 insertions(+), 37 deletions(-) diff --git a/lib/graphql/resolver.ex b/lib/graphql/resolver.ex index 89819394..75253a3c 100644 --- a/lib/graphql/resolver.ex +++ b/lib/graphql/resolver.ex @@ -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 diff --git a/lib/resource/resource.ex b/lib/resource/resource.ex index 9b10988f..ea2911e3 100644 --- a/lib/resource/resource.ex +++ b/lib/resource/resource.ex @@ -1144,7 +1144,7 @@ defmodule AshGraphql.Resource do identifier: :destroyed, module: schema, name: "destroyed", - type: resource_type + type: :id } ], __reference__: ref(__ENV__)