Validate graphql query/mutation arguments on Elixir

graphql
absinthe

#1

I’m looking for a module or something that can be used for validating a graphql query/mutation on Elixir. I’ve researched for it and stumble across a cool middleware called yup-middleware: https://github.com/JCMais/graphql-yup-middleware but it is not for elixir

Is there a same module to in elixir that can be used to validate? I know I can use Absinthe.Middleware but I was just wondering if there is such a module that is easier to use.


#2

Can’t you use Ecto changesets? A quick search yielded Kronky. But I’m not sure if this is what you are looking for.


#3

GraphQL seems to be a strong typed query language, so absinthe will validate all the parameters for you.
So, when you say “validate” what does that mean? Validate upon business logic? This probably should be done in Ecto Changesets or resolvers.


#4

GraphQL types can be leveraged for data validation to a large degree, provided you make use of the tools it provides (i.e. enums). You can get additional validation through custom types like email or url, but you’ll need to write the parsing and dumping yourself, and in a way that emits helpful errors.

There are plenty of places where GraphQL/Absinthe can’t help you though. For example, if you want to enforce a string is a certain size, or that a particular field is filled in but another isn’t, or a provided ID actually exists in the database. You’ll need to fall back to validating with Ecto and your application’s business rules.


#5

If using changeset what I concern is that, there will be a lot of business logic before entering the schema right? What if I want to validate like a lot of formating for example name, email, url. I think Kronky is some module that helps you to return error message back to graphql


#6

Yes Graphql already provided with basic validate parameters as in :string, or :integer. But what I need is something a bit more like String is must be length of with format x. Like that


#7

Bagasprstyadi,

Check-out this talk where the presenter uses Ecto Changeset to create a validation layer.

https://youtu.be/k_xDi7zAcNM

Hope this helps.

Best regards,

Joaquin Alcerro


#8

It may be not so obvious, but you don’t need string to be with minimum length. You need password to be minimum length.

And this leads us two 2 different solutons:

  1. You can create GraphQL type Password, it’s easily achieved with Absinthe library itself.

  2. You can pass password as string, but validate it on your business logic. This can be done with your Ecto structures.


#9

Wow thank you, I will check it!


#10

“You can create GraphQL type Password

This is interesting, so I can create custom type right, and then the graphql will validate it? Awesome, Im gonna look into that