Absinthe atom keys VS Phoenix Controller string keys for input params

When input params are passed to changesets from Phoenix Controllers, keys in the map are string keys.

When input params are passed to changesets from Absinthe, keys in the map are atom keys.

In one of my changesets (or in the context), I want to make a small adaptation to the map containing input values received in arguments, and need to add a key. But I will have the following error when casting:

** (Ecto.CastError) expected params to be a map with atoms or string keys, got a map with mixed keys

That’s because if I add the key as an atom, it will complain to be mixed with the map coming from the Controller; if I add the key as a string, it will in turn complain to be mixed with atom keys from Absinthe.

Any advice?

My advice is usually: do not mix user input with your own data. You don’t need to cast everything. You can cast just the user input and e.g. use Ecto.Changeset.change to apply additional changes you have control over.

5 Likes

Excellent advice @LostKobrakai. It makes total sense!