2626import java .util .HashMap ;
2727
2828import static graphql .schema .GraphQLSchema .newSchema ;
29+ import static org .testng .Assert .assertFalse ;
2930import static org .testng .Assert .assertNotNull ;
3031import static org .testng .Assert .assertTrue ;
3132
@@ -48,15 +49,58 @@ public void shouldUsePreferredConstructor() {
4849 assertTrue (((HashMap <String , Boolean >) data .get ("sample" )).get ("isBad" ));
4950 }
5051
52+ @ Test
53+ public void shouldUseProvidedSoloArgumentForDataFetcherDeclaredInMethod () {
54+ // Given
55+ final GraphQLObjectType object = GraphQLAnnotations .object (TestMethodWithDataFetcherGraphQLQuery .class );
56+ final GraphQLSchema schema = newSchema ().query (object ).build ();
57+ final GraphQL graphql = GraphQL .newGraphQL (schema ).build ();
58+
59+ // When
60+ final ExecutionResult result = graphql .execute ("{great}" );
61+
62+ // Then
63+ final HashMap <String , Object > data = (HashMap ) result .getData ();
64+ assertNotNull (data );
65+ assertFalse ((Boolean )data .get ("great" ));
66+ }
67+
68+ @ Test
69+ public void shouldUseTargetAndArgumentsForDataFetcherDeclaredInMethod () {
70+ // Given
71+ final GraphQLObjectType object = GraphQLAnnotations .object (TestMethodWithDataFetcherGraphQLQuery .class );
72+ final GraphQLSchema schema = newSchema ().query (object ).build ();
73+ final GraphQL graphql = GraphQL .newGraphQL (schema ).build ();
74+
75+ // When
76+ final ExecutionResult result = graphql .execute ("{sample {bad}}" );
77+
78+ // Then
79+ final HashMap <String , Object > data = (HashMap ) result .getData ();
80+ assertNotNull (data );
81+ assertTrue (((HashMap <String ,Boolean >)data .get ("sample" )).get ("bad" ));
82+ }
83+
5184 @ GraphQLName ("Query" )
5285 public static class TestGraphQLQuery {
5386 @ GraphQLField
5487 @ GraphQLDataFetcher (SampleDataFetcher .class )
5588 public TestSample sample () { // Note that GraphQL uses TestSample to build the graph
56- return null ;
89+ return null ;
5790 }
5891 }
5992
93+ @ GraphQLName ("Query" )
94+ public static class TestMethodWithDataFetcherGraphQLQuery {
95+ @ GraphQLField
96+ @ GraphQLDataFetcher (value = SampleOneArgDataFetcher .class , args = "true" )
97+ public Boolean great () { return false ; }
98+
99+ @ GraphQLField
100+ @ GraphQLDataFetcher (SampleDataFetcher .class )
101+ public TestSampleMethod sample () { return null ; }
102+ }
103+
60104 public static class TestSample {
61105 @ GraphQLField
62106 @ GraphQLDataFetcher (value = PropertyDataFetcher .class , args = "isGreat" )
@@ -68,13 +112,38 @@ public static class TestSample {
68112
69113 }
70114
115+ public static class TestSampleMethod {
116+
117+ @ GraphQLField
118+ @ GraphQLDataFetcher (value = SampleMultiArgDataFetcher .class , firstArgIsTargetName = true , args = {"true" })
119+ public Boolean isBad () { return false ; } // Defaults to FieldDataFetcher
120+
121+ }
122+
71123 public static class SampleDataFetcher implements DataFetcher {
72124 @ Override
73125 public Object get (final DataFetchingEnvironment environment ) {
74126 return new Sample (); // Notice that it return a Sample, not a TestSample
75127 }
76128 }
77129
130+ public static class SampleOneArgDataFetcher implements DataFetcher {
131+ private boolean flip = false ;
132+
133+ public SampleOneArgDataFetcher (String flip ) {
134+ this .flip = Boolean .valueOf (flip );
135+ }
136+
137+ @ Override
138+ public Object get (DataFetchingEnvironment environment ) {
139+ if ( flip ) {
140+ return !flip ;
141+ } else {
142+ return flip ;
143+ }
144+ }
145+ }
146+
78147 public static class SampleMultiArgDataFetcher extends PropertyDataFetcher {
79148 private boolean flip = false ;
80149
@@ -87,7 +156,7 @@ public SampleMultiArgDataFetcher(String target, String flip) {
87156 public Object get (DataFetchingEnvironment environment ) {
88157 final Object result = super .get (environment );
89158 if (flip ) {
90- return !(Boolean ) result ;
159+ return !(Boolean )result ;
91160 } else {
92161 return result ;
93162 }
0 commit comments