tcoopman
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.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
tcoopman
Found my mistake. I needed to wrap the result of
handle_error(error) when is_list(error)in a list as well, so:pierreabreup
@tcoopman Have you tried Absinthe.Adapter.LanguageConventions ( absinthe/guides/adapters.md at main · absinthe-graphql/absinthe · GitHub) ?
You have to add this configuration in your
config.exsfile. Example:config :absinthe, adapter: Absinthe.Adapter.LanguageConventionsI’m using it and works very fine.
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
pierreabreup
or in the endpoint.ex when the entire API is base on GraphQL
tcoopman
I’m not sure the adapter can help translating values in error results? If I have
%{errors: [%{field: "my_field"}]}translate themy_fieldvalue tomyField?pierreabreup
hum… probably I misunderstood your question
The LanguageConventions adapter only changes the field name case.
However, in my opinion, sounds strange to change user values in output moment, except for date format or enumeration values.
If your values are statics, perhaps would be more expressive to use Graphl Enum (absinthe/guides/tutorial/complex-arguments.md at main · absinthe-graphql/absinthe · GitHub)
If enum doesn’t fit your needs, the code you have made is nice!
tcoopman
my use case is that I let use ecto changeset to validate parts of the code. But this returns fields in snake case. So before I send it to the user if map my errors into output that is more consistent with the input query.
benwilson512
You can access the adapter within the third argument to the resolution function, so you could use that adapter’s
to_external_namefunction on your own error information to do the transformation.