Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add spec compliant errors opt #285

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/absinthe/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ defmodule Absinthe.Plug do
:analyze_complexity,
:max_complexity,
:token_limit,
:transport_batch_payload_key
:transport_batch_payload_key,
:spec_compliant_errors
]
@raw_options [
:analyze_complexity,
Expand All @@ -167,6 +168,7 @@ defmodule Absinthe.Plug do
- `:max_complexity` -- (Optional) Set the maximum allowed complexity of the GraphQL query. If a document’s calculated complexity exceeds the maximum, resolution will be skipped and an error will be returned in the result detailing the calculated and maximum complexities.
- `:token_limit` -- (Optional) Set a limit on the number of allowed parseable tokens in the GraphQL query. Queries with exceedingly high token counts can be expensive to parse. If a query's token count exceeds the set limit, an error will be returned during Absinthe parsing (default: `:infinity`).
- `:transport_batch_payload_key` -- (Optional) Set whether or not to nest Transport Batch request results in a `payload` key. Older clients expected this key to be present, but newer clients have dropped this pattern. (default: `true`)
- `:spec_compliant_errors` -- (Optional) Set whether or not to use the modern error result format. (see: https://spec.graphql.org/June2018/#sec-Errors) (default: `false`)

"""
@type opts :: [
Expand All @@ -188,7 +190,8 @@ defmodule Absinthe.Plug do
before_send: {module, atom},
log_level: Logger.level(),
pubsub: module | nil,
transport_batch_payload_key: boolean
transport_batch_payload_key: boolean,
spec_compliant_errors: boolean
]

@doc """
Expand Down Expand Up @@ -236,6 +239,8 @@ defmodule Absinthe.Plug do

transport_batch_payload_key = Keyword.get(opts, :transport_batch_payload_key, true)

spec_compliant_errors = Keyword.get(opts, :spec_compliant_errors, false)

%{
adapter: adapter,
context: context,
Expand All @@ -250,7 +255,8 @@ defmodule Absinthe.Plug do
log_level: log_level,
pubsub: pubsub,
before_send: before_send,
transport_batch_payload_key: transport_batch_payload_key
transport_batch_payload_key: transport_batch_payload_key,
spec_compliant_errors: spec_compliant_errors
}
end

Expand Down Expand Up @@ -560,8 +566,16 @@ defmodule Absinthe.Plug do
"""
@spec default_pipeline(map, Keyword.t()) :: Absinthe.Pipeline.t()
def default_pipeline(config, pipeline_opts) do
spec_compliant_errors = Map.get(config, :spec_compliant_errors, false)
merged_pipeline_opts = Keyword.merge(
pipeline_opts,
spec_compliant_errors: spec_compliant_errors
)

config.schema_mod
|> Absinthe.Pipeline.for_document(pipeline_opts)
# See: https://github.com/absinthe-graphql/absinthe/issues/925
|> Absinthe.Pipeline.replace(Absinthe.Phase.Document.Result, {Absinthe.Phase.Document.Result, merged_pipeline_opts})
|> Absinthe.Pipeline.insert_after(
Absinthe.Phase.Document.CurrentOperation,
[
Expand Down
32 changes: 32 additions & 0 deletions test/lib/absinthe/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,38 @@ defmodule Absinthe.PlugTest do
end
end

@internal_error_query """
query InternalErrorQuery {
withErrorExtensions
}
"""

test "uses modern error spec" do
opts = Absinthe.Plug.init(schema: TestSchema, spec_compliant_errors: true)

assert %{status: 200, resp_body: resp_body} =
conn(:post, "/", @internal_error_query)
|> put_req_header("content-type", "application/graphql")
|> plug_parser
|> Absinthe.Plug.call(opts)

body = Jason.decode!(resp_body)
assert %{"errors" => [%{"extensions" => %{"code" => 500}}]} = body
end

test "uses legacy error spec" do
opts = Absinthe.Plug.init(schema: TestSchema)

assert %{status: 200, resp_body: resp_body} =
conn(:post, "/", @internal_error_query)
|> put_req_header("content-type", "application/graphql")
|> plug_parser
|> Absinthe.Plug.call(opts)

body = Jason.decode!(resp_body)
assert %{"errors" => [%{"code" => 500}]} = body
end

def test_before_send(conn, val) do
# just for easy testing
send(self(), {:before_send, val})
Expand Down
6 changes: 6 additions & 0 deletions test/support/test_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ defmodule Absinthe.Plug.TestSchema do
raise "complex string must not be resolved"
end
end

field :with_error_extensions, :string do
resolve fn _, _ ->
{:error, message: "Simulated internal error", code: 500}
end
end
end

subscription do
Expand Down
Loading