5151import static graphql .schema .GraphQLDirective .newDirective ;
5252import static graphql .schema .GraphQLSchema .newSchema ;
5353import static org .testng .Assert .assertEquals ;
54+ import static org .testng .Assert .assertNotNull ;
5455import static org .testng .Assert .assertTrue ;
5556
5657public class GraphQLDirectivesTest {
@@ -83,6 +84,13 @@ public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment)
8384 }
8485
8586 public static class SuffixWiring implements AnnotationsDirectiveWiring {
87+ @ Override
88+ public GraphQLInputObjectField onInputObjectField (AnnotationsWiringEnvironment environment ) {
89+ GraphQLInputObjectField field = (GraphQLInputObjectField ) environment .getElement ();
90+ String suffix = (String ) environment .getDirective ().getArgument ("suffix" ).getValue ();
91+ return field .transform (builder -> builder .name (field .getName ()+suffix ));
92+ }
93+
8694 @ Override
8795 public GraphQLFieldDefinition onField (AnnotationsWiringEnvironment environment ) {
8896 GraphQLFieldDefinition field = (GraphQLFieldDefinition ) environment .getElement ();
@@ -95,6 +103,20 @@ public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment)
95103 })));
96104 return field .transform (builder -> builder .dataFetcher (dataFetcher ));
97105 }
106+
107+ @ Override
108+ public GraphQLArgument onArgument (AnnotationsWiringEnvironment environment ) {
109+ GraphQLArgument element = (GraphQLArgument ) environment .getElement ();
110+ String suffix = (String ) environment .getDirective ().getArgument ("suffix" ).getValue ();
111+ return element .transform (builder -> builder .name (element .getName () + suffix ));
112+ }
113+
114+ @ Override
115+ public GraphQLInputObjectType onInputObjectType (AnnotationsWiringEnvironment environment ) {
116+ GraphQLInputObjectType element = (GraphQLInputObjectType ) environment .getElement ();
117+ String suffix = (String ) environment .getDirective ().getArgument ("suffix" ).getValue ();
118+ return element ;
119+ }
98120 }
99121
100122 public static class Query {
@@ -128,6 +150,57 @@ public static String name() {
128150 }
129151 }
130152
153+ public static class Query4 {
154+ @ GraphQLField
155+ public static String nameWithArgument (@ GraphQLDirectives ({@ Directive (name = "suffix" ,
156+ wiringClass = SuffixWiring .class , argumentsValues = {"coolSuffixForArg" })})
157+ @ GraphQLName ("extensionArg" ) String extensionArg ) {
158+ return "yarin" + extensionArg ;
159+ }
160+ }
161+
162+ public static class InputObject {
163+ @ GraphQLField
164+ @ GraphQLDirectives ({@ Directive (name = "suffix" , wiringClass = SuffixWiring .class , argumentsValues = {"coolSuffix" })})
165+ private String a ;
166+
167+ @ GraphQLField
168+ private int b ;
169+
170+ public InputObject (@ GraphQLName ("a" ) String a ,@ GraphQLName ("b" ) int b ) {
171+ this .a = a ;
172+ this .b = b ;
173+ }
174+ }
175+
176+ public static class Query5 {
177+ @ GraphQLField
178+ public static String nameWithInputObject (@ GraphQLName ("inputObject" ) InputObject input ) {
179+ return "yarin" ;
180+ }
181+ }
182+
183+ @ Test
184+ public void queryNameWithInputObject_directivesProvidedToRegistry_wiringOfInputObjectIsActivated () {
185+ GraphQLDirective suffixDirective = GraphQLDirective .newDirective ().name ("suffix" ).argument (builder -> builder .name ("suffix" ).type (GraphQLString ))
186+ .validLocations (Introspection .DirectiveLocation .INPUT_FIELD_DEFINITION , Introspection .DirectiveLocation .FIELD_DEFINITION ).build ();
187+ GraphQLObjectType object = GraphQLAnnotations .object (Query5 .class , suffixDirective );
188+ GraphQLSchema schema = newSchema ().query (object ).build ();
189+ GraphQLFieldDefinition nameWithInputObject = schema .getQueryType ().getFieldDefinition ("nameWithInputObject" );
190+ GraphQLInputObjectField field = ((GraphQLInputObjectType ) nameWithInputObject .getArgument ("inputObject" ).getType ()).getField ("acoolSuffix" );
191+ assertNotNull (field );
192+ }
193+
194+ @ Test
195+ public void queryNameWithArgument_directivesProvidedToRegistry_wiringOfArgumentIsActivated () {
196+ GraphQLDirective suffixDirective = GraphQLDirective .newDirective ().name ("suffix" ).argument (builder -> builder .name ("suffix" ).type (GraphQLString ))
197+ .validLocations (Introspection .DirectiveLocation .FIELD_DEFINITION , Introspection .DirectiveLocation .ARGUMENT_DEFINITION ).build ();
198+ GraphQLObjectType object = GraphQLAnnotations .object (Query4 .class , suffixDirective );
199+ GraphQLSchema schema = newSchema ().query (object ).build ();
200+
201+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute ("query { nameWithArgument(extensionArgcoolSuffixForArg: \" ext\" ) }" );
202+ assertTrue (result .getErrors ().isEmpty ());
203+ }
131204
132205 @ Test (expectedExceptions = GraphQLAnnotationsException .class )
133206 public void queryName_noDirectivesProvidedToRegistry_exceptionIsThrown () throws Exception {
@@ -139,7 +212,7 @@ public void queryName_noDirectivesProvidedToRegistry_exceptionIsThrown() throws
139212
140213 @ GraphQLName ("upperCase" )
141214 @ DirectiveLocations (Introspection .DirectiveLocation .FIELD_DEFINITION )
142- public static class UpperCase {
215+ public static class UpperCase {
143216 boolean isActive ;
144217 }
145218
@@ -193,7 +266,7 @@ public void queryName_chainedDirectives_wiringIsActivatedInCorrectOrder() throws
193266 GraphQLDirective upperCase = newDirective ().name ("upperCase" ).argument (builder -> builder .name ("isActive" ).type (GraphQLBoolean ).defaultValue (true ))
194267 .validLocations (Introspection .DirectiveLocation .FIELD_DEFINITION ).build ();
195268 GraphQLDirective suffixDirective = GraphQLDirective .newDirective ().name ("suffix" ).argument (builder -> builder .name ("suffix" ).type (GraphQLString ))
196- .validLocations (Introspection .DirectiveLocation .FIELD_DEFINITION ).build ();
269+ .validLocations (Introspection .DirectiveLocation .FIELD_DEFINITION , Introspection . DirectiveLocation . ARGUMENT_DEFINITION ).build ();
197270 GraphQLObjectType object = GraphQLAnnotations .object (Query3 .class , upperCase , suffixDirective );
198271 GraphQLSchema schema = newSchema ().query (object ).build ();
199272
0 commit comments