From c711fa2e9d3ab3f0c871120cc1165ff3aa692242 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Tue, 21 Mar 2023 23:43:43 -0400 Subject: [PATCH] improvement: add tests & improve behavior of update managed relationships --- lib/resource/resource.ex | 9 +++- test/support/resources/post.ex | 8 +++ test/update_test.exs | 92 ++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/lib/resource/resource.ex b/lib/resource/resource.ex index 9edf0e0..4415a64 100644 --- a/lib/resource/resource.ex +++ b/lib/resource/resource.ex @@ -1724,7 +1724,7 @@ defmodule AshGraphql.Resource do if could_lookup? || could_match? do pkey_fields = - if managed_relationship.lookup_with_primary_key? do + if managed_relationship.lookup_with_primary_key? || could_match? do resource |> pkey_fields(schema, false) |> Enum.map(fn field -> @@ -1736,6 +1736,13 @@ defmodule AshGraphql.Resource do resource |> Ash.Resource.Info.identities() + |> then(fn identities -> + if could_lookup? do + identities + else + [] + end + end) |> Enum.filter(fn identity -> is_nil(managed_relationship.lookup_identities) || identity.name in managed_relationship.lookup_identities diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index ca53865..5e541e6 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -85,6 +85,7 @@ defmodule AshGraphql.Test.Post do managed_relationships do managed_relationship :with_comments, :comments + managed_relationship :update_with_comments, :comments, lookup_with_primary_key?: true managed_relationship :with_comments_and_tags, :comments, lookup_with_primary_key?: true, @@ -106,6 +107,7 @@ defmodule AshGraphql.Test.Post do create :create_post_with_comments_and_tags, :with_comments_and_tags update :update_post, :update + update :update_post_with_comments, :update_with_comments update :update_post_confirm, :update_confirm update :update_best_post, :update, read_action: :best_post, identity: false @@ -204,6 +206,12 @@ defmodule AshGraphql.Test.Post do update :update, primary?: true + update :update_with_comments do + argument(:comments, {:array, :map}) + + change(manage_relationship(:comments, type: :direct_control)) + end + update :update_confirm do argument(:confirmation, :string) validate(confirm(:text, :confirmation)) diff --git a/test/update_test.exs b/test/update_test.exs index e08ccfd..eeeb2d3 100644 --- a/test/update_test.exs +++ b/test/update_test.exs @@ -43,6 +43,98 @@ defmodule AshGraphql.UpdateTest do resp end + test "an update with a managed relationship works" do + resp = + """ + mutation CreatePostWithComments($input: CreatePostWithCommentsInput) { + createPostWithComments(input: $input) { + result{ + id + text + comments(sort:{field:TEXT}){ + id + text + } + } + errors{ + message + } + } + } + """ + |> Absinthe.run(AshGraphql.Test.Schema, + variables: %{ + "input" => %{ + "text" => "foobar", + "comments" => [ + %{"text" => "foobar"}, + %{"text" => "barfoo"} + ] + } + } + ) + + assert {:ok, result} = resp + + refute Map.has_key?(result, :errors) + + assert %{ + data: %{ + "createPostWithComments" => %{ + "result" => %{ + "id" => post_id, + "text" => "foobar", + "comments" => [ + %{"id" => comment_id, "text" => "barfoo"}, + %{"text" => "foobar"} + ] + } + } + } + } = result + + resp = + """ + mutation UpdatePostWithComments($id: ID!, $input: UpdatePostWithCommentsInput) { + updatePostWithComments(id: $id, input: $input) { + result{ + comments(sort:{field:TEXT}){ + id + text + } + } + errors{ + message + } + } + } + """ + |> Absinthe.run(AshGraphql.Test.Schema, + variables: %{ + "id" => post_id, + "input" => %{ + "comments" => [ + %{"text" => "barfoonew", "id" => comment_id} + ] + } + } + ) + + assert {:ok, result} = resp + + refute Map.has_key?(result, :errors) + + assert %{ + data: %{ + "updatePostWithComments" => %{ + "result" => %{ + "comments" => [%{"id" => ^comment_id, "text" => "barfoonew"}] + } + } + } + } = result + end + test "an update with a configured read action and no identity works" do post = AshGraphql.Test.Api.create!(