I am using Absinthe.Plug with Phoenix to create a GraphQL API. I have a couple plugs that run and modify the Plug.Conn before Absinthe.Plug processes the request. What I am wondering is, what is the best way to have an error from one of these early Plugs move to the errors
field of the graphql response? I add the errors to the absinthe context like:
Absinthe.Plug.assign_context(conn, :errors, [%{message: error, field: field} | errors])
(errors comes from the existing context so I can add more than one error) so I know they are available later in resolvers or other Absinthe processing.
I know that returning {:error, "An error happened"}
from a resolver will return an errors
field in the response.
I tried a Absinthe.Plugin like:
defmodule PhWeb.Plugins.HandleErrors do
@behaviour Absinthe.Plugin
# Move errors from context into execution (resolution)
def after_resolution(%{context: context} = execution) do
case Map.get(context, :errors) do
nil ->
execution
errors ->
Map.put(execution, :errors, Map.get(execution, :errors, []) ++ errors)
end
end
def before_resolution(execution), do: execution
def pipeline(next_pipeline, _), do: next_pipeline
end
figuring it would set errors on the resolution and those would end up in the GraphQL response but that doesn’t seem to be the case. Is there a way to move errors like this?