2020-12-02 18:07:15 +13:00
|
|
|
defmodule AshGraphql.CreateTest do
|
2020-12-02 18:55:25 +13:00
|
|
|
use ExUnit.Case, async: false
|
|
|
|
|
|
|
|
setup do
|
|
|
|
on_exit(fn ->
|
2021-01-22 17:06:06 +13:00
|
|
|
try do
|
|
|
|
ETS.Set.delete(ETS.Set.wrap_existing!(AshGraphql.Test.Post))
|
2021-03-16 08:51:17 +13:00
|
|
|
ETS.Set.delete(ETS.Set.wrap_existing!(AshGraphql.Test.Comment))
|
2021-01-22 17:06:06 +13:00
|
|
|
rescue
|
|
|
|
_ ->
|
|
|
|
:ok
|
|
|
|
end
|
2020-12-02 18:55:25 +13:00
|
|
|
end)
|
|
|
|
end
|
2020-12-02 18:07:15 +13:00
|
|
|
|
|
|
|
test "a create with arguments works" do
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation CreatePost($input: CreatePostInput) {
|
|
|
|
createPost(input: $input) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"input" => %{
|
|
|
|
"text" => "foobar",
|
|
|
|
"confirmation" => "foobar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
assert %{data: %{"createPost" => %{"result" => %{"text" => "foobar"}}}} = result
|
|
|
|
end
|
|
|
|
|
2021-04-06 06:41:16 +12:00
|
|
|
test "an upsert works" do
|
|
|
|
post =
|
|
|
|
AshGraphql.Test.Post
|
|
|
|
|> Ash.Changeset.new(text: "foobar")
|
|
|
|
|> AshGraphql.Test.Api.create!()
|
|
|
|
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation CreatePost($input: UpsertPostInput) {
|
|
|
|
upsertPost(input: $input) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
id
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"input" => %{
|
|
|
|
"id" => post.id,
|
|
|
|
"text" => "foobar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
post_id = post.id
|
|
|
|
|
2021-04-17 05:49:51 +12:00
|
|
|
assert %{data: %{"upsertPost" => %{"result" => %{"text" => "foobar", "id" => ^post_id}}}} =
|
2021-04-06 06:41:16 +12:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2020-12-02 18:07:15 +13:00
|
|
|
test "arguments are threaded properly" do
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation CreatePost($input: CreatePostInput) {
|
|
|
|
createPost(input: $input) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"input" => %{
|
|
|
|
"text" => "foobar",
|
|
|
|
"confirmation" => "foobar2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
assert %{data: %{"createPost" => %{"result" => nil, "errors" => [%{"message" => message}]}}} =
|
|
|
|
result
|
|
|
|
|
2021-01-08 16:07:50 +13:00
|
|
|
assert message =~ "Confirmation did not match value"
|
2020-12-02 18:07:15 +13:00
|
|
|
end
|
2021-03-16 08:51:17 +13:00
|
|
|
|
2021-03-29 05:30:28 +13:00
|
|
|
test "custom input types are used" do
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation CreatePost($input: CreatePostInput) {
|
|
|
|
createPost(input: $input) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
foo{
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"input" => %{
|
|
|
|
"text" => "foobar",
|
|
|
|
"confirmation" => "foobar",
|
|
|
|
"foo" => %{
|
|
|
|
"foo" => "foo",
|
|
|
|
"bar" => "bar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
|
|
|
|
assert %{
|
|
|
|
data: %{
|
|
|
|
"createPost" => %{
|
|
|
|
"result" => %{"text" => "foobar", "foo" => %{"foo" => "foo", "bar" => "bar"}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} = result
|
|
|
|
end
|
2021-03-29 08:46:23 +13:00
|
|
|
|
|
|
|
test "custom enums are used" do
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation CreatePost($input: CreatePostInput) {
|
|
|
|
createPost(input: $input) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
status
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"input" => %{
|
|
|
|
"text" => "foobar",
|
|
|
|
"confirmation" => "foobar",
|
|
|
|
"status" => "OPEN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
|
|
|
|
assert %{
|
|
|
|
data: %{
|
|
|
|
"createPost" => %{
|
|
|
|
"result" => %{"text" => "foobar", "status" => "OPEN"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} = result
|
|
|
|
end
|
2020-12-02 18:07:15 +13:00
|
|
|
end
|