1414 */
1515package graphql .annotations ;
1616
17+ import graphql .ExceptionWhileDataFetching ;
18+ import graphql .ExecutionResult ;
19+ import graphql .GraphQL ;
20+ import graphql .annotations .annotationTypes .GraphQLDataFetcher ;
21+ import graphql .annotations .annotationTypes .GraphQLField ;
22+ import graphql .annotations .annotationTypes .GraphQLInvokeDetached ;
23+ import graphql .annotations .annotationTypes .GraphQLType ;
1724import graphql .annotations .dataFetchers .MethodDataFetcher ;
1825import graphql .annotations .processor .GraphQLAnnotations ;
19- import graphql .schema .DataFetchingEnvironmentImpl ;
26+ import graphql .schema .* ;
2027import org .testng .annotations .BeforeMethod ;
2128import org .testng .annotations .Test ;
2229
2330import java .util .ArrayList ;
2431import java .util .HashMap ;
32+ import java .util .Map ;
2533
34+ import static graphql .schema .GraphQLSchema .newSchema ;
35+ import static org .testng .Assert .*;
36+
37+ @ SuppressWarnings ("unchecked" )
2638public class MethodDataFetcherTest {
2739
2840 @ BeforeMethod
@@ -41,10 +53,100 @@ public String method() throws TestException {
4153 @ Test (expectedExceptions = RuntimeException .class )
4254 public void exceptionRethrowing () {
4355 try {
44- MethodDataFetcher methodDataFetcher = new MethodDataFetcher (getClass ().getMethod ("method" ),null ,null );
45- methodDataFetcher .get (new DataFetchingEnvironmentImpl (this , new HashMap <String , Object >(), null , null , null , new ArrayList <>(), null , null , null , null , null , null , null ));
56+ MethodDataFetcher methodDataFetcher = new MethodDataFetcher (getClass ().getMethod ("method" ), null , null );
57+ methodDataFetcher .get (new DataFetchingEnvironmentImpl (this , new HashMap <>(), null , null , null , new ArrayList <>(), null , null , null , null , null , null , null ));
4658 } catch (NoSuchMethodException e ) {
4759 e .printStackTrace ();
4860 }
4961 }
62+
63+
64+ @ GraphQLType
65+ public static class ApiType {
66+ @ GraphQLField
67+ public int a () {
68+ return 1 ;
69+ }
70+
71+ @ GraphQLField
72+ @ GraphQLInvokeDetached
73+ public int b () {
74+ return 2 ;
75+ }
76+
77+ @ GraphQLField
78+ public int c () {
79+ return 4 ;
80+ }
81+ }
82+
83+ public static class InternalType {
84+ public int a = 123 ;
85+ public int b ;
86+ }
87+
88+ @ GraphQLType
89+ public static class Query {
90+ @ GraphQLField
91+ @ GraphQLDataFetcher (MyFetcher .class )
92+ public ApiType field ;
93+
94+ @ GraphQLField
95+ @ GraphQLDataFetcher (MyApiFetcher .class )
96+ public ApiType apiField ;
97+ }
98+
99+ public static class MyFetcher implements DataFetcher <InternalType > {
100+ public InternalType get (DataFetchingEnvironment environment ) {
101+ return new InternalType ();
102+ }
103+ }
104+
105+ public static class MyApiFetcher implements DataFetcher <ApiType > {
106+ public ApiType get (DataFetchingEnvironment environment ) {
107+ return new ApiType ();
108+ }
109+ }
110+
111+
112+ @ Test
113+ public void queryingOneFieldNotAnnotatedWithGraphQLInvokeDetached_valueIsDeterminedByEntity () {
114+ GraphQLObjectType object = GraphQLAnnotations .object (Query .class );
115+ GraphQLSchema schema = newSchema ().query (object ).build ();
116+
117+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute ("query { field { a } }" );
118+ assertTrue (result .getErrors ().isEmpty ());
119+ assertEquals (((Map <String , Map <String , Integer >>) result .getData ()).get ("field" ).get ("a" ).toString (), "123" );
120+ }
121+
122+ @ Test
123+ public void queryingOneFieldAnnotatedWithGraphQLInvokeDetached_valueIsDeterminedByApiEntity () {
124+ GraphQLObjectType object = GraphQLAnnotations .object (Query .class );
125+ GraphQLSchema schema = newSchema ().query (object ).build ();
126+
127+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute ("query { field { b } }" );
128+ assertTrue (result .getErrors ().isEmpty ());
129+ assertEquals (((Map <String , Map <String , Integer >>) result .getData ()).get ("field" ).get ("b" ).toString (), "2" );
130+ }
131+
132+ @ Test
133+ public void queryingFieldsFromApiEntityFetcher_valueIsDeterminedByApiEntity () {
134+ GraphQLObjectType object = GraphQLAnnotations .object (Query .class );
135+ GraphQLSchema schema = newSchema ().query (object ).build ();
136+
137+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute ("query { apiField { a b } }" );
138+ assertTrue (result .getErrors ().isEmpty ());
139+ assertEquals (((Map <String , Map <String , Integer >>) result .getData ()).get ("apiField" ).get ("a" ).toString (), "1" );
140+ assertEquals (((Map <String , Map <String , Integer >>) result .getData ()).get ("apiField" ).get ("b" ).toString (), "2" );
141+ }
142+
143+ @ Test
144+ public void queryingFieldsFromNoApiEntityFetcher_noMatchingFieldInEntity_throwException (){
145+ GraphQLObjectType object = GraphQLAnnotations .object (Query .class );
146+ GraphQLSchema schema = newSchema ().query (object ).build ();
147+
148+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute ("query { field { c } }" );
149+ assertFalse (result .getErrors ().isEmpty ());
150+ assertTrue (((ExceptionWhileDataFetching )result .getErrors ().get (0 )).getException ().getCause () instanceof NoSuchFieldException );
151+ }
50152}
0 commit comments