tcoopman

tcoopman

Changing value to camelCase with middleware in absinthe

I’m trying to change the values of some fields to camelCase with middleware like this:

defmodule Middlewares.CamelCaseField do
  @behaviour Absinthe.Middleware
  def call(resolution, _) do
    %{resolution | errors: Enum.flat_map(resolution.errors, &handle_error/1)}
  end

  defp handle_error(%{field: field} = error) do
    %{error | field: camel(field)}
  end

  defp handle_error([]), do: []

  defp handle_error(error) when is_list(error) do
    case Keyword.get(error, :field) do
      nil -> error
      field -> Keyword.put(error, :field, camel(field))
    end
  end

  defp handle_error(error), do: [error]

  def camel(f), do: Atom.to_string(f) |> Recase.to_camel()
end
...
defmodule Schema do
  ....

  def middleware(middleware, field, object) do
    middleware ++ [Middlewares.CamelCaseField]
  end

But now when I run my code I get errors like these:

** (Protocol.UndefinedError) protocol String.Chars not implemented for {:code, :unknown_error}. This protocol is implemented for: Postgrex.Copy, Postgrex.Query, Decimal, DateTime, Date, Version, NaiveDateTime, List, Atom, Time, BitString, Integer, URI, Version.Requirement, Float.

If I remove my middleware everything works, so I’m probably doing something wrong with the middleware, but don’t know what exactly.

Marked As Solved

tcoopman

tcoopman

Found my mistake. I needed to wrap the result of handle_error(error) when is_list(error) in a list as well, so:

  defp handle_error(error) when is_list(error) do
    error = case Keyword.get(error, :field) do
      nil -> error
      field -> Keyword.put(error, :field, camel(field))
    end
    [error]
  end

Also Liked

axelson

axelson

Scenic Core Team

Note that you can no longer set the adapter in the configuration, but instead need to do it in the router: Update Adapter documentation to not refer to configuration by axelson · Pull Request #575 · absinthe-graphql/absinthe · GitHub

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You can access the adapter within the third argument to the resolution function, so you could use that adapter’s to_external_name function on your own error information to do the transformation.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New

Other popular topics Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement