smallrye-graphql
smallrye-graphql copied to clipboard
ValidationGroup support for Null and NotNull
Is there a way to use validation groups like this? Described in the quarkus guide for rest endpoints https://quarkus.io/guides/validation#validation-groups-for-rest-endpoint-or-service-method-validation
@Null(groups = ValidationGroups.Insert.class)
@NotNull(groups = ValidationGroups.Update.class)
public Long id;
Currently, I always get a validation error, even so I deactivate the automatic validation. The Schema for this results in:
id: BigInteger!
I do not think groups is supported. We can look into adding support. @jmartisk ? w.d.y.t ?
The problem I see is that this basically makes the set of constraints dynamic, while our schema is static. How would we be able to generate a single schema if the field is nullable in some cases, but non-nullable in other cases?
We would have to scan the application to find out that, for example, one class contains a nullable field when used as part of one query, but non-nullable when used with some other query (if we are able to reliably determine this at build-time at all), and therefore we would need two different GraphQL types for that class. That sounds like too much of a mess for relatively little benefit IMO.
Maybe it would be enough to only add support for the null/non-null case. So if there is null and non-null, it would result in a nullable graphql type. And the user needs to validate later.
We have to clearly distinguish between GraphQL (@NonNull) and BeanValidation (@NotNull). Adding conditional (i.e. with groups) BeanValidation annotations to the GraphQL schema doesn't make too much sense, I guess, as it would explode the schema. OTOH, actually validating BeanValidation annotations already works: if you annotate the parameter of, e.g., a query method as @Valid, it gets validated independently of the GraphQL schema validation. You can specify the group to be used for the validation with a @ConvertGroup (from Default) annotation, i.e.:
@Mutation public Hero createHero(@Valid @ConvertGroup(to = ValidationGroups.Insert.class) Hero hero) { ... }
The three BeanValidation annotations @NotNull, @NotEmpty, and @NotBlank are treated as synonyms for the MP GraphQL @NonNull annotation in SmallRye GraphQL, so they unconditionally show up in the GraphQL schema. In hindsight this might not be a 100% complete solution. I have the impression that we'll need to fully ignore them for the schema, if they have any groups parameters, so they would be nullable in the schema, but still can be validated with @Valid. But this will lead to many dazzled faces, because this behavior is non-obvious. Maybe it would be better to just remove these exceptions; and they are not part of the spec anyway.