11/**
22 * Copyright 2016 Yurii Rashkovskii
3- *
3+ * <p>
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
66 * 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>
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
1212 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -83,6 +83,13 @@ public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment)
8383 }
8484
8585 public static class SuffixWiring implements AnnotationsDirectiveWiring {
86+ @ Override
87+ public GraphQLInputObjectField onInputObjectField (AnnotationsWiringEnvironment environment ) {
88+ GraphQLInputObjectField field = (GraphQLInputObjectField ) environment .getElement ();
89+ String suffix = (String ) environment .getDirective ().getArgument ("suffix" ).getValue ();
90+ return field .transform (builder -> builder .name (field .getName ()+suffix ));
91+ }
92+
8693 @ Override
8794 public GraphQLFieldDefinition onField (AnnotationsWiringEnvironment environment ) {
8895 GraphQLFieldDefinition field = (GraphQLFieldDefinition ) environment .getElement ();
@@ -95,6 +102,20 @@ public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment)
95102 })));
96103 return field .transform (builder -> builder .dataFetcher (dataFetcher ));
97104 }
105+
106+ @ Override
107+ public GraphQLArgument onArgument (AnnotationsWiringEnvironment environment ) {
108+ GraphQLArgument element = (GraphQLArgument ) environment .getElement ();
109+ String suffix = (String ) environment .getDirective ().getArgument ("suffix" ).getValue ();
110+ return element .transform (builder -> builder .name (element .getName () + suffix ));
111+ }
112+
113+ @ Override
114+ public GraphQLInputObjectType onInputObjectType (AnnotationsWiringEnvironment environment ) {
115+ GraphQLInputObjectType element = (GraphQLInputObjectType ) environment .getElement ();
116+ String suffix = (String ) environment .getDirective ().getArgument ("suffix" ).getValue ();
117+ return element ;
118+ }
98119 }
99120
100121 public static class Query {
@@ -128,6 +149,58 @@ public static String name() {
128149 }
129150 }
130151
152+ public static class Query4 {
153+ @ GraphQLField
154+ public static String nameWithArgument (@ GraphQLDirectives ({@ Directive (name = "suffix" ,
155+ wiringClass = SuffixWiring .class , argumentsValues = {"coolSuffixForArg" })})
156+ @ GraphQLName ("extensionArg" ) String extensionArg ) {
157+ return "yarin" + extensionArg ;
158+ }
159+ }
160+
161+ public static class InputObject {
162+ @ GraphQLField
163+ @ GraphQLDirectives ({@ Directive (name = "suffix" , wiringClass = SuffixWiring .class , argumentsValues = {"coolSuffix" })})
164+ private String a ;
165+
166+ @ GraphQLField
167+ private int b ;
168+
169+ public InputObject (@ GraphQLName ("a" ) String a ,@ GraphQLName ("b" ) int b ) {
170+ this .a = a ;
171+ this .b = b ;
172+ }
173+ }
174+
175+ public static class Query5 {
176+ @ GraphQLField
177+ public static String nameWithInputObject (@ GraphQLName ("inputObject" ) InputObject input ) {
178+ return "yarin" ;
179+ }
180+ }
181+
182+ @ Test
183+ public void queryNameWithInputObject_directivesProvidedToRegistry_wiringOfInputObjectIsActivated () {
184+ GraphQLDirective suffixDirective = GraphQLDirective .newDirective ().name ("suffix" ).argument (builder -> builder .name ("suffix" ).type (GraphQLString ))
185+ .validLocations (Introspection .DirectiveLocation .INPUT_FIELD_DEFINITION , Introspection .DirectiveLocation .FIELD_DEFINITION ).build ();
186+ GraphQLObjectType object = GraphQLAnnotations .object (Query5 .class , suffixDirective );
187+ GraphQLSchema schema = newSchema ().query (object ).build ();
188+
189+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute ("query { nameWithInputObject" +
190+ "(inputObject: {acoolSuffix: \" magnificent\" , b: 5}) }" );
191+ assertTrue (result .getErrors ().isEmpty ());
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