Simple Authentication components strategy with VueJs

A lot of application are using authentication, and we use many variants to handle it properly. The component system of VueJs is powerful, so let’s use it to simply handle authentication process. Note…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




GraphQL validation using directives

How to extend the SDL to add concise rules to your schema

Out of the box, GraphQL supports validating your inputs based on type information. For example, you can ensure that an argument is a particular scalar type, such as String or Int. You can also enforce the shape of object arguments using input object types. While this prevents a large class of possible errors, this doesn’t cater to more complex validation needs like enforcing the length of a string, or the size of a number.

The above enforces all fields are strings, and ensures they are not null. However, we’ve got some additional constraints that come from the database that we need to be wary of. In our server, the backend database is using a VARCHAR(255) field for all Strings. This means providing a field greater than 255 characters will cause an error!

Today, people most commonly write this kind of validation logic in their resolver functions or models. However, this means we can’t easily see our validation logic, and we have to write some repetitive code to verify the same kinds of conditions over and over.

Here’s how your SDL will look like with some constraint directives added in:

Before we move on, it’s good to mention that this isn’t a silver bullet solution. More complex validation rules, such as those which rely on state (e.g. does this entity exist?) will need to remain elsewhere in your code.

Let’s break down the example above, field by field…

Another way to write this could be to split it into multiple @constraint directives. Note that directives are resolved right to left. For example, maxLength would be resolved before format in the following field definition: email: String! @constraint(format: "email") @constraint(maxLength: 255)

On a different field, it is confirming the last name is a maximum of 255 characters and also matches a regular expression. In this instance, alphanumeric only (sorry double barrelled surnames!).

When a consumer sends a request which breaks these rules, the application will respond with an error message.

When the value of each decorated field is parsed, the validation rules are executed. For example, here’s the code for minLength:

If all validation rules pass, ConstraintString/ConstraintNumber executes the original scalar's parseValue and returns it. This allows wrapped scalars to execute their own parseValue function, enabling support for custom scalar types!

I hope this inspires you to develop and publish your own custom directives!

Add a comment

Related posts:

Really Active Learning!

From the moment I could sit upright on my own, I’ve been a fidgeter. In fact, my inability to keep still led to so many incidents that my mom had me walk through stores with my hands on my stomach in…

Iteration Nine. Chapter 1

Jack has been staring at his monitor for some time now, just staring at it blankly, as if waiting for something magical to appear. The blue desktop screen was plain, almost empty save for a few icons…

Apuntes Charla Identidad Video Juegos Chilenos

Generar nuevas reflexiones sobre “el último Arte”, involucrar a Analistas, Educadores, Gamers, Desarrolladores buscando ciencia , arte, revolución Periodista Universidad de Concepción impacto de los…