sandorbedo

sandorbedo

How to return partial responses in Absinthe?

Hi,

How should an Absinthe.Resolution struct look like when I want to return both data and errors from Absinthe?

This is now allowed by the GraphQL spec., but whenever I put something into the errors field of the Absinthe.Resolution struct, the data part of the response disappears from the response. This is what I try to do in a middleware without much success:

  def call(resolution, params) do
    resolution
    |> Absinthe.Resolution.put_result({:error, params.errors})
    |> Absinthe.Resolution.put_result({:ok, params.data})
  end

…and in the handler function I simply return {:middleware, MyMiddleware, %{data: ..., errors: ...}}. It doesn’t make any difference when I try to manipulate the resolution struct directly, not via put_result/2.

What’s wrong with this approach?

Is it even possible to return data and errors in a single response using Absinthe?

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hi @sandorbedo this is a common misreading of the spec. This is talking about the overall response shape, not the resolution of any specific field. Absinthe absolutely lets you do the following:

{
  fieldwithNoError
  fieldWithError
}

fieldwithNoError will return a result even if fieldWithError returns an error. What you’re asking about is for an individual field to return both data and errors. That’s governed by this section of the spec: Handling Field Errors which unambiguously states:

If a field error is raised while resolving a field, it is handled as though the field returned null,
and the error must be added to the “errors” list in the response.

Absinthe is compliant here.

EDIT: A quick check of the 2025 draft shows that this remains unchanged in the latest draft too: GraphQL

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

To answer your last question first:

You absolutely can:

object :hero do
  field :id, :id
  field :name, :string, resolve: &name_or_error/3
end

defp name_or_error(parent, _, _) do
  case Map.fetch(parent, :name) do
    {:ok, name} -> {:ok, name}
    :error ->  {:error, "Name for character with ID #{parent.id} could not be fetched."}
  end
end

It’s worth pointing out as well that in your example what is the path of the error? It’s "path": ["hero", "heroFriends", 1, "name"]. This means that the error was raised while resolving this field. If it was raised from the heroFriends field you would see the path simply be ["hero", "heroFriends"].

This is not correct. Every field has a resolver. Every field is “resolved”. Objects do not have resolvers at all, only fields do. More to the point, the spec is talking about the “process” of resolving a field, which isn’t implementation specific. See GraphQL. Every field goes through this execution and resolution process recursively.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement