leangen / graphql-spqr

Build a GraphQL service in seconds
Apache License 2.0
1.09k stars 181 forks source link

Apply bean validation annotations only if an appropriate group is enabled [Breaking] #472

Closed kaqqao closed 1 year ago

kaqqao commented 1 year ago

All bean validation annotations (@NotNull, @NotEmpty etc), regardless of groups, currently map into GraphQL non-null types. This occasionally causes problems. It should be made possible to globally toggle what validation groups are active. Annotations without groups should still always apply.

The way to activate a group globally will looks like this:

generator.withTypeMappers((config, mappers) -> mappers.replace(
        //Replace the default NonNullMapper and activate groups Group1 and Group2
        NonNullMapper.class, __ -> new NonNullMapper(Group1.class, Group2.class)))

A built-in group BeanValidationGroupSupport.GraphQL.class is active by default.

Example:

public class Service {
    @GraphQLQuery
    @NotNull(groups = GraphQL.class) //The return type will be non-null in GraphQL, but not other contexts
    public String nonNull(@NotNull(groups = GraphQL.class) String in) { // Likewise for the parameter type
        return in;
    }
}

See NonNullTest.java for more details.

Initial work was done in #471 by @esc-sbarden. This adds some configurability.

Note: This is unfortunately a breaking change as it may result in a schema different than what the previous versions of SPQR had produced.