mirror of
https://github.com/ash-project/ash_graphql.git
synced 2024-09-20 05:13:33 +12:00
Add test for mutation with read action in simple data layer resource issue (#170)
Co-authored-by: Junil Chon <jichon@gmail.com>
This commit is contained in:
parent
543fbeab7e
commit
4ff56a3c59
5 changed files with 126 additions and 2 deletions
|
@ -17,7 +17,7 @@ defmodule AshGraphql.CustpmPaginateTest do
|
|||
test "channel record with direct union message records are fetched" do
|
||||
channel =
|
||||
AshGraphql.Test.Channel
|
||||
|> Ash.Changeset.for_create(:create, %{})
|
||||
|> Ash.Changeset.for_create(:create, name: "test channel")
|
||||
|> Ash.create!()
|
||||
|
||||
text_message =
|
||||
|
@ -88,7 +88,7 @@ defmodule AshGraphql.CustpmPaginateTest do
|
|||
test "channel record with page of channel messages record is fetched" do
|
||||
channel =
|
||||
AshGraphql.Test.Channel
|
||||
|> Ash.Changeset.for_create(:create, %{})
|
||||
|> Ash.Changeset.for_create(:create, name: "test channel")
|
||||
|> Ash.create!()
|
||||
|
||||
text_message =
|
||||
|
|
|
@ -41,6 +41,7 @@ defmodule AshGraphql.Test.Domain do
|
|||
resource(AshGraphql.Test.Tag)
|
||||
resource(AshGraphql.Test.User)
|
||||
resource(AshGraphql.Test.Channel)
|
||||
resource(AshGraphql.Test.ChannelSimple)
|
||||
resource(AshGraphql.Test.Message)
|
||||
resource(AshGraphql.Test.TextMessage)
|
||||
resource(AshGraphql.Test.ImageMessage)
|
||||
|
|
|
@ -33,6 +33,8 @@ defmodule AshGraphql.Test.Channel do
|
|||
attributes do
|
||||
uuid_primary_key(:id)
|
||||
|
||||
attribute(:name, :string, public?: true, allow_nil?: false)
|
||||
|
||||
create_timestamp(:created_at, public?: true)
|
||||
end
|
||||
|
||||
|
|
83
test/support/resources/channel/channel_simple.ex
Normal file
83
test/support/resources/channel/channel_simple.ex
Normal file
|
@ -0,0 +1,83 @@
|
|||
defmodule AshGraphql.Test.ChannelSimple do
|
||||
@moduledoc false
|
||||
|
||||
use Ash.Resource,
|
||||
domain: AshGraphql.Test.Domain,
|
||||
extensions: [AshGraphql.Resource]
|
||||
|
||||
require Ash.Query
|
||||
|
||||
graphql do
|
||||
type :channel_simple
|
||||
|
||||
mutations do
|
||||
update :update_channel, :update_channel, read_action: :read_channel, identity: false
|
||||
end
|
||||
end
|
||||
|
||||
actions do
|
||||
default_accept(:*)
|
||||
|
||||
create(:create, primary?: true)
|
||||
|
||||
read(:read, primary?: true)
|
||||
|
||||
update(:update, primary?: true)
|
||||
|
||||
destroy(:destroy, primary?: true)
|
||||
|
||||
read :read_channel do
|
||||
argument(:channel_id, :uuid, allow_nil?: false)
|
||||
|
||||
get?(true)
|
||||
|
||||
prepare(fn query, _ ->
|
||||
channel_id = Ash.Query.get_argument(query, :channel_id)
|
||||
|
||||
case AshGraphql.Test.Channel
|
||||
|> Ash.Query.for_read(:read, %{})
|
||||
|> Ash.Query.filter(id == ^channel_id)
|
||||
|> Ash.read_one() do
|
||||
{:ok, channel} ->
|
||||
query
|
||||
|> Ash.DataLayer.Simple.set_data([
|
||||
struct(AshGraphql.Test.ChannelSimple, %{
|
||||
channel: channel
|
||||
})
|
||||
])
|
||||
|
||||
{:error, error} ->
|
||||
query |> Ash.Query.add_error(error)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
update :update_channel do
|
||||
require_atomic?(false)
|
||||
|
||||
argument(:name, :string, allow_nil?: false)
|
||||
|
||||
change(fn changeset, _ ->
|
||||
name = Ash.Changeset.get_argument(changeset, :name)
|
||||
|
||||
changeset.data.channel
|
||||
|> Ash.Changeset.for_update(:update, name: name)
|
||||
|> Ash.update!()
|
||||
|
||||
changeset
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
attributes do
|
||||
uuid_primary_key(:id)
|
||||
|
||||
attribute :channel, :struct do
|
||||
constraints(instance_of: AshGraphql.Test.Channel)
|
||||
allow_nil?(false)
|
||||
public?(true)
|
||||
end
|
||||
|
||||
create_timestamp(:created_at, public?: true)
|
||||
end
|
||||
end
|
|
@ -325,4 +325,42 @@ defmodule AshGraphql.UpdateTest do
|
|||
} =
|
||||
resp
|
||||
end
|
||||
|
||||
test "an update with a configured read action and no identity works in resource with simple data layer" do
|
||||
channel =
|
||||
AshGraphql.Test.Channel
|
||||
|> Ash.Changeset.for_create(:create, name: "test channel 1")
|
||||
|> Ash.create!()
|
||||
|
||||
resp =
|
||||
"""
|
||||
mutation UpdateChannel($channelId: ID!, $input: UpdateChannelInput!) {
|
||||
updateChannel(channelId: $channelId, input: $input) {
|
||||
result{
|
||||
channel {
|
||||
name
|
||||
}
|
||||
}
|
||||
errors{
|
||||
message
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
|> Absinthe.run(AshGraphql.Test.Schema,
|
||||
variables: %{
|
||||
"channelId" => channel.id,
|
||||
"input" => %{"name" => "test channel 2"}
|
||||
}
|
||||
)
|
||||
|
||||
assert {:ok, result} = resp
|
||||
|
||||
assert %{
|
||||
data: %{
|
||||
"updateChannel" => %{"result" => %{"channel" => %{"name" => "test channel 2"}}}
|
||||
}
|
||||
} =
|
||||
result
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue