improvement: add tests & improve behavior of update managed relationships

This commit is contained in:
Zach Daniel 2023-03-21 23:43:43 -04:00
parent fc1433170e
commit c711fa2e9d
3 changed files with 108 additions and 1 deletions

View file

@ -1724,7 +1724,7 @@ defmodule AshGraphql.Resource do
if could_lookup? || could_match? do if could_lookup? || could_match? do
pkey_fields = pkey_fields =
if managed_relationship.lookup_with_primary_key? do if managed_relationship.lookup_with_primary_key? || could_match? do
resource resource
|> pkey_fields(schema, false) |> pkey_fields(schema, false)
|> Enum.map(fn field -> |> Enum.map(fn field ->
@ -1736,6 +1736,13 @@ defmodule AshGraphql.Resource do
resource resource
|> Ash.Resource.Info.identities() |> Ash.Resource.Info.identities()
|> then(fn identities ->
if could_lookup? do
identities
else
[]
end
end)
|> Enum.filter(fn identity -> |> Enum.filter(fn identity ->
is_nil(managed_relationship.lookup_identities) || is_nil(managed_relationship.lookup_identities) ||
identity.name in managed_relationship.lookup_identities identity.name in managed_relationship.lookup_identities

View file

@ -85,6 +85,7 @@ defmodule AshGraphql.Test.Post do
managed_relationships do managed_relationships do
managed_relationship :with_comments, :comments managed_relationship :with_comments, :comments
managed_relationship :update_with_comments, :comments, lookup_with_primary_key?: true
managed_relationship :with_comments_and_tags, :comments, managed_relationship :with_comments_and_tags, :comments,
lookup_with_primary_key?: true, 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 create :create_post_with_comments_and_tags, :with_comments_and_tags
update :update_post, :update update :update_post, :update
update :update_post_with_comments, :update_with_comments
update :update_post_confirm, :update_confirm update :update_post_confirm, :update_confirm
update :update_best_post, :update, read_action: :best_post, identity: false 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, primary?: true
update :update_with_comments do
argument(:comments, {:array, :map})
change(manage_relationship(:comments, type: :direct_control))
end
update :update_confirm do update :update_confirm do
argument(:confirmation, :string) argument(:confirmation, :string)
validate(confirm(:text, :confirmation)) validate(confirm(:text, :confirmation))

View file

@ -43,6 +43,98 @@ defmodule AshGraphql.UpdateTest do
resp resp
end 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 test "an update with a configured read action and no identity works" do
post = post =
AshGraphql.Test.Api.create!( AshGraphql.Test.Api.create!(