Skip to content

Commit 7c63ec6

Browse files
committed
Tidy
1 parent bbd3c68 commit 7c63ec6

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private DirectivesSchema(
4747
}
4848

4949
//TODO:mess of exceptions
50-
public static DirectivesSchema build(List<RestrictTypeFactory<?>> globalDirectives, Set<Class<?>> directiveTypes) throws ReflectiveOperationException {
50+
public static DirectivesSchema build(List<RestrictTypeFactory<?>> globalDirectives, Set<Class<?>> directiveTypes, Set<Class<? extends Annotation>> jakartaDirectiveTypes) throws ReflectiveOperationException {
5151
Map<Class<? extends Annotation>, DirectiveCaller<?>> targets = new HashMap<>();
5252

5353
Collection<Class<? extends Annotation>> allDirectives = new ArrayList<>();
@@ -61,7 +61,7 @@ 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()) {
@@ -70,6 +70,8 @@ public static DirectivesSchema build(List<RestrictTypeFactory<?>> globalDirectiv
7070
allDirectives.add((Class<? extends Annotation>) directiveType);
7171
}
7272

73+
allDirectives.addAll(jakartaDirectiveTypes);
74+
7375
return new DirectivesSchema(globalDirectives, targets, allDirectives);
7476
}
7577

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.reflections.Reflections;
1919
import org.reflections.scanners.Scanners;
2020

21+
import java.lang.annotation.Annotation;
2122
import java.lang.reflect.InvocationTargetException;
2223
import java.lang.reflect.Method;
2324
import java.util.ArrayList;
@@ -162,15 +163,15 @@ private static DirectivesSchema getDirectivesSchema(Reflections reflections) thr
162163
Set<Class<?>> directivesTypes = reflections.getTypesAnnotatedWith(Directive.class);
163164
directivesTypes.addAll(reflections.getTypesAnnotatedWith(DataFetcherWrapper.class));
164165

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

169-
return DirectivesSchema.build(globalRestricts, directivesTypes);
168+
return DirectivesSchema.build(globalRestricts, directivesTypes, getJakartaAnnotations());
170169
}
171170

172-
private static void addAllJakartaAnnotations(Set<Class<?>> directivesTypes) {
173-
directivesTypes.add(Size.class); // use reflection?
171+
private static Set<Class<? extends Annotation>> getJakartaAnnotations() {
172+
Set<Class<? extends Annotation>> list = new HashSet<>();
173+
list.add(Size.class);
174+
return list;
174175
}
175176

176177
private static List<RestrictTypeFactory<?>> getGlobalRestricts(Reflections reflections)

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
import static org.junit.jupiter.api.Assertions.assertEquals;
1717

18-
public class JakartaValidationDirectiveTest {
18+
class JakartaValidationDirectiveTest {
1919
@Test
20-
public void testJakartaArgumentAnnotationChangedToConstraint() {
20+
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"));
2323
var constraint = name.getArgument("name").getAppliedDirective("Size");
@@ -27,29 +27,28 @@ public void testJakartaArgumentAnnotationChangedToConstraint() {
2727
}
2828

2929
@Test
30-
public void testDirectiveArgumentDefinition() {
31-
Map<String, Object> response = execute("query IntrospectionQuery { __schema { directives { name locations args { name } } } }", null).getData();
30+
void testDirectiveArgumentDefinition() {
31+
Map<String, Object> response = execute("query IntrospectionQuery { __schema { directives { name locations args { name } } } }").getData();
3232
List<LinkedHashMap<String, Object>> dir = (List<LinkedHashMap<String, Object>>) ((Map<String, Object>) response.get("__schema")).get("directives");
3333
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));
37-
assertEquals(1, ((List<Object>) constraint.get("args")).size());
38-
assertEquals("{name=validatedBy}", ((List<Object>) constraint.get("args")).getFirst().toString());
39-
//setName(name: String! @Size(min : 3)): Int!
40-
//directive @Constraint(name: String!) on ARGUMENT_DEFINITION
37+
assertEquals("INPUT_FIELD_DEFINITION", ((List<String>) constraint.get("locations")).get(1));
38+
assertEquals(5, ((List<Object>) constraint.get("args")).size());
39+
assertEquals("{name=payload}", ((List<Object>) constraint.get("args")).getFirst().toString());
40+
assertEquals("{name=min}", ((List<Object>) constraint.get("args")).get(1).toString());
41+
assertEquals("{name=max}", ((List<Object>) constraint.get("args")).get(2).toString());
42+
assertEquals("{name=message}", ((List<Object>) constraint.get("args")).get(3).toString());
43+
assertEquals("{name=groups}", ((List<Object>) constraint.get("args")).get(4).toString());
4144
}
4245

43-
private ExecutionResult execute(String query, Map<String, Object> variables) {
46+
private ExecutionResult execute(String query) {
4447
GraphQLSchema preSchema = SchemaBuilder.builder().classpath("com.fleetpin.graphql.builder.type.directive").build().build();
4548
GraphQL schema = GraphQL.newGraphQL(new IntrospectionWithDirectivesSupport().apply(preSchema)).build();
4649

4750
var input = ExecutionInput.newExecutionInput();
4851
input.query(query);
49-
if (variables != null) {
50-
input.variables(variables);
51-
}
52-
ExecutionResult result = schema.execute(input);
53-
return result;
52+
return schema.execute(input);
5453
}
5554
}

0 commit comments

Comments
 (0)