Skip to content

Commit bbd3c68

Browse files
committed
Can add Size annotation
1 parent b36f3a1 commit bbd3c68

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
lines changed

src/main/java/com/fleetpin/graphql/builder/DirectiveProcessor.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,29 @@ public DirectiveProcessor(GraphQLDirective directive, Map<String, Function<Objec
2727

2828
public static DirectiveProcessor build(EntityProcessor entityProcessor, Class<? extends Annotation> directive) {
2929
var builder = GraphQLDirective.newDirective().name(directive.getSimpleName());
30-
var validLocations = directive.getAnnotation(Directive.class).value();
30+
31+
Introspection.DirectiveLocation[] validLocations;
32+
if (
33+
!directive.isAnnotationPresent(Directive.class) &&
34+
directive
35+
.getName()
36+
.startsWith("jakarta.validation.constraints") // Jakarta constraint annotations don't have the Directive annotation, so we need to manually add in the locations
37+
) {
38+
validLocations = new Introspection.DirectiveLocation[] {
39+
Introspection.DirectiveLocation.ARGUMENT_DEFINITION,
40+
Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION };
41+
} else {
42+
validLocations = directive.getAnnotation(Directive.class).value();
43+
44+
// Check for repeatable tag in annotation and add it
45+
builder.repeatable(directive.getAnnotation(Directive.class).repeatable());
46+
}
47+
3148
// loop through and add valid locations
3249
for (Introspection.DirectiveLocation location : validLocations) {
3350
builder.validLocation(location);
3451
}
3552

36-
// Check for repeatable tag in annotation and add it
37-
builder.repeatable(directive.getAnnotation(Directive.class).repeatable());
38-
3953
// Go through each argument and add name/type to directive
4054
var methods = directive.getDeclaredMethods();
4155
Map<String, Function<Object, GraphQLAppliedDirectiveArgument>> builders = new HashMap<>();

src/main/java/com/fleetpin/graphql/builder/DirectivesSchema.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static DirectivesSchema build(List<RestrictTypeFactory<?>> globalDirectiv
6161
}
6262
continue;
6363
}
64-
if (!directiveType.isAnnotationPresent(Directive.class)) {
64+
if (!directiveType.isAnnotationPresent(Directive.class) && !directiveType.getName().startsWith("jakarta.validation.constraints")) {
6565
continue;
6666
}
6767
if (!directiveType.isAnnotation()) {

src/main/java/com/fleetpin/graphql/builder/MethodProcessor.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
import com.fleetpin.graphql.builder.annotations.*;
44
import graphql.GraphQLContext;
5+
import graphql.GraphQLError;
6+
import graphql.execution.DataFetcherResult;
57
import graphql.schema.*;
68
import graphql.schema.GraphQLFieldDefinition.Builder;
9+
import graphql.validation.rules.ValidationRules;
710

811
import java.lang.annotation.Annotation;
912
import java.lang.reflect.InvocationTargetException;
1013
import java.lang.reflect.Method;
1114
import java.lang.reflect.Modifier;
15+
import java.util.List;
1216
import java.util.function.Function;
1317

1418
import static com.fleetpin.graphql.builder.EntityUtil.isContext;
@@ -61,8 +65,7 @@ void process(AuthorizerSchema authorizer, Method method) throws ReflectiveOperat
6165
object.field(process(authorizer, coordinates, null, method));
6266
}
6367

64-
Builder process(AuthorizerSchema authorizer, FieldCoordinates coordinates, TypeMeta parentMeta, Method method)
65-
throws InvocationTargetException, IllegalAccessException {
68+
Builder process(AuthorizerSchema authorizer, FieldCoordinates coordinates, TypeMeta parentMeta, Method method) {
6669
GraphQLFieldDefinition.Builder field = GraphQLFieldDefinition.newFieldDefinition();
6770

6871
entityProcessor.addSchemaDirective(method, method.getDeclaringClass(), field::withAppliedDirective);
@@ -133,8 +136,17 @@ private DataFetcher<?> buildDataFetcher(TypeMeta meta, Method method) {
133136
resolvers[i] = buildResolver(name, argMeta, parameter.getAnnotations());
134137
}
135138

139+
ValidationRules validationRules = ValidationRules
140+
.newValidationRules()
141+
.build();
142+
136143
DataFetcher<?> fetcher = env -> {
137144
try {
145+
List<GraphQLError> errors = validationRules.runValidationRules(env);
146+
if (!errors.isEmpty()) {
147+
return DataFetcherResult.newResult().errors(errors).data(null).build();
148+
}
149+
138150
Object[] args = new Object[resolvers.length];
139151
for (int i = 0; i < resolvers.length; i++) {
140152
args[i] = resolvers[i].apply(env);

src/main/java/com/fleetpin/graphql/builder/SchemaBuilder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.fleetpin.graphql.builder.annotations.*;
1515
import graphql.schema.GraphQLScalarType;
1616
import graphql.schema.GraphQLSchema;
17+
import jakarta.validation.constraints.Size;
1718
import org.reflections.Reflections;
1819
import org.reflections.scanners.Scanners;
1920

@@ -161,11 +162,17 @@ private static DirectivesSchema getDirectivesSchema(Reflections reflections) thr
161162
Set<Class<?>> directivesTypes = reflections.getTypesAnnotatedWith(Directive.class);
162163
directivesTypes.addAll(reflections.getTypesAnnotatedWith(DataFetcherWrapper.class));
163164

165+
addAllJakartaAnnotations(directivesTypes);
166+
164167
List<RestrictTypeFactory<?>> globalRestricts = getGlobalRestricts(reflections);
165168

166169
return DirectivesSchema.build(globalRestricts, directivesTypes);
167170
}
168171

172+
private static void addAllJakartaAnnotations(Set<Class<?>> directivesTypes) {
173+
directivesTypes.add(Size.class); // use reflection?
174+
}
175+
169176
private static List<RestrictTypeFactory<?>> getGlobalRestricts(Reflections reflections)
170177
throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
171178
Set<Class<?>> restrict = reflections.getTypesAnnotatedWith(Restrict.class);
@@ -198,7 +205,7 @@ private static List<RestrictTypeFactory<?>> getGlobalRestricts(Reflections refle
198205
globalRestricts.add(factory);
199206
}
200207
}
201-
208+
202209
return globalRestricts;
203210
}
204211
}

src/test/java/com/fleetpin/graphql/builder/JakartaValidationDirectiveTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class JakartaValidationDirectiveTest {
2020
public void testJakartaArgumentAnnotationChangedToConstraint() {
2121
GraphQL schema = GraphQL.newGraphQL(SchemaBuilder.build("com.fleetpin.graphql.builder.type.directive")).build();
2222
var name = schema.getGraphQLSchema().getFieldDefinition(FieldCoordinates.coordinates(schema.getGraphQLSchema().getMutationType(), "setName"));
23-
var constraint = name.getArgument("name").getAppliedDirective("Constraint");
23+
var constraint = name.getArgument("name").getAppliedDirective("Size");
2424
var argument = constraint.getArgument("min");
2525
var min = argument.getValue();
2626
assertEquals(3, min);
@@ -30,7 +30,7 @@ public void testJakartaArgumentAnnotationChangedToConstraint() {
3030
public void testDirectiveArgumentDefinition() {
3131
Map<String, Object> response = execute("query IntrospectionQuery { __schema { directives { name locations args { name } } } }", null).getData();
3232
List<LinkedHashMap<String, Object>> dir = (List<LinkedHashMap<String, Object>>) ((Map<String, Object>) response.get("__schema")).get("directives");
33-
LinkedHashMap<String, Object> constraint = dir.stream().filter(map -> map.get("name").equals("Constraint")).collect(Collectors.toList()).get(0);
33+
LinkedHashMap<String, Object> constraint = dir.stream().filter(map -> map.get("name").equals("Size")).collect(Collectors.toList()).get(0);
3434

3535
assertEquals(9, dir.size());
3636
assertEquals("ARGUMENT_DEFINITION", ((List<String>) constraint.get("locations")).get(0));

0 commit comments

Comments
 (0)