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 ->
|
2022-02-01 08:55:14 +13:00
|
|
|
Application.delete_env(:ash_graphql, AshGraphql.Test.Api)
|
2021-09-12 18:13:19 +12:00
|
|
|
|
2021-01-22 17:06:06 +13:00
|
|
|
try do
|
2021-06-29 05:33:01 +12:00
|
|
|
Ash.DataLayer.Ets.stop(AshGraphql.Test.Post)
|
|
|
|
Ash.DataLayer.Ets.stop(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
|
|
|
|
2021-08-30 07:23:28 +12:00
|
|
|
test "metadata is in the result" do
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation SimpleCreatePost($input: SimpleCreatePostInput) {
|
|
|
|
simpleCreatePost(input: $input) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
comments(sort:{field:TEXT}){
|
|
|
|
text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
metadata{
|
|
|
|
foo
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{"input" => %{"text" => "foobar"}}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
|
|
|
|
assert %{
|
|
|
|
data: %{
|
|
|
|
"simpleCreatePost" => %{
|
|
|
|
"result" => %{
|
|
|
|
"text" => "foobar"
|
|
|
|
},
|
|
|
|
"metadata" => %{
|
|
|
|
"foo" => "bar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} = result
|
|
|
|
end
|
|
|
|
|
2021-04-24 10:44:56 +12:00
|
|
|
test "a create with a managed relationship works" do
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation CreatePostWithComments($input: CreatePostWithCommentsInput) {
|
|
|
|
createPostWithComments(input: $input) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
comments(sort:{field:TEXT}){
|
|
|
|
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" => %{
|
|
|
|
"text" => "foobar",
|
|
|
|
"comments" => [%{"text" => "barfoo"}, %{"text" => "foobar"}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} = result
|
|
|
|
end
|
|
|
|
|
2021-09-12 18:13:19 +12:00
|
|
|
test "a create can load a calculation without selecting the fields the calculation needs" do
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation SimpleCreatePost($input: SimpleCreatePostInput) {
|
|
|
|
simpleCreatePost(input: $input) {
|
|
|
|
result{
|
|
|
|
text1
|
|
|
|
fullText
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{"input" => %{"text1" => "foo", "text2" => "bar"}}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
|
|
|
|
assert %{
|
|
|
|
data: %{
|
|
|
|
"simpleCreatePost" => %{
|
|
|
|
"result" => %{
|
|
|
|
"fullText" => "foobar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} = result
|
|
|
|
end
|
|
|
|
|
2021-05-12 11:35:50 +12:00
|
|
|
test "a create with a managed relationship works with many_to_many and [on_lookup: :relate, on_match: :relate]" do
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation CreatePostWithCommentsAndTags($input: CreatePostWithCommentsAndTagsInput) {
|
|
|
|
createPostWithCommentsAndTags(input: $input) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
comments(sort:{field:TEXT}){
|
|
|
|
text
|
|
|
|
}
|
|
|
|
tags(sort:{field:NAME}){
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"input" => %{
|
|
|
|
"text" => "foobar",
|
|
|
|
"comments" => [
|
|
|
|
%{"text" => "foobar"},
|
|
|
|
%{"text" => "barfoo"}
|
|
|
|
],
|
|
|
|
"tags" => [%{"name" => "test"}, %{"name" => "tag"}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
|
|
|
|
assert %{
|
|
|
|
data: %{
|
|
|
|
"createPostWithCommentsAndTags" => %{
|
|
|
|
"result" => %{
|
|
|
|
"text" => "foobar",
|
|
|
|
"comments" => [%{"text" => "barfoo"}, %{"text" => "foobar"}],
|
|
|
|
"tags" => [%{"name" => "tag"}, %{"name" => "test"}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} = result
|
|
|
|
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-19 15:19:46 +12:00
|
|
|
test "a create with a fragment works" do
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
fragment comparisonFields on Post {
|
|
|
|
text
|
|
|
|
}
|
|
|
|
mutation CreatePost($input: CreatePostInput) {
|
|
|
|
createPost(input: $input) {
|
|
|
|
result{
|
|
|
|
...comparisonFields
|
|
|
|
}
|
|
|
|
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-09-12 17:02:08 +12:00
|
|
|
test "root level error" do
|
2022-02-01 08:55:14 +13:00
|
|
|
Application.put_env(:ash_graphql, AshGraphql.Test.Api,
|
2021-09-12 17:02:08 +12:00
|
|
|
graphql: [show_raised_errors?: true, root_level_errors?: true]
|
|
|
|
)
|
|
|
|
|
|
|
|
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 %{errors: [%{message: message}]} = result
|
|
|
|
|
|
|
|
assert message =~ "Confirmation did not match value"
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2021-04-24 10:44:56 +12:00
|
|
|
test "standard enums are used" do
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation CreatePost($input: CreatePostInput) {
|
|
|
|
createPost(input: $input) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
statusEnum
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"input" => %{
|
|
|
|
"text" => "foobar",
|
|
|
|
"confirmation" => "foobar",
|
|
|
|
"statusEnum" => "OPEN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
|
|
|
|
assert %{
|
|
|
|
data: %{
|
|
|
|
"createPost" => %{
|
|
|
|
"result" => %{"text" => "foobar", "statusEnum" => "OPEN"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} = 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
|