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
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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








