kirega
Absinthe - Validation of values in input object/args
Hi all,
I ran into this scenario while running Absinthe GQL with Phoenix, I have this query that look as a follows:
object :admin_queries do
field :get_user, non_null(:user) do
arg :input, non_null(:user_input)
resolve(&Resolvers.User.get_user/2)
end
end
input_object :user_input do
field :id, non_null(:id)
end
When I tried to run this query, I realized that is allowed for the following to pass
query getUser( {
input { id: "" }
}) {
id
name
}
correctly so, since an "" != null.
I tried to reason about how to handle the validation of id (and in extension other fields too), and I came to the following plausible solutions:
- Use middleware, but its too deep in the resolution and it was a challenge to use when you need to validate nested inputs. As an example, see what that looks like below:
defmodule Gql.Middleware.ValidateUuid do
@behaviour Absinthe.Middleware
def call(%Resolution{arguments: %{merchant_id: merchant_id}} = resolution, _),
do: validate_uuid(resolution, merchant_id, :merchant_id)
def call(%Resolution{arguments: %{delivery_id: delivery_id}} = resolution, _),
do: validate_uuid(resolution, delivery_id, :delivery_id)
def call(%Resolution{arguments: %{user_id: user_id}} = resolution, _),
do: validate_uuid(resolution, user_id, :user_id)
def call(%Resolution{arguments: %{id: id}} = resolution, _),
do: validate_uuid(resolution, id, :id)
def call(%Resolution{arguments: %{input: %{user_id: user_id}}} = resolution, _),
do: validate_uuid(resolution, user_id, :user_id)
....
- Build my own scalar type, but that would result in breaking changes in my client and the naming of the type will change, which is not ideal, within which I would use the parse macro to handle my validations.
scalar :uuid, name: "UUID" do
parse(&parse_custom/1)
end
- Handle all validation in the resolver, it would result to a lot more changes in each resolver.
My question is, is there a way to handle this more seamlessly?
In an ideal world, i think it would be awesome to have the middleware available in args or field
in input_object.
Thanks in advance
Marked As Solved
kirega
I found this library that does the job.
Popular in Questions
can someone please explain to me how Enum.reduce works with maps
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I have a list say
x = ["23gh", "56kh", "97mh"]
I would like to pass each element to Val in each iteration.
Say, in iteration 1 -------...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir
iex(2)...
New
Other popular topics
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New








