improvement: make mutation result errors list non-nullable (#144)

* chore: regenerate cheat sheets

* improvement: make mutation result errors list non-nullable
This commit is contained in:
Alan Heywood 2024-05-04 11:57:02 +10:00 committed by GitHub
parent 446bc12f70
commit 7386a5b627
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 84 additions and 31 deletions

View file

@ -3,11 +3,11 @@ This file was generated by Spark. Do not edit it by hand.
-->
# DSL: AshGraphql.Domain
The entrypoint for adding graphql behavior to an Ash domain
The entrypoint for adding GraphQL behavior to an Ash domain
## graphql
Global configuration for graphql
Domain level configuration for GraphQL

View file

@ -930,9 +930,11 @@ defmodule AshGraphql.Resource do
identifier: :errors,
module: schema,
name: "errors",
type: %Absinthe.Blueprint.TypeReference.List{
of_type: %Absinthe.Blueprint.TypeReference.NonNull{
of_type: :mutation_error
type: %Absinthe.Blueprint.TypeReference.NonNull{
of_type: %Absinthe.Blueprint.TypeReference.List{
of_type: %Absinthe.Blueprint.TypeReference.NonNull{
of_type: :mutation_error
}
}
},
__reference__: ref(__ENV__)

View file

@ -11,34 +11,72 @@ defmodule AshGraphql.ErrorsTest do
end)
end
test "errors can be configured to be shown in the root" do
resp =
"""
mutation CreatePost($input: CreatePostInput) {
createPost(input: $input) {
result{
text
}
errors{
message
describe "when root level errors are enabled" do
test "errors that occur are shown at the root level" do
resp =
"""
mutation CreatePost($input: CreatePostInput) {
createPost(input: $input) {
result{
text
}
errors{
message
}
}
}
}
"""
|> Absinthe.run(AshGraphql.Test.RootLevelErrorsSchema,
variables: %{
"input" => %{
"text" => "foobar",
"confirmation" => "foobar2"
"""
|> Absinthe.run(AshGraphql.Test.RootLevelErrorsSchema,
variables: %{
"input" => %{
"text" => "foobar",
"confirmation" => "foobar2"
}
}
)
assert {:ok, result} = resp
assert %{data: %{"createPost" => nil}, errors: [%{message: message}]} = result
assert message =~ "confirmation did not match value"
end
test "the root level errors field is not present when no errors occur" do
resp =
"""
mutation CreatePost($input: CreatePostInput) {
createPost(input: $input) {
result{
text
}
errors{
message
}
}
}
)
"""
|> Absinthe.run(AshGraphql.Test.RootLevelErrorsSchema,
variables: %{
"input" => %{
"text" => "foobar",
"confirmation" => "foobar"
}
}
)
assert {:ok, result} = resp
assert {:ok, result} = resp
assert %{data: %{"createPost" => nil}, errors: [%{message: message}]} = result
assert %{
data: %{
"createPost" => %{
"result" => %{"text" => "foobar"},
"errors" => []
}
}
} = result
assert message =~ "confirmation did not match value"
refute Map.has_key?(result, :errors)
end
end
test "raised errors are by default not shown" do
@ -354,7 +392,7 @@ defmodule AshGraphql.ErrorsTest do
assert message =~ "Breakdown"
end
test "error items are non-nullable" do
test "error items are a non-nullable list of non-nullables" do
{:ok, %{data: data}} =
"""
query {
@ -366,7 +404,10 @@ defmodule AshGraphql.ErrorsTest do
ofType {
kind
ofType {
name
kind
ofType {
name
}
}
}
}
@ -380,9 +421,19 @@ defmodule AshGraphql.ErrorsTest do
data["__type"]["fields"]
|> Enum.find(fn field -> field["name"] == "errors" end)
assert errors["type"]["kind"] == "LIST"
assert errors["type"]["ofType"]["kind"] == "NON_NULL"
assert errors["type"]["ofType"]["ofType"]["name"] == "MutationError"
assert errors == %{
"name" => "errors",
"type" => %{
"kind" => "NON_NULL",
"ofType" => %{
"kind" => "LIST",
"ofType" => %{
"kind" => "NON_NULL",
"ofType" => %{"name" => "MutationError"}
}
}
}
}
end
test "MutationError fields items are non-nullable" do