From 43dae2ad2820394729225d50d1dfbd7e5af83c77 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Fri, 16 Apr 2021 12:55:34 -0400 Subject: [PATCH] fix: don't clear fields on `nil` result chore: add tests for destroy --- lib/graphql/resolver.ex | 5 ++-- test/destroy_test.exs | 43 ++++++++++++++++++++++++++++++++++ test/support/resources/post.ex | 1 + 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 test/destroy_test.exs diff --git a/lib/graphql/resolver.ex b/lib/graphql/resolver.ex index 9434b1a..8fcfc95 100644 --- a/lib/graphql/resolver.ex +++ b/lib/graphql/resolver.ex @@ -346,13 +346,14 @@ defmodule AshGraphql.Graphql.Resolver do }} end + defp clear_fields(nil, _, _), do: nil + defp clear_fields(result, resource, resolution) do resolution |> fields(true) |> Enum.map(fn field -> Ash.Resource.Info.aggregate(resource, field.schema_node.identifier) || - Ash.Resource.Info.calculation(resource, field.schema_node.identifier) || - Ash.Resource.Info.attribute(resource, field.schema_node.identifier) + Ash.Resource.Info.calculation(resource, field.schema_node.identifier) end) |> Enum.filter(& &1) |> Enum.map(& &1.name) diff --git a/test/destroy_test.exs b/test/destroy_test.exs new file mode 100644 index 0000000..d45239b --- /dev/null +++ b/test/destroy_test.exs @@ -0,0 +1,43 @@ +defmodule AshGraphql.DestroyTest do + use ExUnit.Case, async: false + + setup do + on_exit(fn -> + try do + ETS.Set.delete(ETS.Set.wrap_existing!(AshGraphql.Test.Post)) + ETS.Set.delete(ETS.Set.wrap_existing!(AshGraphql.Test.Comment)) + rescue + _ -> + :ok + end + end) + end + + test "a destroy works" do + post = AshGraphql.Test.Api.create!(Ash.Changeset.new(AshGraphql.Test.Post, text: "foobar")) + + resp = + """ + mutation DeletePost($id: ID) { + deletePost(id: $id) { + result{ + text + } + errors{ + message + } + } + } + """ + |> Absinthe.run(AshGraphql.Test.Schema, + variables: %{ + "id" => post.id + } + ) + + assert {:ok, result} = resp + + refute Map.has_key?(result, :errors) + assert %{data: %{"deletePost" => %{"result" => %{"text" => "foobar"}}}} = result + end +end diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index 24cd2ba..df89575 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -17,6 +17,7 @@ defmodule AshGraphql.Test.Post do create :create_post, :create_confirm create :upsert_post, :upsert, upsert?: true update :update_post, :update + destroy :delete_post, :destroy end end