Skip to content

Commit b36f3a1

Browse files
committed
Revert "WIP"
This reverts commit dc120e4.
1 parent dc120e4 commit b36f3a1

File tree

5 files changed

+10
-45
lines changed

5 files changed

+10
-45
lines changed

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

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,15 @@ 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-
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-
30+
var validLocations = directive.getAnnotation(Directive.class).value();
4831
// loop through and add valid locations
4932
for (Introspection.DirectiveLocation location : validLocations) {
5033
builder.validLocation(location);
5134
}
5235

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

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,14 @@ public static DirectivesSchema build(List<RestrictTypeFactory<?>> globalDirectiv
6161
}
6262
continue;
6363
}
64-
if (!directiveType.isAnnotationPresent(Directive.class) && !directiveType.getName().startsWith("jakarta.validation.constraints")) {
64+
if (!directiveType.isAnnotationPresent(Directive.class)) {
6565
continue;
6666
}
6767
if (!directiveType.isAnnotation()) {
6868
throw new RuntimeException("@Directive Annotation must only be placed on annotations");
6969
}
7070
allDirectives.add((Class<? extends Annotation>) directiveType);
7171
}
72-
// allDirectives.addAll(extra);
73-
// we could pass the constraint annotations around as an extra list, separate to the other directive.
7472

7573
return new DirectivesSchema(globalDirectives, targets, allDirectives);
7674
}

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

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

33
import com.fleetpin.graphql.builder.annotations.*;
44
import graphql.GraphQLContext;
5-
import graphql.GraphQLError;
6-
import graphql.execution.DataFetcherResult;
75
import graphql.schema.*;
86
import graphql.schema.GraphQLFieldDefinition.Builder;
9-
import graphql.validation.rules.ValidationRules;
107

118
import java.lang.annotation.Annotation;
129
import java.lang.reflect.InvocationTargetException;
1310
import java.lang.reflect.Method;
1411
import java.lang.reflect.Modifier;
15-
import java.util.List;
1612
import java.util.function.Function;
1713

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

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

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

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

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

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

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

165-
addAllJakartaAnnotations(directivesTypes);
166-
167164
List<RestrictTypeFactory<?>> globalRestricts = getGlobalRestricts(reflections);
168165

169166
return DirectivesSchema.build(globalRestricts, directivesTypes);
170167
}
171168

172-
private static void addAllJakartaAnnotations(Set<Class<?>> directivesTypes) {
173-
directivesTypes.add(Size.class); // use reflection?
174-
}
175-
176169
private static List<RestrictTypeFactory<?>> getGlobalRestricts(Reflections reflections)
177170
throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
178171
Set<Class<?>> restrict = reflections.getTypesAnnotatedWith(Restrict.class);
@@ -205,7 +198,7 @@ private static List<RestrictTypeFactory<?>> getGlobalRestricts(Reflections refle
205198
globalRestricts.add(factory);
206199
}
207200
}
208-
201+
209202
return globalRestricts;
210203
}
211204
}

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("Size");
23+
var constraint = name.getArgument("name").getAppliedDirective("Constraint");
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("Size")).collect(Collectors.toList()).get(0);
33+
LinkedHashMap<String, Object> constraint = dir.stream().filter(map -> map.get("name").equals("Constraint")).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)