1
1
/**
2
2
* Copyright 2016 Yurii Rashkovskii
3
- *
3
+ * <p>
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
6
* You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
7
+ * <p>
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ * <p>
10
10
* Unless required by applicable law or agreed to in writing, software
11
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
16
17
17
import graphql .ExecutionResult ;
18
18
import graphql .GraphQL ;
19
+ import graphql .annotations .annotationTypes .GraphQLDirectiveDefinition ;
19
20
import graphql .annotations .annotationTypes .GraphQLDirectives ;
20
21
import graphql .annotations .annotationTypes .GraphQLField ;
21
22
import graphql .annotations .annotationTypes .GraphQLName ;
22
23
import graphql .annotations .directives .AnnotationsDirectiveWiring ;
23
24
import graphql .annotations .directives .AnnotationsWiringEnvironment ;
24
25
import graphql .annotations .directives .Directive ;
26
+ import graphql .annotations .directives .creation .DirectiveAnnotation ;
25
27
import graphql .annotations .directives .creation .DirectiveLocations ;
28
+ import graphql .annotations .directives .creation .DirectiveWiring ;
29
+ import graphql .annotations .processor .DirectiveAndWiring ;
26
30
import graphql .annotations .processor .GraphQLAnnotations ;
27
31
import graphql .annotations .processor .exceptions .GraphQLAnnotationsException ;
28
32
import graphql .annotations .processor .util .CodeRegistryUtil ;
31
35
import org .testng .annotations .BeforeMethod ;
32
36
import org .testng .annotations .Test ;
33
37
38
+ import java .lang .annotation .ElementType ;
39
+ import java .lang .annotation .Retention ;
40
+ import java .lang .annotation .RetentionPolicy ;
41
+ import java .lang .annotation .Target ;
34
42
import java .util .Map ;
35
43
36
44
import static graphql .Scalars .GraphQLBoolean ;
@@ -103,47 +111,46 @@ public GraphQLInputObjectType onInputObjectType(AnnotationsWiringEnvironment env
103
111
104
112
public static class Query {
105
113
@ GraphQLField
106
- @ GraphQLDirectives (@ Directive (name = "upperCase" , wiringClass = UpperWiring . class , argumentsValues = {"true" }))
114
+ @ GraphQLDirectives (@ Directive (name = "upperCase" , argumentsValues = {"true" }))
107
115
public static String name () {
108
116
return "yarin" ;
109
117
}
110
118
111
119
@ GraphQLField
112
- @ GraphQLDirectives (@ Directive (name = "upperCase" , wiringClass = UpperWiring . class , argumentsValues = {"false" }))
120
+ @ GraphQLDirectives (@ Directive (name = "upperCase" , argumentsValues = {"false" }))
113
121
public static String nameWithFalse () {
114
122
return "yarin" ;
115
123
}
116
124
}
117
125
118
126
public static class Query2 {
119
127
@ GraphQLField
120
- @ GraphQLDirectives (@ Directive (name = "upperCase" , wiringClass = UpperWiring . class ))
128
+ @ GraphQLDirectives (@ Directive (name = "upperCase" ))
121
129
public static String nameWithNoArgs () {
122
130
return "yarin" ;
123
131
}
124
132
}
125
133
126
134
public static class Query3 {
127
135
@ GraphQLField
128
- @ GraphQLDirectives ({@ Directive (name = "upperCase" , wiringClass = UpperWiring . class , argumentsValues = {"true" }),
129
- @ Directive (name = "suffix" , wiringClass = SuffixWiring . class , argumentsValues = {"coolSuffix" })})
136
+ @ GraphQLDirectives ({@ Directive (name = "upperCase" , argumentsValues = {"true" }),
137
+ @ Directive (name = "suffix" , argumentsValues = {"coolSuffix" })})
130
138
public static String name () {
131
139
return "yarin" ;
132
140
}
133
141
}
134
142
135
143
public static class Query4 {
136
144
@ GraphQLField
137
- public static String nameWithArgument (@ GraphQLDirectives ({@ Directive (name = "suffix" ,
138
- wiringClass = SuffixWiring .class , argumentsValues = {"coolSuffixForArg" })})
145
+ public static String nameWithArgument (@ GraphQLDirectives ({@ Directive (name = "suffix" , argumentsValues = {"coolSuffixForArg" })})
139
146
@ GraphQLName ("extensionArg" ) String extensionArg ) {
140
147
return "yarin" + extensionArg ;
141
148
}
142
149
}
143
150
144
151
public static class InputObject {
145
152
@ GraphQLField
146
- @ GraphQLDirectives ({@ Directive (name = "suffix" , wiringClass = SuffixWiring . class , argumentsValues = {"coolSuffix" })})
153
+ @ GraphQLDirectives ({@ Directive (name = "suffix" , argumentsValues = {"coolSuffix" })})
147
154
private String a ;
148
155
149
156
@ GraphQLField
@@ -162,12 +169,79 @@ public static String nameWithInputObject(@GraphQLName("inputObject") InputObject
162
169
}
163
170
}
164
171
172
+
173
+ public static class QueryAlternative {
174
+ @ GraphQLName ("suffix" )
175
+ @ GraphQLDirectiveDefinition (wiring = SuffixWiring .class )
176
+ @ DirectiveLocations ({Introspection .DirectiveLocation .FIELD_DEFINITION , Introspection .DirectiveLocation .ARGUMENT_DEFINITION })
177
+ public static void suffixDirective (@ GraphQLName ("suffix" ) String suffix ) {
178
+
179
+ }
180
+
181
+ @ GraphQLField
182
+ @ GraphQLDirectives ({
183
+ @ Directive (name = "suffix" , argumentsValues = {"coolSuffix" })})
184
+ public static String name () {
185
+ return "yarin" ;
186
+ }
187
+ }
188
+
189
+
190
+ @ Target ({ElementType .TYPE , ElementType .METHOD })
191
+ @ Retention (RetentionPolicy .RUNTIME )
192
+ @ DirectiveAnnotation
193
+ @ DirectiveWiring (UpperWiring .class )
194
+ @ DirectiveLocations (Introspection .DirectiveLocation .FIELD_DEFINITION )
195
+ @ GraphQLName ("upper" )
196
+ public @interface ToUpperCase {
197
+ boolean isActive () default true ;
198
+ }
199
+
200
+
201
+ public static class QueryAnnotation {
202
+ @ GraphQLField
203
+ @ ToUpperCase
204
+ public static String name () {
205
+ return "yarin" ;
206
+ }
207
+ }
208
+
209
+
210
+ @ Test
211
+ public void queryName_javaAnnotationDirective_wiringIsActivated () throws Exception {
212
+ this .graphQLAnnotations .directiveViaAnnotation (ToUpperCase .class );
213
+ GraphQLObjectType object = this .graphQLAnnotations .object (QueryAnnotation .class );
214
+ GraphQLCodeRegistry codeRegistry = graphQLAnnotations .getContainer ().getCodeRegistryBuilder ().build ();
215
+
216
+ GraphQLSchema schema = newSchema ().query (object ).codeRegistry (codeRegistry ).build ();
217
+
218
+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute ("query { name }" );
219
+ assertTrue (result .getErrors ().isEmpty ());
220
+ assertEquals (((Map <String , String >) result .getData ()).get ("name" ).toString (), "YARIN" );
221
+ }
222
+
223
+
224
+
225
+ @ Test
226
+ public void queryName_directivesInAlternativeWayCreation_wiringIsActivated () throws Exception {
227
+ this .graphQLAnnotations .directives (QueryAlternative .class );
228
+ GraphQLObjectType object = this .graphQLAnnotations .object (QueryAlternative .class );
229
+ GraphQLCodeRegistry codeRegistry = graphQLAnnotations .getContainer ().getCodeRegistryBuilder ().build ();
230
+
231
+ GraphQLSchema schema = newSchema ().query (object ).codeRegistry (codeRegistry ).build ();
232
+
233
+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute ("query { name }" );
234
+ assertTrue (result .getErrors ().isEmpty ());
235
+ assertEquals (((Map <String , String >) result .getData ()).get ("name" ).toString (), "yarincoolSuffix" );
236
+ }
237
+
238
+
165
239
@ Test
166
240
public void queryNameWithInputObject_directivesProvidedToRegistry_wiringOfInputObjectIsActivated () {
167
241
GraphQLDirective suffixDirective = GraphQLDirective .newDirective ().name ("suffix" ).argument (builder -> builder .name ("suffix" ).type (GraphQLString ))
168
242
.validLocations (Introspection .DirectiveLocation .INPUT_FIELD_DEFINITION , Introspection .DirectiveLocation .FIELD_DEFINITION ).build ();
169
243
170
- this .graphQLAnnotations .getContainer ().getDirectiveRegistry ().put (suffixDirective .getName (), suffixDirective );
244
+ this .graphQLAnnotations .getContainer ().getDirectiveRegistry ().put (suffixDirective .getName (), new DirectiveAndWiring ( suffixDirective , SuffixWiring . class ) );
171
245
GraphQLObjectType object = this .graphQLAnnotations .object (Query5 .class );
172
246
GraphQLCodeRegistry codeRegistry = graphQLAnnotations .getContainer ().getCodeRegistryBuilder ().build ();
173
247
GraphQLSchema schema = newSchema ().query (object ).codeRegistry (codeRegistry ).build ();
@@ -180,7 +254,7 @@ public void queryNameWithInputObject_directivesProvidedToRegistry_wiringOfInputO
180
254
public void queryNameWithArgument_directivesProvidedToRegistry_wiringOfArgumentIsActivated () {
181
255
GraphQLDirective suffixDirective = GraphQLDirective .newDirective ().name ("suffix" ).argument (builder -> builder .name ("suffix" ).type (GraphQLString ))
182
256
.validLocations (Introspection .DirectiveLocation .FIELD_DEFINITION , Introspection .DirectiveLocation .ARGUMENT_DEFINITION ).build ();
183
- this .graphQLAnnotations .getContainer ().getDirectiveRegistry ().put (suffixDirective .getName (), suffixDirective );
257
+ this .graphQLAnnotations .getContainer ().getDirectiveRegistry ().put (suffixDirective .getName (), new DirectiveAndWiring ( suffixDirective , SuffixWiring . class ) );
184
258
GraphQLObjectType object = this .graphQLAnnotations .object (Query4 .class );
185
259
GraphQLCodeRegistry codeRegistry = graphQLAnnotations .getContainer ().getCodeRegistryBuilder ().build ();
186
260
GraphQLSchema schema = newSchema ().query (object ).codeRegistry (codeRegistry ).build ();
@@ -200,6 +274,7 @@ public void queryName_noDirectivesProvidedToRegistry_exceptionIsThrown() throws
200
274
201
275
@ GraphQLName ("upperCase" )
202
276
@ DirectiveLocations (Introspection .DirectiveLocation .FIELD_DEFINITION )
277
+ @ DirectiveWiring (UpperWiring .class )
203
278
public static class UpperCase {
204
279
boolean isActive ;
205
280
}
@@ -221,7 +296,7 @@ public void queryName_directivesProvidedToRegistry_wiringIsActivated() throws Ex
221
296
public void queryNameWithFalse_directivesProvidedToRegistry_wiringIsActivated () throws Exception {
222
297
GraphQLDirective upperCase = newDirective ().name ("upperCase" ).argument (builder -> builder .name ("isActive" ).type (GraphQLBoolean ))
223
298
.validLocations (Introspection .DirectiveLocation .FIELD_DEFINITION ).build ();
224
- this .graphQLAnnotations .getContainer ().getDirectiveRegistry ().put (upperCase .getName (), upperCase );
299
+ this .graphQLAnnotations .getContainer ().getDirectiveRegistry ().put (upperCase .getName (), new DirectiveAndWiring ( upperCase , UpperWiring . class ) );
225
300
GraphQLObjectType object = this .graphQLAnnotations .object (Query .class );
226
301
GraphQLCodeRegistry codeRegistry = graphQLAnnotations .getContainer ().getCodeRegistryBuilder ().build ();
227
302
GraphQLSchema schema = newSchema ().query (object ).codeRegistry (codeRegistry ).build ();
@@ -235,7 +310,7 @@ public void queryNameWithFalse_directivesProvidedToRegistry_wiringIsActivated()
235
310
public void queryNameWithNoArgs_directivesProvidedToRegistry_wiringIsActivated () throws Exception {
236
311
GraphQLDirective upperCase = newDirective ().name ("upperCase" ).argument (builder -> builder .name ("isActive" ).type (GraphQLBoolean ).defaultValue (true ))
237
312
.validLocations (Introspection .DirectiveLocation .FIELD_DEFINITION ).build ();
238
- this .graphQLAnnotations .getContainer ().getDirectiveRegistry ().put (upperCase .getName (), upperCase );
313
+ this .graphQLAnnotations .getContainer ().getDirectiveRegistry ().put (upperCase .getName (), new DirectiveAndWiring ( upperCase , UpperWiring . class ) );
239
314
GraphQLObjectType object = this .graphQLAnnotations .object (Query2 .class );
240
315
GraphQLCodeRegistry codeRegistry = graphQLAnnotations .getContainer ().getCodeRegistryBuilder ().build ();
241
316
GraphQLSchema schema = newSchema ().query (object ).codeRegistry (codeRegistry ).build ();
@@ -249,7 +324,7 @@ public void queryNameWithNoArgs_directivesProvidedToRegistry_wiringIsActivated()
249
324
public void queryNameWithNoArgs_noDefaultValue_exceptionIsThrown () throws Exception {
250
325
GraphQLDirective upperCase = newDirective ().name ("upperCase" ).argument (builder -> builder .name ("isActive" ).type (GraphQLBoolean ))
251
326
.validLocations (Introspection .DirectiveLocation .FIELD_DEFINITION ).build ();
252
- this .graphQLAnnotations .getContainer ().getDirectiveRegistry ().put (upperCase .getName (), upperCase );
327
+ this .graphQLAnnotations .getContainer ().getDirectiveRegistry ().put (upperCase .getName (), new DirectiveAndWiring ( upperCase , UpperWiring . class ) );
253
328
GraphQLObjectType object = this .graphQLAnnotations .object (Query2 .class );
254
329
GraphQLCodeRegistry codeRegistry = graphQLAnnotations .getContainer ().getCodeRegistryBuilder ().build ();
255
330
@@ -264,8 +339,8 @@ public void queryName_chainedDirectives_wiringIsActivatedInCorrectOrder() throws
264
339
.validLocations (Introspection .DirectiveLocation .FIELD_DEFINITION ).build ();
265
340
GraphQLDirective suffixDirective = GraphQLDirective .newDirective ().name ("suffix" ).argument (builder -> builder .name ("suffix" ).type (GraphQLString ))
266
341
.validLocations (Introspection .DirectiveLocation .FIELD_DEFINITION , Introspection .DirectiveLocation .ARGUMENT_DEFINITION ).build ();
267
- this .graphQLAnnotations .getContainer ().getDirectiveRegistry ().put (upperCase .getName (), upperCase );
268
- this .graphQLAnnotations .getContainer ().getDirectiveRegistry ().put (suffixDirective .getName (), suffixDirective );
342
+ this .graphQLAnnotations .getContainer ().getDirectiveRegistry ().put (upperCase .getName (), new DirectiveAndWiring ( upperCase , UpperWiring . class ) );
343
+ this .graphQLAnnotations .getContainer ().getDirectiveRegistry ().put (suffixDirective .getName (), new DirectiveAndWiring ( suffixDirective , SuffixWiring . class ) );
269
344
GraphQLObjectType object = this .graphQLAnnotations .object (Query3 .class );
270
345
GraphQLCodeRegistry codeRegistry = graphQLAnnotations .getContainer ().getCodeRegistryBuilder ().build ();
271
346
0 commit comments