Spring Boot GraphQL Tutorial #32 – Schema Directive Validation

Опубликовано: 29 Октябрь 2024
на канале: Philip Starritt
5,280
58

In this episode we try out the graphql-java-extended-validation library version 15.0.1.

This library provides schema validation via a range of directives that follow the JSR-303 bean validation name/pattern.

In order to activate the directive validation with spring boot graphql, you must first create a ValidationSchemaWiring bean.

There are a range of annotations you can add to your schema. As of 2020/11/29 they are:
@AssertFalse, @AssertTrue, @DecimalMax, @DecimalMin, @Digits, @Expression, @Max, @Min, @Negative, @NegativeOrZero, @NotBlank, @NotEmpty, @Pattern, @Positive, @PositiveOrZero, @Range and @Size.

To customize the error messages:
Create a ValidationMessages.properties file, add it to the classpath
Add K,V pairs to ValidationMessages.properties in the format: key:custom error message
Set the @Directive(message: “key”) t

Things I found about this library
Dependency 15.0.3/2 is not available on common maven repositories.
Schema Validation does not trigger on nested Input Types (deal-breaker for me)
All directives are validated. Allowing for a full object validation.
Locale support and externalized custom messages
Powerful expression language with @Expression
Overall good quality of annotations. Practising what they preach.
Integates easily with spring boot graphql. But no auto-config based on the dependency.

Hope you enjoyed this tutorial!
Sure, why not.. give it a go :)

See you in the next one,
Philip


Spring Boot GraphQL Java: https://github.com/graphql-java-kicks...


Graphql java extended validation github: https://github.com/graphql-java/graph...

GraphQL Schema directives: https://www.graphql-java.com/document...

GraphQL Queries and Mutations: https://graphql.org/learn/queries/