Validate graphql query/mutation arguments on Elixir

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.

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

2 Likes

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.

2 Likes

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.

2 Likes

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

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

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

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.

Wow thank you, I will check it!

“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

2 Likes