Skip to content

Commit

Permalink
Merge pull request #1341 from absinthe-graphql/bryanj/config_error_re…
Browse files Browse the repository at this point in the history
…turn_maps

Allow config/2 to send errors in spec compliant format
  • Loading branch information
bryanjos authored Oct 2, 2024
2 parents 3d0823b + ed1f86d commit a093119
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- 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)
Expand Down
9 changes: 9 additions & 0 deletions lib/absinthe/phase/document/result.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
42 changes: 42 additions & 0 deletions test/absinthe/execution/subscription_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a093119

Please sign in to comment.