From 7386a5b627a98d5af9a83c50b4726513e0ac274a Mon Sep 17 00:00:00 2001 From: Alan Heywood Date: Sat, 4 May 2024 11:57:02 +1000 Subject: [PATCH] improvement: make mutation result errors list non-nullable (#144) * chore: regenerate cheat sheets * improvement: make mutation result errors list non-nullable --- documentation/dsls/DSL:-AshGraphql.Domain.md | 4 +- lib/resource/resource.ex | 8 +- test/errors_test.exs | 103 ++++++++++++++----- 3 files changed, 84 insertions(+), 31 deletions(-) diff --git a/documentation/dsls/DSL:-AshGraphql.Domain.md b/documentation/dsls/DSL:-AshGraphql.Domain.md index 95e96a9..593bc88 100644 --- a/documentation/dsls/DSL:-AshGraphql.Domain.md +++ b/documentation/dsls/DSL:-AshGraphql.Domain.md @@ -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 diff --git a/lib/resource/resource.ex b/lib/resource/resource.ex index 56b3c83..79c2e2d 100644 --- a/lib/resource/resource.ex +++ b/lib/resource/resource.ex @@ -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__) diff --git a/test/errors_test.exs b/test/errors_test.exs index 12300f1..40fdeb1 100644 --- a/test/errors_test.exs +++ b/test/errors_test.exs @@ -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