1414 */
1515package graphql .annotations ;
1616
17+ import graphql .ExecutionResult ;
18+ import graphql .GraphQL ;
19+ import graphql .annotations .annotationTypes .GraphQLDataFetcher ;
20+ import graphql .annotations .annotationTypes .GraphQLField ;
21+ import graphql .annotations .annotationTypes .GraphQLInvokeDetached ;
22+ import graphql .annotations .annotationTypes .GraphQLType ;
1723import graphql .annotations .dataFetchers .MethodDataFetcher ;
1824import graphql .annotations .processor .GraphQLAnnotations ;
19- import graphql .schema .DataFetchingEnvironmentImpl ;
25+ import graphql .schema .* ;
2026import org .testng .annotations .BeforeMethod ;
2127import org .testng .annotations .Test ;
2228
2329import java .util .ArrayList ;
2430import java .util .HashMap ;
31+ import java .util .Map ;
2532
33+ import static graphql .schema .GraphQLSchema .newSchema ;
34+ import static org .testng .Assert .assertEquals ;
35+ import static org .testng .Assert .assertTrue ;
36+
37+ @ SuppressWarnings ("unchecked" )
2638public class MethodDataFetcherTest {
2739
2840 @ BeforeMethod
@@ -41,10 +53,84 @@ 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+
78+ public static class InternalType {
79+ public int a = 123 ;
80+ public int b ;
81+ }
82+
83+ @ GraphQLType
84+ public static class Query {
85+ @ GraphQLField
86+ @ GraphQLDataFetcher (MyFetcher .class )
87+ public ApiType field ;
88+
89+ @ GraphQLField
90+ @ GraphQLDataFetcher (MyApiFetcher .class )
91+ public ApiType apiField ;
92+ }
93+
94+ public static class MyFetcher implements DataFetcher <InternalType > {
95+ public InternalType get (DataFetchingEnvironment environment ) {
96+ return new InternalType ();
97+ }
98+ }
99+
100+ public static class MyApiFetcher implements DataFetcher <ApiType > {
101+ public ApiType get (DataFetchingEnvironment environment ) {
102+ return new ApiType ();
103+ }
104+ }
105+
106+ @ Test
107+ public void queryingOneFieldNotAnnotatedWithGraphQLInvokeDetached_valueIsDeterminedByEntity () {
108+ GraphQLObjectType object = GraphQLAnnotations .object (Query .class );
109+ GraphQLSchema schema = newSchema ().query (object ).build ();
110+
111+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute ("query { field { a } }" );
112+ assertTrue (result .getErrors ().isEmpty ());
113+ assertEquals (((Map <String , Map <String , Integer >>) result .getData ()).get ("field" ).get ("a" ).toString (), "123" );
114+ }
115+
116+ @ Test
117+ public void queryingOneFieldAnnotatedWithGraphQLInvokeDetached_valueIsDeterminedByApiEntity () {
118+ GraphQLObjectType object = GraphQLAnnotations .object (Query .class );
119+ GraphQLSchema schema = newSchema ().query (object ).build ();
120+
121+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute ("query { field { b } }" );
122+ assertTrue (result .getErrors ().isEmpty ());
123+ assertEquals (((Map <String , Map <String , Integer >>) result .getData ()).get ("field" ).get ("b" ).toString (), "2" );
124+ }
125+
126+ @ Test
127+ public void queryingFieldsFromApiEntityFetcher_valueIsDeterminedByApiEntity () {
128+ GraphQLObjectType object = GraphQLAnnotations .object (Query .class );
129+ GraphQLSchema schema = newSchema ().query (object ).build ();
130+
131+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute ("query { apiField { a b } }" );
132+ assertTrue (result .getErrors ().isEmpty ());
133+ assertEquals (((Map <String , Map <String , Integer >>) result .getData ()).get ("apiField" ).get ("a" ).toString (), "1" );
134+ assertEquals (((Map <String , Map <String , Integer >>) result .getData ()).get ("apiField" ).get ("b" ).toString (), "2" );
135+ }
50136}
0 commit comments