14
14
import com .fleetpin .graphql .builder .annotations .*;
15
15
import graphql .schema .GraphQLScalarType ;
16
16
import graphql .schema .GraphQLSchema ;
17
+ import org .reflections .Reflections ;
18
+ import org .reflections .scanners .Scanners ;
19
+
20
+ import java .lang .reflect .InvocationTargetException ;
17
21
import java .lang .reflect .Method ;
18
22
import java .util .ArrayList ;
19
23
import java .util .HashSet ;
20
24
import java .util .List ;
21
25
import java .util .Set ;
22
- import org .reflections .Reflections ;
23
- import org .reflections .scanners .Scanners ;
24
26
25
27
public class SchemaBuilder {
26
28
@@ -75,7 +77,7 @@ private graphql.schema.GraphQLSchema.Builder build(Set<Class<? extends SchemaCon
75
77
builder .subscription (subscriptions );
76
78
}
77
79
78
- directives .getSchemaDirective ().forEach (directive -> builder . additionalDirective ( directive ) );
80
+ directives .getSchemaDirective ().forEach (builder :: additionalDirective );
79
81
80
82
for (var schema : schemaConfiguration ) {
81
83
this .directives .addSchemaDirective (schema , schema , builder ::withSchemaAppliedDirective );
@@ -131,41 +133,7 @@ public GraphQLSchema.Builder build() {
131
133
132
134
Set <Class <? extends SchemaConfiguration >> schemaConfiguration = reflections .getSubTypesOf (SchemaConfiguration .class );
133
135
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 );
169
137
170
138
Set <Class <?>> types = reflections .getTypesAnnotatedWith (Entity .class );
171
139
@@ -178,7 +146,7 @@ public GraphQLSchema.Builder build() {
178
146
endPoints .addAll (queries );
179
147
180
148
types .removeIf (t -> t .getDeclaredAnnotation (Entity .class ) == null );
181
- types .removeIf (t -> t . isAnonymousClass () );
149
+ types .removeIf (Class :: isAnonymousClass );
182
150
183
151
return new SchemaBuilder (dataFetcherRunner , scalars , directivesSchema , authorizer )
184
152
.processTypes (types )
@@ -188,5 +156,50 @@ public GraphQLSchema.Builder build() {
188
156
throw new RuntimeException (e );
189
157
}
190
158
}
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
+ }
191
204
}
192
205
}
0 commit comments