Skip to content

Commit c340d5a

Browse files
committed
Add all Jakarta annotations
1 parent 7c63ec6 commit c340d5a

File tree

6 files changed

+54
-43
lines changed

6 files changed

+54
-43
lines changed

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@
173173
<groupId>com.graphql-java</groupId>
174174
<artifactId>graphql-java-extended-scalars</artifactId>
175175
<version>21.0</version>
176-
<scope>test</scope>
177176
</dependency>
178177
<dependency>
179178
<groupId>com.graphql-java</groupId>

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,11 @@ public DirectiveProcessor(GraphQLDirective directive, Map<String, Function<Objec
2525
this.builders = builders;
2626
}
2727

28-
public static DirectiveProcessor build(EntityProcessor entityProcessor, Class<? extends Annotation> directive) {
28+
public static DirectiveProcessor build(EntityProcessor entityProcessor, Class<? extends Annotation> directive, boolean isJakarta) {
2929
var builder = GraphQLDirective.newDirective().name(directive.getSimpleName());
3030

3131
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-
) {
32+
if (isJakarta) {
3833
validLocations = new Introspection.DirectiveLocation[] {
3934
Introspection.DirectiveLocation.ARGUMENT_DEFINITION,
4035
Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION };

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,27 @@ class DirectivesSchema {
3434
private final Collection<RestrictTypeFactory<?>> global;
3535
private final Map<Class<? extends Annotation>, DirectiveCaller<?>> targets;
3636
private final Collection<Class<? extends Annotation>> directives;
37+
private final Collection<Class<? extends Annotation>> jakartaDirectives;
3738
private Map<Class<? extends Annotation>, DirectiveProcessor> directiveProcessors;
3839

3940
private DirectivesSchema(
4041
Collection<RestrictTypeFactory<?>> global,
4142
Map<Class<? extends Annotation>, DirectiveCaller<?>> targets,
42-
Collection<Class<? extends Annotation>> directives
43+
Collection<Class<? extends Annotation>> directives,
44+
Collection<Class<? extends Annotation>> jakartaDirectives
4345
) {
4446
this.global = global;
4547
this.targets = targets;
4648
this.directives = directives;
49+
this.jakartaDirectives = jakartaDirectives;
4750
}
4851

4952
//TODO:mess of exceptions
50-
public static DirectivesSchema build(List<RestrictTypeFactory<?>> globalDirectives, Set<Class<?>> directiveTypes, Set<Class<? extends Annotation>> jakartaDirectiveTypes) throws ReflectiveOperationException {
53+
public static DirectivesSchema build(
54+
List<RestrictTypeFactory<?>> globalDirectives,
55+
Set<Class<?>> directiveTypes,
56+
Set<Class<?>> jakartaDirectiveTypes
57+
) throws ReflectiveOperationException {
5158
Map<Class<? extends Annotation>, DirectiveCaller<?>> targets = new HashMap<>();
5259

5360
Collection<Class<? extends Annotation>> allDirectives = new ArrayList<>();
@@ -70,19 +77,23 @@ public static DirectivesSchema build(List<RestrictTypeFactory<?>> globalDirectiv
7077
allDirectives.add((Class<? extends Annotation>) directiveType);
7178
}
7279

73-
allDirectives.addAll(jakartaDirectiveTypes);
80+
Collection<Class<? extends Annotation>> jakartaDirectives = new ArrayList<>();
81+
for (Class<?> jakartaDirectiveType : jakartaDirectiveTypes) {
82+
if (!jakartaDirectiveType.isAnnotation()) {
83+
continue;
84+
}
85+
jakartaDirectives.add((Class<? extends Annotation>) jakartaDirectiveType);
86+
}
7487

75-
return new DirectivesSchema(globalDirectives, targets, allDirectives);
88+
return new DirectivesSchema(globalDirectives, targets, allDirectives, jakartaDirectives);
7689
}
7790

7891
private DirectiveCaller<?> get(Annotation annotation) {
7992
return targets.get(annotation.annotationType());
8093
}
8194

8295
private <T extends Annotation> DataFetcher<?> wrap(DirectiveCaller<T> directive, T annotation, DataFetcher<?> fetcher) {
83-
return env -> {
84-
return directive.process(annotation, env, fetcher);
85-
};
96+
return env -> directive.process(annotation, env, fetcher);
8697
}
8798

8899
public Stream<GraphQLDirective> getSchemaDirective() {
@@ -104,18 +115,18 @@ private DataFetcher<?> wrap(RestrictTypeFactory<?> directive, DataFetcher<?> fet
104115
}
105116
return applyRestrict(restrict, response);
106117
} catch (Exception e) {
107-
if (e instanceof RuntimeException) {
108-
throw (RuntimeException) e;
118+
if (e instanceof RuntimeException runtimeException) {
119+
throw runtimeException;
109120
}
110121
throw new RuntimeException(e);
111122
}
112123
});
113124
}
114125

115126
public boolean target(Method method, TypeMeta meta) {
116-
for (var global : this.global) {
127+
for (var globalRestricts : this.global) {
117128
//TODO: extract class
118-
if (global.extractType().isAssignableFrom(meta.getType())) {
129+
if (globalRestricts.extractType().isAssignableFrom(meta.getType())) {
119130
return true;
120131
}
121132
}
@@ -134,15 +145,15 @@ public DataFetcher<?> wrap(Method method, TypeMeta meta, DataFetcher<?> fetcher)
134145
}
135146
}
136147
for (Annotation annotation : method.getAnnotations()) {
137-
DirectiveCaller directive = (DirectiveCaller) get(annotation);
148+
DirectiveCaller directive = get(annotation);
138149
if (directive != null) {
139150
fetcher = wrap(directive, annotation, fetcher);
140151
}
141152
}
142153
return fetcher;
143154
}
144155

145-
private <T> CompletableFuture<Object> applyRestrict(RestrictType restrict, Object response) {
156+
private CompletableFuture<Object> applyRestrict(RestrictType restrict, Object response) {
146157
if (response instanceof List) {
147158
return restrict.filter((List) response);
148159
} else if (response instanceof Publisher) {
@@ -193,9 +204,10 @@ public void addSchemaDirective(AnnotatedElement element, Class<?> location, Cons
193204
}
194205

195206
public void processDirectives(EntityProcessor ep) { // Replacement of processSDL
196-
Map<Class<? extends Annotation>, DirectiveProcessor> directiveProcessors = new HashMap<>();
207+
Map<Class<? extends Annotation>, DirectiveProcessor> directiveProcessorsList = new HashMap<>();
197208

198-
this.directives.forEach(dir -> directiveProcessors.put(dir, DirectiveProcessor.build(ep, dir)));
199-
this.directiveProcessors = directiveProcessors;
209+
this.directives.forEach(dir -> directiveProcessorsList.put(dir, DirectiveProcessor.build(ep, dir, false)));
210+
this.jakartaDirectives.forEach(dir -> directiveProcessorsList.put(dir, DirectiveProcessor.build(ep, dir, true)));
211+
this.directiveProcessors = directiveProcessorsList;
200212
}
201213
}

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
import com.fleetpin.graphql.builder.annotations.*;
1515
import graphql.schema.GraphQLScalarType;
1616
import graphql.schema.GraphQLSchema;
17-
import jakarta.validation.constraints.Size;
17+
import graphql.scalars.ExtendedScalars;
18+
import jakarta.validation.Constraint;
1819
import org.reflections.Reflections;
1920
import org.reflections.scanners.Scanners;
2021

21-
import java.lang.annotation.Annotation;
2222
import java.lang.reflect.InvocationTargetException;
2323
import java.lang.reflect.Method;
24-
import java.util.ArrayList;
25-
import java.util.HashSet;
26-
import java.util.List;
27-
import java.util.Set;
24+
import java.util.*;
25+
import java.util.stream.Collectors;
26+
27+
import static org.reflections.scanners.Scanners.SubTypes;
2828

2929
public class SchemaBuilder {
3030

@@ -106,8 +106,8 @@ public static Builder builder() {
106106
public static class Builder {
107107

108108
private DataFetcherRunner dataFetcherRunner = (method, fetcher) -> fetcher;
109-
private List<String> classpaths = new ArrayList<>();
110-
private List<GraphQLScalarType> scalars = new ArrayList<>();
109+
private final List<String> classpaths = new ArrayList<>();
110+
private final List<GraphQLScalarType> scalars = new ArrayList<>();
111111

112112
private Builder() {}
113113

@@ -128,7 +128,9 @@ public Builder scalar(GraphQLScalarType scalar) {
128128

129129
public GraphQLSchema.Builder build() {
130130
try {
131-
Reflections reflections = new Reflections(classpaths, Scanners.SubTypes, Scanners.MethodsAnnotated, Scanners.TypesAnnotated);
131+
this.scalar(ExtendedScalars.GraphQLLong);
132+
133+
Reflections reflections = new Reflections(classpaths, SubTypes, Scanners.MethodsAnnotated, Scanners.TypesAnnotated);
132134
Set<Class<? extends Authorizer>> authorizers = reflections.getSubTypesOf(Authorizer.class);
133135
//want to make everything split by package
134136
AuthorizerSchema authorizer = AuthorizerSchema.build(dataFetcherRunner, new HashSet<>(classpaths), authorizers);
@@ -168,10 +170,11 @@ private static DirectivesSchema getDirectivesSchema(Reflections reflections) thr
168170
return DirectivesSchema.build(globalRestricts, directivesTypes, getJakartaAnnotations());
169171
}
170172

171-
private static Set<Class<? extends Annotation>> getJakartaAnnotations() {
172-
Set<Class<? extends Annotation>> list = new HashSet<>();
173-
list.add(Size.class);
174-
return list;
173+
private static Set<Class<?>> getJakartaAnnotations() {
174+
Reflections reflections = new Reflections("jakarta.validation.constraints", SubTypes.filterResultsBy(c -> true));
175+
return reflections.getSubTypesOf(Object.class).stream()
176+
.filter(a -> a.isAnnotationPresent(Constraint.class))
177+
.collect(Collectors.toSet());
175178
}
176179

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

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import graphql.introspection.IntrospectionWithDirectivesSupport;
2020
import graphql.schema.FieldCoordinates;
2121
import graphql.schema.GraphQLSchema;
22+
2223
import java.util.LinkedHashMap;
2324
import java.util.List;
2425
import java.util.Map;
2526
import java.util.stream.Collectors;
27+
2628
import org.junit.jupiter.api.Test;
2729

2830
public class DirectiveTest {
@@ -87,7 +89,7 @@ public void testDirectiveArgumentDefinition() {
8789
List<LinkedHashMap<String, Object>> dir = (List<LinkedHashMap<String, Object>>) ((Map<String, Object>) response.get("__schema")).get("directives");
8890
LinkedHashMap<String, Object> input = dir.stream().filter(map -> map.get("name").equals("Input")).collect(Collectors.toList()).get(0);
8991

90-
assertEquals(8, dir.size());
92+
assertEquals(30, dir.size());
9193
assertEquals("ARGUMENT_DEFINITION", ((List<String>) input.get("locations")).get(0));
9294
assertEquals(1, ((List<Object>) input.get("args")).size());
9395
//getNickname(nickName: String! @Input(value : "TT")): String!

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717

1818
class JakartaValidationDirectiveTest {
1919
@Test
20-
void testJakartaArgumentAnnotationChangedToConstraint() {
20+
void testJakartaSizeAnnotationAddedAsDirective() {
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");
24-
var argument = constraint.getArgument("min");
23+
var directive = name.getArgument("name").getAppliedDirective("Size");
24+
var argument = directive.getArgument("min");
2525
var min = argument.getValue();
2626
assertEquals(3, min);
2727
}
2828

2929
@Test
30-
void testDirectiveArgumentDefinition() {
30+
void testJakartaSizeDirectiveArgumentDefinition() {
3131
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

35-
assertEquals(9, dir.size());
35+
assertEquals(30, dir.size());
3636
assertEquals("ARGUMENT_DEFINITION", ((List<String>) constraint.get("locations")).get(0));
3737
assertEquals("INPUT_FIELD_DEFINITION", ((List<String>) constraint.get("locations")).get(1));
3838
assertEquals(5, ((List<Object>) constraint.get("args")).size());

0 commit comments

Comments
 (0)