Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/graphql/resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1665,6 +1665,7 @@ defmodule AshGraphql.Graphql.Resolver do
|> Ash.bulk_update(action, input,
return_errors?: true,
notify?: true,
rollback_on_error?: false,
strategy: [:atomic, :stream, :atomic_batches],
allow_stream_with: :full_read,
authorize_changeset_with: authorize_bulk_with(query.resource),
Expand Down Expand Up @@ -1829,6 +1830,7 @@ defmodule AshGraphql.Graphql.Resolver do
|> Ash.bulk_destroy(action, input,
return_errors?: true,
notify?: true,
rollback_on_error?: false,
authorize_changeset_with: authorize_bulk_with(query.resource),
strategy: [:atomic, :stream, :atomic_batches],
allow_stream_with: :full_read,
Expand Down
22 changes: 11 additions & 11 deletions mix.lock

Large diffs are not rendered by default.

142 changes: 142 additions & 0 deletions test/after_transaction_mnesia_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# SPDX-FileCopyrightText: 2020 ash_graphql contributors <https://github.com/ash-project/ash_graphql/graphs.contributors>
#
# SPDX-License-Identifier: MIT

defmodule AshGraphql.AfterTransactionMnesiaTest do
@moduledoc "Tests after_transaction with Mnesia (real transactions)"
use ExUnit.Case, async: false

setup do
:mnesia.start()
Ash.DataLayer.Mnesia.start(AshGraphql.Test.Domain, [AshGraphql.Test.AfterTransactionMnesia])
on_exit(fn -> :mnesia.clear_table(:after_transaction_mnesia_table) end)
end

describe "create mutation" do
test "after_transaction is called on success" do
"""
mutation CreateAfterTransactionMnesia($input: CreateAfterTransactionMnesiaInput!) {
createAfterTransactionMnesia(input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"input" => %{"name" => "test"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :create, {:ok, _}}
end

test "after_transaction is called on error" do
"""
mutation CreateAfterTransactionMnesiaWithError($input: CreateAfterTransactionMnesiaWithErrorInput!) {
createAfterTransactionMnesiaWithError(input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"input" => %{"name" => "test"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :create_with_error, {:error, _}}
end
end

describe "update mutation" do
test "after_transaction is called on success" do
record =
AshGraphql.Test.AfterTransactionMnesia
|> Ash.Changeset.for_create(:create, %{name: "original"})
|> Ash.create!()

"""
mutation UpdateAfterTransactionMnesia($id: ID!, $input: UpdateAfterTransactionMnesiaInput!) {
updateAfterTransactionMnesia(id: $id, input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id, "input" => %{"name" => "updated"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :update, {:ok, _}}
end

test "after_transaction is called on error" do
record =
AshGraphql.Test.AfterTransactionMnesia
|> Ash.Changeset.for_create(:create, %{name: "original"})
|> Ash.create!()

"""
mutation UpdateAfterTransactionMnesiaWithError($id: ID!, $input: UpdateAfterTransactionMnesiaWithErrorInput!) {
updateAfterTransactionMnesiaWithError(id: $id, input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id, "input" => %{"name" => "updated"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :update_with_error, {:error, _}}
end
end

describe "destroy mutation" do
test "after_transaction is called on success" do
record =
AshGraphql.Test.AfterTransactionMnesia
|> Ash.Changeset.for_create(:create, %{name: "to_destroy"})
|> Ash.create!()

"""
mutation DestroyAfterTransactionMnesia($id: ID!) {
destroyAfterTransactionMnesia(id: $id) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :destroy, {:ok, _}}
end

test "after_transaction is called on error" do
record =
AshGraphql.Test.AfterTransactionMnesia
|> Ash.Changeset.for_create(:create, %{name: "to_destroy"})
|> Ash.create!()

"""
mutation DestroyAfterTransactionMnesiaWithError($id: ID!) {
destroyAfterTransactionMnesiaWithError(id: $id) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :destroy_with_error, {:error, _}}
end
end
end
139 changes: 139 additions & 0 deletions test/after_transaction_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# SPDX-FileCopyrightText: 2020 ash_graphql contributors <https://github.com/ash-project/ash_graphql/graphs.contributors>
#
# SPDX-License-Identifier: MIT

defmodule AshGraphql.AfterTransactionEts do
use ExUnit.Case, async: false

setup do
on_exit(fn -> AshGraphql.TestHelpers.stop_ets() end)
end

describe "create mutation" do
test "after_transaction is called on success" do
"""
mutation CreateAfterTransactionEts($input: CreateAfterTransactionEtsInput!) {
createAfterTransactionEts(input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"input" => %{"name" => "test"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :create, {:ok, _}}
end

test "after_transaction is called on error" do
"""
mutation CreateAfterTransactionEtsWithError($input: CreateAfterTransactionEtsWithErrorInput!) {
createAfterTransactionEtsWithError(input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"input" => %{"name" => "test"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :create_with_error, {:error, _}}
end
end

describe "update mutation" do
test "after_transaction is called on success" do
record =
AshGraphql.Test.AfterTransactionEts
|> Ash.Changeset.for_create(:create, %{name: "original"})
|> Ash.create!()

"""
mutation UpdateAfterTransactionEts($id: ID!, $input: UpdateAfterTransactionEtsInput!) {
updateAfterTransactionEts(id: $id, input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id, "input" => %{"name" => "updated"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :update, {:ok, _}}
end

test "after_transaction is called on error" do
record =
AshGraphql.Test.AfterTransactionEts
|> Ash.Changeset.for_create(:create, %{name: "original"})
|> Ash.create!()

"""
mutation UpdateAfterTransactionEtsWithError($id: ID!, $input: UpdateAfterTransactionEtsWithErrorInput!) {
updateAfterTransactionEtsWithError(id: $id, input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id, "input" => %{"name" => "updated"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :update_with_error, {:error, _}}
end
end

describe "destroy mutation" do
test "after_transaction is called on success" do
record =
AshGraphql.Test.AfterTransactionEts
|> Ash.Changeset.for_create(:create, %{name: "to_destroy"})
|> Ash.create!()

"""
mutation DestroyAfterTransactionEts($id: ID!) {
destroyAfterTransactionEts(id: $id) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :destroy, {:ok, _}}
end

test "after_transaction is called on error" do
record =
AshGraphql.Test.AfterTransactionEts
|> Ash.Changeset.for_create(:create, %{name: "to_destroy"})
|> Ash.create!()

"""
mutation DestroyAfterTransactionEtsWithError($id: ID!) {
destroyAfterTransactionEtsWithError(id: $id) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :destroy_with_error, {:error, _}}
end
end
end
2 changes: 2 additions & 0 deletions test/support/domain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@ defmodule AshGraphql.Test.Domain do
resource(AshGraphql.Test.Product)
resource(AshGraphql.Test.ResourceWithUnion)
resource(AshGraphql.Test.ResourceWithTypedStruct)
resource(AshGraphql.Test.AfterTransactionEts)
resource(AshGraphql.Test.AfterTransactionMnesia)
end
end
Loading
Loading