Skip to content

Commit

Permalink
[GRAPH-1099] Allows a 3-tuple to be return for config errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanjos committed Sep 26, 2024
1 parent 0a89be5 commit f7f69fd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/absinthe/phase/subscription/subscribe_self.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,31 @@ defmodule Absinthe.Phase.Subscription.SubscribeSelf do
{:ok, config}

{:error, msg} ->
error = %Phase.Error{
phase: __MODULE__,
message: msg,
locations: [field.source_location]
}
{:error, create_config_error(field, msg)}

{:error, error}
{:error, msg, extra} ->
{:error, create_config_error(field, msg, extra)}

val ->
raise """
Invalid return from config function!
A config function must return `{:ok, config}` or `{:error, msg}`. You returned:
A config function must return `{:ok, config}`, `{:error, msg}` or `{:error, msg, extra}`. You returned:
#{inspect(val)}
"""
end
end

defp create_config_error(field, msg, extra \\ %{}) do
%Phase.Error{
phase: __MODULE__,
message: msg,
locations: [field.source_location],
extra: extra
}
end

defp get_field_keys(%{schema_node: schema_node} = _field, config) do
name = schema_node.identifier

Expand Down
53 changes: 53 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_extra, :string do
config fn _, _ ->
{:error, "failed", %{code: "FAILED"}}
end
end
end

mutation do
Expand Down Expand Up @@ -584,6 +596,47 @@ 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 {
configErrorWithExtra
}
"""
test "config errors with extra" do
assert {:ok, %{errors: [%{message: "failed", code: "FAILED"}]}} =
run_subscription(
@query,
Schema,
variables: %{},
context: %{pubsub: PubSub}
)
end

test "config errors with extra and spec_compliant_errors turned on" do
assert {:ok, %{errors: [%{message: "failed", extensions: %{code: "FAILED"}}]}} =
run_subscription(
@query,
Schema,
variables: %{},
context: %{pubsub: PubSub},
spec_compliant_errors: true
)
end

describe "subscription_ids" do
@query """
subscription {
Expand Down

0 comments on commit f7f69fd

Please sign in to comment.