2525import org .testng .annotations .BeforeMethod ;
2626import org .testng .annotations .Test ;
2727
28- import javax .xml .crypto .Data ;
2928import java .util .ArrayList ;
3029import java .util .HashMap ;
3130import java .util .Map ;
@@ -41,6 +40,24 @@ public void init() {
4140 GraphQLAnnotations .getInstance ().getTypeRegistry ().clear ();
4241 }
4342
43+ public static class StaticApi {
44+ @ GraphQLField
45+ public static String name () {
46+ return "osher" ;
47+ }
48+ }
49+
50+ @ Test
51+ public void query_staticMethod_valueIsDeterminedByMethod (){
52+ GraphQLObjectType object = GraphQLAnnotations .object (StaticApi .class );
53+ GraphQLSchema schema = newSchema ().query (object ).build ();
54+
55+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute (builder -> builder .query ("query { name }" ).root (new StaticApi ()));
56+ assertTrue (result .getErrors ().isEmpty ());
57+ assertEquals (((Map <String , String >) result .getData ()).get ("name" ).toString (), "osher" );
58+ }
59+
60+
4461 /**
4562 * CASE 1 : Only Api class, value determined by field
4663 */
@@ -95,10 +112,12 @@ public void query_onlyApiClass_valueIsDeterminedByMethod() throws Exception {
95112 }
96113
97114 /**
98- * Case 3: Api and a DB class, value is determined by the db field
115+ * Case 3: Api and a DB class with polymorphism, value is determined by the db field
116+ * name of api method <-> name of db field
99117 */
100118 public static class Api3 {
101119 @ GraphQLField
120+ @ GraphQLName ("nameX" )
102121 public String name () {
103122 return "dani" ;
104123 }
@@ -131,17 +150,19 @@ public void query_apiAndDbClass_valueIsDeterminedByDBField() throws Exception {
131150 GraphQLObjectType object = GraphQLAnnotations .object (Query3 .class );
132151 GraphQLSchema schema = newSchema ().query (object ).build ();
133152
134- ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute (builder -> builder .query ("query { queryField { name } }" ).root (new Query3 ()));
153+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute (builder -> builder .query ("query { queryField { nameX } }" ).root (new Query3 ()));
135154 assertTrue (result .getErrors ().isEmpty ());
136- assertEquals (((Map <String , Map <String , String >>) result .getData ()).get ("queryField" ).get ("name " ).toString (), "osher" );
155+ assertEquals (((Map <String , Map <String , String >>) result .getData ()).get ("queryField" ).get ("nameX " ).toString (), "osher" );
137156 }
138157
139158 /**
140159 * Case 4: Api and DB classes, value is determined by db method
160+ * api method name <-> (`get`) + db method name
141161 */
142162
143163 public static class Api4 {
144164 @ GraphQLField
165+ @ GraphQLName ("nameX" )
145166 public String name () {
146167 return null ;
147168 }
@@ -173,15 +194,116 @@ public static class Query4 {
173194 }
174195
175196 @ Test
176- public void query_apiAndDbClass_valueIsDeterminedByDBMethod () throws Exception {
197+ public void query_apiAndDbClass_valueIsDeterminedByGetPrefixDBMethod () throws Exception {
177198 GraphQLObjectType object = GraphQLAnnotations .object (Query4 .class );
178199 GraphQLSchema schema = newSchema ().query (object ).build ();
179200
180- ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute (builder -> builder .query ("query { queryField { name } }" ).root (new Query4 ()));
201+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute (builder -> builder .query ("query { queryField { nameX } }" ).root (new Query4 ()));
202+ assertTrue (result .getErrors ().isEmpty ());
203+ assertEquals (((Map <String , Map <String , String >>) result .getData ()).get ("queryField" ).get ("nameX" ).toString (), "guy/yarin" );
204+ }
205+
206+ /**
207+ * Case: Api and DB classes, value is determined by db method
208+ * api method name <-> (`is`) + db method name
209+ */
210+
211+ public static class Api6 {
212+ @ GraphQLField
213+ @ GraphQLName ("nameX" )
214+ public String name () {
215+ return null ;
216+ }
217+ }
218+
219+ public static class SuperDB6 {
220+ private String name = "guy" ;
221+
222+ public String isName () {
223+ return name + "/yarin" ;
224+ }
225+ }
226+
227+ public static class DB6 extends SuperDB6 {
228+ }
229+
230+ public static class Api6Resolver implements DataFetcher <DB6 > {
231+
232+ @ Override
233+ public DB6 get (DataFetchingEnvironment environment ) {
234+ return new DB6 ();
235+ }
236+ }
237+
238+ public static class Query6 {
239+ @ GraphQLField
240+ @ GraphQLDataFetcher (Api6Resolver .class )
241+ public Api6 queryField ;
242+ }
243+
244+ @ Test
245+ public void query_apiAndDbClass_valueIsDeterminedByIsPrefixDBMethod () throws Exception {
246+ GraphQLObjectType object = GraphQLAnnotations .object (Query6 .class );
247+ GraphQLSchema schema = newSchema ().query (object ).build ();
248+
249+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute (builder -> builder .query ("query { queryField { nameX } }" ).root (new Query6 ()));
181250 assertTrue (result .getErrors ().isEmpty ());
182- assertEquals (((Map <String , Map <String , String >>) result .getData ()).get ("queryField" ).get ("name " ).toString (), "guy/yarin" );
251+ assertEquals (((Map <String , Map <String , String >>) result .getData ()).get ("queryField" ).get ("nameX " ).toString (), "guy/yarin" );
183252 }
184253
254+ /**
255+ * Case: Api and DB classes, value is determined by db method
256+ * api method name <-> db method name
257+ */
258+
259+ public static class Api7 {
260+ @ GraphQLField
261+ @ GraphQLName ("nameX" )
262+ public String name () {
263+ return null ;
264+ }
265+ }
266+
267+ public static class SuperDB7 {
268+ private String name = "guy" ;
269+
270+ public String name () {
271+ return name + "/yarin" ;
272+ }
273+
274+ public String isName () {
275+ return "blabla" ;
276+ }
277+ }
278+
279+ public static class DB7 extends SuperDB7 {
280+ }
281+
282+ public static class Api7Resolver implements DataFetcher <DB7 > {
283+
284+ @ Override
285+ public DB7 get (DataFetchingEnvironment environment ) {
286+ return new DB7 ();
287+ }
288+ }
289+
290+ public static class Query7 {
291+ @ GraphQLField
292+ @ GraphQLDataFetcher (Api7Resolver .class )
293+ public Api7 queryField ;
294+ }
295+
296+ @ Test
297+ public void query_apiAndDbClass_valueIsDeterminedByDBMethod () throws Exception {
298+ GraphQLObjectType object = GraphQLAnnotations .object (Query7 .class );
299+ GraphQLSchema schema = newSchema ().query (object ).build ();
300+
301+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute (builder -> builder .query ("query { queryField { nameX } }" ).root (new Query7 ()));
302+ assertTrue (result .getErrors ().isEmpty ());
303+ assertEquals (((Map <String , Map <String , String >>) result .getData ()).get ("queryField" ).get ("nameX" ).toString (), "guy/yarin" );
304+ }
305+
306+
185307 /**
186308 * Case 5: Invoke Detached on method, both api and db classes, value is determined by the api method
187309 */
@@ -215,8 +337,6 @@ public static class Query5 {
215337 public Api5 queryField ;
216338 }
217339
218- /////////////////////////////////////////
219-
220340 @ Test
221341 public void query_apiAndDbClassAndApiIsInvokeDetached_valueIsDeterminedByApiMethod () throws Exception {
222342 GraphQLObjectType object = GraphQLAnnotations .object (Query5 .class );
@@ -227,6 +347,9 @@ public void query_apiAndDbClassAndApiIsInvokeDetached_valueIsDeterminedByApiMeth
227347 assertEquals (((Map <String , Map <String , String >>) result .getData ()).get ("queryField" ).get ("name" ).toString (), "yarin/guy/osher" );
228348 }
229349
350+ /////////////////////////////////////////
351+ /////////////////////////////////////////
352+ /////////////////////////////////////////
230353
231354 public class TestException extends Exception {
232355 }
@@ -276,8 +399,6 @@ public int c() {
276399 public CanonizedTypeApi getCanonizedType () {
277400 return null ;
278401 }
279-
280-
281402 }
282403
283404 public static class CanonizedFetcher implements DataFetcher <CanonizedType > {
0 commit comments