kirega

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:

  1. 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)
....
  1. 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
  1. 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 Switch mode

kirega

kirega

I found this library that does the job.

https://github.com/Bounceapp/absinthe_constraints

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement