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:
Zach Daniel 2024-06-02 08:08:46 -04:00 committed by GitHub
parent 543fbeab7e
commit 4ff56a3c59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 126 additions and 2 deletions

View file

@ -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 =

View file

@ -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)

View file

@ -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

View 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

View file

@ -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