mirror of
https://github.com/ash-project/ash_graphql.git
synced 2024-09-20 05:13:33 +12:00
8fff0d361d
* improvement: make mutation arguments non-null As discussed in #105 and #110, put this behind an opt-in configuration to avoid breaking existing code. The ID in update mutations is always non-null if non-null mutation arguments are allowed, while input is non-null if it's allowed _and_ there is at least a non-null field in the input. Document the newly added config variable in the getting started guide. * chore: enable non-null mutation arguments in tests
186 lines
4 KiB
Elixir
186 lines
4 KiB
Elixir
defmodule AshGraphql.DestroyTest do
|
|
use ExUnit.Case, async: false
|
|
|
|
setup do
|
|
on_exit(fn ->
|
|
Application.delete_env(:ash_graphql, AshGraphql.Test.Api)
|
|
|
|
AshGraphql.TestHelpers.stop_ets()
|
|
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
|
|
|
|
test "a soft destroy works" do
|
|
post = AshGraphql.Test.Api.create!(Ash.Changeset.new(AshGraphql.Test.Post, text: "foobar"))
|
|
|
|
resp =
|
|
"""
|
|
mutation ArchivePost($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
|
|
|
|
test "a destroy with a configured read action and no identity works" do
|
|
AshGraphql.Test.Api.create!(
|
|
Ash.Changeset.new(AshGraphql.Test.Post, text: "foobar", best: true)
|
|
)
|
|
|
|
resp =
|
|
"""
|
|
mutation DeleteBestPost {
|
|
deleteBestPost {
|
|
result{
|
|
text
|
|
}
|
|
errors{
|
|
message
|
|
}
|
|
}
|
|
}
|
|
"""
|
|
|> Absinthe.run(AshGraphql.Test.Schema)
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
assert %{data: %{"deleteBestPost" => %{"result" => %{"text" => "foobar"}}}} = result
|
|
end
|
|
|
|
test "a destroy with an error" do
|
|
post = AshGraphql.Test.Api.create!(Ash.Changeset.new(AshGraphql.Test.Post, text: "foobar"))
|
|
|
|
resp =
|
|
"""
|
|
mutation DeleteWithError($id: ID!) {
|
|
deletePostWithError(id: $id) {
|
|
result{
|
|
text
|
|
}
|
|
errors{
|
|
message
|
|
}
|
|
}
|
|
}
|
|
"""
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
variables: %{
|
|
"id" => post.id
|
|
}
|
|
)
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
assert %{
|
|
data: %{
|
|
"deletePostWithError" => %{
|
|
"errors" => [%{"message" => "could not be found"}],
|
|
"result" => nil
|
|
}
|
|
}
|
|
} == result
|
|
end
|
|
|
|
test "destroying a non-existent record returns a not found error" do
|
|
resp =
|
|
"""
|
|
mutation DeletePost($id: ID!) {
|
|
deletePost(id: $id) {
|
|
result{
|
|
text
|
|
}
|
|
errors{
|
|
message
|
|
}
|
|
}
|
|
}
|
|
"""
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
variables: %{
|
|
"id" => Ash.UUID.generate()
|
|
}
|
|
)
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
assert %{data: %{"deletePost" => %{"errors" => [%{"message" => "could not be found"}]}}} =
|
|
result
|
|
end
|
|
|
|
test "root level error on destroy" do
|
|
Application.put_env(:ash_graphql, AshGraphql.Test.Api,
|
|
graphql: [show_raised_errors?: true, root_level_errors?: true]
|
|
)
|
|
|
|
post = AshGraphql.Test.Api.create!(Ash.Changeset.new(AshGraphql.Test.Post, text: "foobar"))
|
|
|
|
resp =
|
|
"""
|
|
mutation DeletePost($id: ID!) {
|
|
deletePostWithError(id: $id) {
|
|
result{
|
|
text
|
|
}
|
|
errors{
|
|
message
|
|
}
|
|
}
|
|
}
|
|
"""
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
variables: %{
|
|
"id" => post.id
|
|
}
|
|
)
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
assert %{errors: [%{message: "could not be found"}]} = result
|
|
end
|
|
end
|