2021-03-16 08:51:17 +13:00
|
|
|
defmodule AshGraphql.UpdateTest do
|
|
|
|
use ExUnit.Case, async: false
|
|
|
|
|
|
|
|
setup do
|
2022-02-01 08:55:14 +13:00
|
|
|
on_exit(fn ->
|
2024-04-02 07:03:06 +13:00
|
|
|
Application.delete_env(:ash_graphql, AshGraphql.Test.Domain)
|
2023-01-19 17:14:08 +13:00
|
|
|
|
2021-03-16 08:51:17 +13:00
|
|
|
try do
|
2022-09-28 19:57:23 +13:00
|
|
|
AshGraphql.TestHelpers.stop_ets()
|
2021-03-16 08:51:17 +13:00
|
|
|
rescue
|
|
|
|
_ ->
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "an update works" do
|
2024-04-02 07:03:06 +13:00
|
|
|
post =
|
|
|
|
AshGraphql.Test.Post
|
|
|
|
|> Ash.Changeset.for_create(:create, text: "foobar")
|
|
|
|
|> Ash.create!()
|
2021-03-16 08:51:17 +13:00
|
|
|
|
|
|
|
resp =
|
|
|
|
"""
|
2024-02-01 11:52:01 +13:00
|
|
|
mutation UpdatePost($id: ID!, $input: UpdatePostInput) {
|
2021-03-16 08:51:17 +13:00
|
|
|
updatePost(id: $id, input: $input) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"id" => post.id,
|
|
|
|
"input" => %{
|
|
|
|
"text" => "barbuz"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, %{data: %{"updatePost" => %{"errors" => [], "result" => %{"text" => "barbuz"}}}}} =
|
|
|
|
resp
|
|
|
|
end
|
2021-04-20 07:26:20 +12:00
|
|
|
|
2023-03-22 16:43:43 +13:00
|
|
|
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
|
|
|
|
|
2021-04-20 07:26:20 +12:00
|
|
|
test "an update with a configured read action and no identity works" do
|
|
|
|
post =
|
2024-04-02 07:03:06 +13:00
|
|
|
AshGraphql.Test.Post
|
|
|
|
|> Ash.Changeset.for_create(:create, text: "foobar", best: true)
|
|
|
|
|> Ash.create!()
|
2021-04-20 07:26:20 +12:00
|
|
|
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation UpdateBestPost($input: UpdateBestPostInput) {
|
|
|
|
updateBestPost(input: $input) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"id" => post.id,
|
|
|
|
"input" => %{
|
|
|
|
"text" => "barbuz"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok,
|
|
|
|
%{data: %{"updateBestPost" => %{"errors" => [], "result" => %{"text" => "barbuz"}}}}} =
|
|
|
|
resp
|
|
|
|
end
|
2021-09-12 17:02:08 +12:00
|
|
|
|
2023-05-20 10:01:33 +12:00
|
|
|
test "an update with a configured read action and no identity works with an argument the same name as an attribute" do
|
2024-04-02 07:03:06 +13:00
|
|
|
AshGraphql.Test.Post
|
|
|
|
|> Ash.Changeset.for_create(:create, text: "foobar", best: true)
|
|
|
|
|> Ash.create!()
|
2023-05-20 10:01:33 +12:00
|
|
|
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation UpdateBestPostArg($best: Boolean!, $input: UpdateBestPostArgInput) {
|
|
|
|
updateBestPostArg(best: $best, input: $input) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"best" => true,
|
|
|
|
"input" => %{
|
|
|
|
"text" => "barbuz"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok,
|
|
|
|
%{
|
|
|
|
data: %{"updateBestPostArg" => %{"errors" => [], "result" => %{"text" => "barbuz"}}}
|
|
|
|
}} = resp
|
|
|
|
end
|
|
|
|
|
2021-09-12 17:02:08 +12:00
|
|
|
test "arguments are threaded properly" do
|
2022-05-29 13:41:00 +12:00
|
|
|
post =
|
2024-04-02 07:03:06 +13:00
|
|
|
AshGraphql.Test.Post
|
|
|
|
|> Ash.Changeset.for_create(:create, text: "foobar", best: true)
|
|
|
|
|> Ash.create!()
|
2022-05-29 13:41:00 +12:00
|
|
|
|
2021-09-12 17:02:08 +12:00
|
|
|
resp =
|
|
|
|
"""
|
2024-02-01 11:52:01 +13:00
|
|
|
mutation UpdatePostConfirm($input: UpdatePostConfirmInput, $id: ID!) {
|
2022-05-29 13:41:00 +12:00
|
|
|
updatePostConfirm(input: $input, id: $id) {
|
2021-09-12 17:02:08 +12:00
|
|
|
result{
|
|
|
|
text
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
2022-05-29 13:41:00 +12:00
|
|
|
"id" => post.id,
|
2021-09-12 17:02:08 +12:00
|
|
|
"input" => %{
|
|
|
|
"text" => "foobar",
|
|
|
|
"confirmation" => "foobar2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
2021-09-12 18:13:19 +12:00
|
|
|
assert %{
|
|
|
|
data: %{
|
|
|
|
"updatePostConfirm" => %{"result" => nil, "errors" => [%{"message" => message}]}
|
|
|
|
}
|
|
|
|
} = result
|
2021-09-12 17:02:08 +12:00
|
|
|
|
2023-06-03 05:54:09 +12:00
|
|
|
assert message =~ "confirmation did not match value"
|
2021-09-12 17:02:08 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
test "root level error" do
|
2024-04-02 07:03:06 +13:00
|
|
|
Application.put_env(:ash_graphql, AshGraphql.Test.Domain,
|
2021-09-12 17:02:08 +12:00
|
|
|
graphql: [show_raised_errors?: true, root_level_errors?: true]
|
|
|
|
)
|
|
|
|
|
2022-05-29 13:41:00 +12:00
|
|
|
post =
|
2024-04-02 07:03:06 +13:00
|
|
|
AshGraphql.Test.Post
|
|
|
|
|> Ash.Changeset.for_create(:create, text: "foobar", best: true)
|
|
|
|
|> Ash.create!()
|
2022-05-29 13:41:00 +12:00
|
|
|
|
2021-09-12 17:02:08 +12:00
|
|
|
resp =
|
|
|
|
"""
|
2024-02-01 11:52:01 +13:00
|
|
|
mutation UpdatePostConfirm($input: UpdatePostConfirmInput, $id: ID!) {
|
2022-05-29 13:41:00 +12:00
|
|
|
updatePostConfirm(input: $input, id: $id) {
|
2021-09-12 17:02:08 +12:00
|
|
|
result{
|
|
|
|
text
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
2022-05-29 13:41:00 +12:00
|
|
|
"id" => post.id,
|
2021-09-12 17:02:08 +12:00
|
|
|
"input" => %{
|
|
|
|
"text" => "foobar",
|
|
|
|
"confirmation" => "foobar2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
assert %{errors: [%{message: message}]} = result
|
|
|
|
|
2023-06-03 05:54:09 +12:00
|
|
|
assert message =~ "confirmation did not match value"
|
2021-09-12 17:02:08 +12:00
|
|
|
end
|
2024-03-12 03:46:42 +13:00
|
|
|
|
|
|
|
test "referencing a hidden input is not allowed" do
|
2024-04-02 07:03:06 +13:00
|
|
|
post =
|
|
|
|
AshGraphql.Test.Post
|
|
|
|
|> Ash.Changeset.for_create(:create, text: "foobar")
|
|
|
|
|> Ash.create!()
|
2024-03-12 03:46:42 +13:00
|
|
|
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation UpdatePostWithHiddenInput($id: ID!, $input: UpdatePostWithHiddenInputInput) {
|
|
|
|
updatePostWithHiddenInput(id: $id, input: $input) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"id" => post.id,
|
|
|
|
"input" => %{
|
|
|
|
"score" => 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {
|
|
|
|
:ok,
|
|
|
|
%{
|
|
|
|
errors: [
|
|
|
|
%{
|
|
|
|
message:
|
|
|
|
"Argument \"input\" has invalid value $input.\nIn field \"score\": Unknown field."
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
} =
|
|
|
|
resp
|
|
|
|
end
|
2024-06-03 00:08:46 +12:00
|
|
|
|
|
|
|
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
|
2024-07-12 02:56:40 +12:00
|
|
|
|
|
|
|
test "authenticateWithToken" do
|
2024-07-16 06:25:40 +12:00
|
|
|
_user =
|
|
|
|
AshGraphql.Test.User
|
2024-07-12 02:56:40 +12:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "Name"})
|
|
|
|
|> Ash.create!(authorize?: false)
|
|
|
|
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation AuthenticateWithToken($token: String!) {
|
|
|
|
authenticateWithToken(token: $token) {
|
|
|
|
result {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
errors {
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema, variables: %{"token" => "invalid-token"})
|
|
|
|
|
2024-07-16 06:25:40 +12:00
|
|
|
assert {:ok,
|
|
|
|
%{
|
|
|
|
data: %{
|
|
|
|
"authenticateWithToken" => %{
|
|
|
|
"errors" => [%{"message" => "test error"}],
|
|
|
|
"result" => nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}} = resp
|
2024-07-12 02:56:40 +12:00
|
|
|
end
|
2021-03-16 08:51:17 +13:00
|
|
|
end
|