Deleting all trailing spaces on all strings in graphql mutation

I have the following problem:
I can imagine users inserting information with trailing spaces, f.e. name "John " instead of just “John” in an Absinthe mutation. I don’t want to rely on people not doing that mistake (either the user of the api user/frontend).
Is there a smart way to ensure all string Absinthe query parameters have a function like String.trim_trailing/2 automatically applied to them?

Thank you!

1 Like

There are several ways to do this in Absinthe, custom Scalar (Absinthe.Type.Scalar — absinthe v1.7.1), custom Middleware (Absinthe.Middleware — absinthe v1.7.1).

Let me offer a different way of thinking. (I’m assuming that you want to normalize the data before persisting it.)

If you think of Absinthe (GraphQL) as one of potentially multiple interfaces then it makes more sense to normalize the data at the core (most likely your contexts). That way if you ever need an second interface, REST API, LiveView, etc you do not need to implement normalizing the data in that layer too.

@sasajuric explains it better here: Towards Maintainable Elixir: The Core and the Interface | by Saša Jurić | Very Big Things | Medium

p.s. if you are already expecting user data to contain extra spaces than maybe String.trim/1 fits better

1 Like