tcoopman

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.

Showing Posts 1 to 8

tcoopman

tcoopman OP

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
pierreabreup

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.exs file. Example:
config :absinthe, adapter: Absinthe.Adapter.LanguageConventions

I’m using it and works very fine.

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

pierreabreup

pierreabreup

or in the endpoint.ex when the entire API is base on GraphQL

plug Absinthe.Plug,
    schema: ContentProxyGraphql.Schema,
    adapter: Absinthe.Adapter.LanguageConventions,
    json_codec: Jason
tcoopman

tcoopman OP

I’m not sure the adapter can help translating values in error results? If I have %{errors: [%{field: "my_field"}]} translate the my_field value to myField?

pierreabreup

pierreabreup

hum… probably I misunderstood your question :smiley:
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

tcoopman OP

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

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
kszambelanczyk
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
RemyXRenard
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
velrest
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
samoloth
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
FlyingNoodle
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
nseaSeb
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 Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews