From 7e26f4226add4fff3f996ed676c7c8b410117f93 Mon Sep 17 00:00:00 2001 From: Bryan Joseph Date: Fri, 27 Sep 2024 09:19:26 -0500 Subject: [PATCH 1/3] Treat maps returned from config/2 as response --- lib/absinthe/phase/document/result.ex | 9 ++++ test/absinthe/execution/subscription_test.exs | 42 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/absinthe/phase/document/result.ex b/lib/absinthe/phase/document/result.ex index dd6a0928a8..651555b78d 100644 --- a/lib/absinthe/phase/document/result.ex +++ b/lib/absinthe/phase/document/result.ex @@ -109,6 +109,15 @@ defmodule Absinthe.Phase.Document.Result do defp field_name(%{alias: name}), do: name defp field_name(%{name: name}), do: name + defp format_error(%Phase.Error{message: %{message: _message} = error_object} = error, _opts) do + if Enum.empty?(error.locations) do + error_object + else + locations = Enum.flat_map(error.locations, &format_location/1) + Map.put_new(error_object, :locations, locations) + end + end + defp format_error(%Phase.Error{locations: []} = error, opts) do error_object = %{message: error.message} diff --git a/test/absinthe/execution/subscription_test.exs b/test/absinthe/execution/subscription_test.exs index 6159cd38ee..ddf1e5c002 100644 --- a/test/absinthe/execution/subscription_test.exs +++ b/test/absinthe/execution/subscription_test.exs @@ -214,6 +214,18 @@ defmodule Absinthe.Execution.SubscriptionTest do {:ok, topic: "*", context_id: "*", document_id: op_name} end end + + field :config_error, :string do + config fn _, _ -> + {:error, "failed"} + end + end + + field :config_error_with_map, :string do + config fn _, _ -> + {:error, %{message: "failed", extensions: %{code: "FAILED"}}} + end + end end mutation do @@ -617,6 +629,36 @@ defmodule Absinthe.Execution.SubscriptionTest do assert_receive(:batch_get_group) end + @query """ + subscription Example { + configError + } + """ + test "config errors" do + assert {:ok, %{errors: [%{message: "failed"}]}} = + run_subscription( + @query, + Schema, + variables: %{}, + context: %{pubsub: PubSub} + ) + end + + @query """ + subscription Example { + configErrorWithMap + } + """ + test "config errors with a map" do + assert {:ok, %{errors: [%{message: "failed", extensions: %{code: "FAILED"}}]}} = + run_subscription( + @query, + Schema, + variables: %{}, + context: %{pubsub: PubSub} + ) + end + describe "subscription_ids" do @query """ subscription { From 2cfe89ce9f6dc6884df444527294782de9a7a4a5 Mon Sep 17 00:00:00 2001 From: Bryan Joseph Date: Wed, 2 Oct 2024 10:02:42 -0500 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f5fa94204..0031f1ff18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## Unreleased +- Feature: [Treat maps returned from config/2 as response](https://github.com/absinthe-graphql/absinthe/pull/1341) - Feature: [Add async option to Absinthe.Subscription](https://github.com/absinthe-graphql/absinthe/pull/1329) - Bug Fix: [Avoid table scans on registry](https://github.com/absinthe-graphql/absinthe/pull/1330) - Big Fix: [Unregsiter duplicate (listening to the same topic) subscriptions individually](https://github.com/absinthe-graphql/absinthe/pull/1336) From ed1f86d2ee4e609fa5afeb8ae23c02d535d194a8 Mon Sep 17 00:00:00 2001 From: Bryan Joseph Date: Wed, 2 Oct 2024 11:31:01 -0500 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0031f1ff18..34aea9eda7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # Changelog ## Unreleased -- Feature: [Treat maps returned from config/2 as response](https://github.com/absinthe-graphql/absinthe/pull/1341) +- Feature: [Allow config/2 to send errors in spec compliant format](https://github.com/absinthe-graphql/absinthe/pull/1341) - Feature: [Add async option to Absinthe.Subscription](https://github.com/absinthe-graphql/absinthe/pull/1329) - Bug Fix: [Avoid table scans on registry](https://github.com/absinthe-graphql/absinthe/pull/1330) - Big Fix: [Unregsiter duplicate (listening to the same topic) subscriptions individually](https://github.com/absinthe-graphql/absinthe/pull/1336)