Skip to content

Commit 4f35a65

Browse files
committed
Refactoring
1 parent 394a9a4 commit 4f35a65

File tree

4 files changed

+70
-112
lines changed

4 files changed

+70
-112
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
import com.fleetpin.graphql.builder.annotations.Directive;
44
import graphql.introspection.Introspection;
5-
import graphql.schema.*;
5+
import graphql.schema.GraphQLAppliedDirective;
6+
import graphql.schema.GraphQLAppliedDirectiveArgument;
7+
import graphql.schema.GraphQLArgument;
8+
import graphql.schema.GraphQLDirective;
9+
610
import java.lang.annotation.Annotation;
711
import java.lang.reflect.InvocationTargetException;
812
import java.lang.reflect.Method;
9-
import java.util.*;
13+
import java.util.HashMap;
14+
import java.util.Map;
1015
import java.util.function.Consumer;
1116
import java.util.function.Function;
1217

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,17 @@
1717
import graphql.schema.DataFetchingEnvironment;
1818
import graphql.schema.GraphQLAppliedDirective;
1919
import graphql.schema.GraphQLDirective;
20+
import org.reactivestreams.Publisher;
21+
2022
import java.lang.annotation.Annotation;
2123
import java.lang.reflect.AnnotatedElement;
2224
import java.lang.reflect.InvocationTargetException;
2325
import java.lang.reflect.Method;
24-
import java.lang.reflect.ParameterizedType;
2526
import java.util.*;
2627
import java.util.concurrent.CompletableFuture;
2728
import java.util.concurrent.CompletionStage;
28-
import java.util.concurrent.ExecutionException;
2929
import java.util.function.Consumer;
30-
import java.util.stream.Collectors;
3130
import java.util.stream.Stream;
32-
import org.reactivestreams.Publisher;
3331

3432
class DirectivesSchema {
3533

@@ -179,23 +177,6 @@ private <T> CompletableFuture<Object> applyRestrict(RestrictType restrict, Objec
179177
}
180178
}
181179

182-
private static <T> CompletableFuture<List<T>> all(List<CompletableFuture<T>> toReturn) {
183-
return CompletableFuture
184-
.allOf(toReturn.toArray(CompletableFuture[]::new))
185-
.thenApply(__ ->
186-
toReturn
187-
.stream()
188-
.map(m -> {
189-
try {
190-
return m.get();
191-
} catch (InterruptedException | ExecutionException e) {
192-
throw new RuntimeException(e);
193-
}
194-
})
195-
.collect(Collectors.toList())
196-
);
197-
}
198-
199180
public void addSchemaDirective(AnnotatedElement element, Class<?> location, Consumer<GraphQLAppliedDirective> builder) {
200181
for (Annotation annotation : element.getAnnotations()) {
201182
var processor = this.directiveProcessors.get(annotation.annotationType());

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

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
11
package com.fleetpin.graphql.builder;
22

3-
import static com.fleetpin.graphql.builder.EntityUtil.isContext;
4-
5-
import com.fleetpin.graphql.builder.annotations.Directive;
6-
import com.fleetpin.graphql.builder.annotations.GraphQLDeprecated;
7-
import com.fleetpin.graphql.builder.annotations.GraphQLDescription;
8-
import com.fleetpin.graphql.builder.annotations.Mutation;
9-
import com.fleetpin.graphql.builder.annotations.Query;
10-
import com.fleetpin.graphql.builder.annotations.Subscription;
3+
import com.fleetpin.graphql.builder.annotations.*;
114
import graphql.GraphQLContext;
12-
import graphql.schema.DataFetcher;
13-
import graphql.schema.DataFetchingEnvironment;
14-
import graphql.schema.FieldCoordinates;
15-
import graphql.schema.GraphQLAppliedDirective;
16-
import graphql.schema.GraphQLAppliedDirectiveArgument;
17-
import graphql.schema.GraphQLArgument;
18-
import graphql.schema.GraphQLCodeRegistry;
19-
import graphql.schema.GraphQLFieldDefinition;
5+
import graphql.schema.*;
206
import graphql.schema.GraphQLFieldDefinition.Builder;
21-
import graphql.schema.GraphQLObjectType;
7+
228
import java.lang.annotation.Annotation;
239
import java.lang.reflect.InvocationTargetException;
2410
import java.lang.reflect.Method;
2511
import java.lang.reflect.Modifier;
2612
import java.util.function.Function;
2713

14+
import static com.fleetpin.graphql.builder.EntityUtil.isContext;
15+
2816
class MethodProcessor {
2917

3018
private final DataFetcherRunner dataFetcherRunner;
3119
private final EntityProcessor entityProcessor;
32-
private final DirectivesSchema diretives;
20+
private final DirectivesSchema directives;
3321

3422
private final GraphQLCodeRegistry.Builder codeRegistry;
3523

@@ -40,7 +28,7 @@ class MethodProcessor {
4028
public MethodProcessor(DataFetcherRunner dataFetcherRunner, EntityProcessor entityProcessor, DirectivesSchema diretives) {
4129
this.dataFetcherRunner = dataFetcherRunner;
4230
this.entityProcessor = entityProcessor;
43-
this.diretives = diretives;
31+
this.directives = diretives;
4432
this.codeRegistry = GraphQLCodeRegistry.newCodeRegistry();
4533

4634
this.graphQuery = GraphQLObjectType.newObject();
@@ -109,47 +97,18 @@ Builder process(AuthorizerSchema authorizer, FieldCoordinates coordinates, TypeM
10997
argument.description(description.value());
11098
}
11199

112-
for (Annotation annotation : parameter.getAnnotations()) {
113-
// Check to see if the annotation is a directive
114-
if (!annotation.annotationType().isAnnotationPresent(Directive.class)) {
115-
continue;
116-
}
117-
var annotationType = annotation.annotationType();
118-
// Get the values out of the directive annotation
119-
var methods = annotationType.getDeclaredMethods();
120-
121-
// Get the applied directive and add it to the argument
122-
var appliedDirective = getAppliedDirective(annotation, annotationType, methods);
123-
argument.withAppliedDirective(appliedDirective);
124-
}
100+
entityProcessor.addSchemaDirective(parameter, method.getDeclaringClass(), argument::withAppliedDirective);
125101

126102
argument.name(EntityUtil.getName(parameter.getName(), parameter));
127103
// TODO: argument.defaultValue(defaultValue)
128104
field.argument(argument);
129105
}
130106

131-
DataFetcher<?> fetcher = buildFetcher(diretives, authorizer, method, meta);
107+
DataFetcher<?> fetcher = buildFetcher(directives, authorizer, method, meta);
132108
codeRegistry.dataFetcher(coordinates, fetcher);
133109
return field;
134110
}
135111

136-
private GraphQLAppliedDirective getAppliedDirective(Annotation annotation, Class<? extends Annotation> annotationType, Method[] methods)
137-
throws IllegalAccessException, InvocationTargetException {
138-
var appliedDirective = new GraphQLAppliedDirective.Builder().name(annotationType.getSimpleName());
139-
for (var definedMethod : methods) {
140-
var name = definedMethod.getName();
141-
var value = definedMethod.invoke(annotation);
142-
if (value == null) {
143-
continue;
144-
}
145-
146-
TypeMeta innerMeta = new TypeMeta(null, definedMethod.getReturnType(), definedMethod.getGenericReturnType());
147-
var argumentType = entityProcessor.getEntity(innerMeta).getInputType(innerMeta, definedMethod.getAnnotations());
148-
appliedDirective.argument(GraphQLAppliedDirectiveArgument.newArgument().name(name).type(argumentType).valueProgrammatic(value).build());
149-
}
150-
return appliedDirective.build();
151-
}
152-
153112
private <T extends Annotation> DataFetcher<?> buildFetcher(DirectivesSchema diretives, AuthorizerSchema authorizer, Method method, TypeMeta meta) {
154113
DataFetcher<?> fetcher = buildDataFetcher(meta, method);
155114
fetcher = diretives.wrap(method, meta, fetcher);

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

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
import com.fleetpin.graphql.builder.annotations.*;
1515
import graphql.schema.GraphQLScalarType;
1616
import graphql.schema.GraphQLSchema;
17+
import org.reflections.Reflections;
18+
import org.reflections.scanners.Scanners;
19+
20+
import java.lang.reflect.InvocationTargetException;
1721
import java.lang.reflect.Method;
1822
import java.util.ArrayList;
1923
import java.util.HashSet;
2024
import java.util.List;
2125
import java.util.Set;
22-
import org.reflections.Reflections;
23-
import org.reflections.scanners.Scanners;
2426

2527
public class SchemaBuilder {
2628

@@ -75,7 +77,7 @@ private graphql.schema.GraphQLSchema.Builder build(Set<Class<? extends SchemaCon
7577
builder.subscription(subscriptions);
7678
}
7779

78-
directives.getSchemaDirective().forEach(directive -> builder.additionalDirective(directive));
80+
directives.getSchemaDirective().forEach(builder::additionalDirective);
7981

8082
for (var schema : schemaConfiguration) {
8183
this.directives.addSchemaDirective(schema, schema, builder::withSchemaAppliedDirective);
@@ -131,41 +133,7 @@ public GraphQLSchema.Builder build() {
131133

132134
Set<Class<? extends SchemaConfiguration>> schemaConfiguration = reflections.getSubTypesOf(SchemaConfiguration.class);
133135

134-
Set<Class<?>> directivesTypes = reflections.getTypesAnnotatedWith(Directive.class);
135-
directivesTypes.addAll(reflections.getTypesAnnotatedWith(DataFetcherWrapper.class));
136-
137-
Set<Class<?>> restrict = reflections.getTypesAnnotatedWith(Restrict.class);
138-
Set<Class<?>> restricts = reflections.getTypesAnnotatedWith(Restricts.class);
139-
List<RestrictTypeFactory<?>> globalRestricts = new ArrayList<>();
140-
141-
for (var r : restrict) {
142-
Restrict annotation = EntityUtil.getAnnotation(r, Restrict.class);
143-
var factoryClass = annotation.value();
144-
var factory = factoryClass.getConstructor().newInstance();
145-
if (!factory.extractType().isAssignableFrom(r)) {
146-
throw new RuntimeException(
147-
"Restrict annotation does match class applied to targets" + factory.extractType() + " but was on class " + r
148-
);
149-
}
150-
globalRestricts.add(factory);
151-
}
152-
153-
for (var r : restricts) {
154-
Restricts annotations = EntityUtil.getAnnotation(r, Restricts.class);
155-
for (Restrict annotation : annotations.value()) {
156-
var factoryClass = annotation.value();
157-
var factory = factoryClass.getConstructor().newInstance();
158-
159-
if (!factory.extractType().isAssignableFrom(r)) {
160-
throw new RuntimeException(
161-
"Restrict annotation does match class applied to targets" + factory.extractType() + " but was on class " + r
162-
);
163-
}
164-
globalRestricts.add(factory);
165-
}
166-
}
167-
168-
DirectivesSchema directivesSchema = DirectivesSchema.build(globalRestricts, directivesTypes); // Entry point for directives
136+
DirectivesSchema directivesSchema = getDirectivesSchema(reflections);
169137

170138
Set<Class<?>> types = reflections.getTypesAnnotatedWith(Entity.class);
171139

@@ -178,7 +146,7 @@ public GraphQLSchema.Builder build() {
178146
endPoints.addAll(queries);
179147

180148
types.removeIf(t -> t.getDeclaredAnnotation(Entity.class) == null);
181-
types.removeIf(t -> t.isAnonymousClass());
149+
types.removeIf(Class::isAnonymousClass);
182150

183151
return new SchemaBuilder(dataFetcherRunner, scalars, directivesSchema, authorizer)
184152
.processTypes(types)
@@ -188,5 +156,50 @@ public GraphQLSchema.Builder build() {
188156
throw new RuntimeException(e);
189157
}
190158
}
159+
160+
private static DirectivesSchema getDirectivesSchema(Reflections reflections) throws ReflectiveOperationException {
161+
Set<Class<?>> directivesTypes = reflections.getTypesAnnotatedWith(Directive.class);
162+
directivesTypes.addAll(reflections.getTypesAnnotatedWith(DataFetcherWrapper.class));
163+
164+
List<RestrictTypeFactory<?>> globalRestricts = getGlobalRestricts(reflections);
165+
166+
return DirectivesSchema.build(globalRestricts, directivesTypes);
167+
}
168+
169+
private static List<RestrictTypeFactory<?>> getGlobalRestricts(Reflections reflections)
170+
throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
171+
Set<Class<?>> restrict = reflections.getTypesAnnotatedWith(Restrict.class);
172+
Set<Class<?>> restricts = reflections.getTypesAnnotatedWith(Restricts.class);
173+
List<RestrictTypeFactory<?>> globalRestricts = new ArrayList<>();
174+
175+
for (var r : restrict) {
176+
Restrict annotation = EntityUtil.getAnnotation(r, Restrict.class);
177+
var factoryClass = annotation.value();
178+
var factory = factoryClass.getConstructor().newInstance();
179+
if (!factory.extractType().isAssignableFrom(r)) {
180+
throw new RuntimeException(
181+
"Restrict annotation does match class applied to targets" + factory.extractType() + " but was on class " + r
182+
);
183+
}
184+
globalRestricts.add(factory);
185+
}
186+
187+
for (var r : restricts) {
188+
Restricts annotations = EntityUtil.getAnnotation(r, Restricts.class);
189+
for (Restrict annotation : annotations.value()) {
190+
var factoryClass = annotation.value();
191+
var factory = factoryClass.getConstructor().newInstance();
192+
193+
if (!factory.extractType().isAssignableFrom(r)) {
194+
throw new RuntimeException(
195+
"Restrict annotation does match class applied to targets" + factory.extractType() + " but was on class " + r
196+
);
197+
}
198+
globalRestricts.add(factory);
199+
}
200+
}
201+
202+
return globalRestricts;
203+
}
191204
}
192205
}

0 commit comments

Comments
 (0)