1+ package graphql .annotations ;
2+
3+ import graphql .ExecutionResult ;
4+ import graphql .GraphQL ;
5+ import graphql .annotations .annotationTypes .GraphQLDataFetcher ;
6+ import graphql .annotations .annotationTypes .GraphQLField ;
7+ import graphql .annotations .processor .GraphQLAnnotations ;
8+ import graphql .schema .DataFetcher ;
9+ import graphql .schema .DataFetchingEnvironment ;
10+ import graphql .schema .GraphQLObjectType ;
11+ import graphql .schema .GraphQLSchema ;
12+ import org .testng .annotations .BeforeMethod ;
13+ import org .testng .annotations .Test ;
14+
15+ import java .util .ArrayList ;
16+ import java .util .Collections ;
17+ import java .util .LinkedHashMap ;
18+ import java .util .List ;
19+
20+ import static graphql .schema .GraphQLSchema .newSchema ;
21+ import static org .testng .Assert .assertEquals ;
22+ import static org .testng .Assert .assertTrue ;
23+
24+ public class GraphQLIterableTest {
25+ @ BeforeMethod
26+ public void init () {
27+ GraphQLAnnotations .getInstance ().getTypeRegistry ().clear ();
28+ }
29+
30+ public static class TestMappedObject {
31+ @ GraphQLField
32+ public String name ;
33+
34+ @ GraphQLField
35+ public String foo ;
36+ }
37+
38+ public static class TestObjectDB {
39+ private String foo ;
40+
41+ private String name ;
42+
43+ public String getName () {
44+ return name ;
45+ }
46+
47+ public String getFoo () {
48+ return foo ;
49+ }
50+
51+ TestObjectDB (String name , String foo ) {
52+ this .name = name ;
53+ this .foo = foo ;
54+ }
55+ }
56+
57+ public static class IterableTestQuery {
58+ @ GraphQLField
59+ @ GraphQLDataFetcher (ArrayFetcher .class )
60+ public TestMappedObject [] array ;
61+
62+ @ GraphQLField
63+ @ GraphQLDataFetcher (ListFetcher .class )
64+ public List <TestMappedObject > list ;
65+ }
66+
67+ public static class ArrayFetcher implements DataFetcher <TestObjectDB []> {
68+
69+ @ Override
70+ public TestObjectDB [] get (DataFetchingEnvironment environment ) {
71+ return new TestObjectDB []{new TestObjectDB ("hello" , "world" )};
72+ }
73+ }
74+
75+ public static class ListFetcher implements DataFetcher <List <TestObjectDB >> {
76+
77+ @ Override
78+ public List <TestObjectDB > get (DataFetchingEnvironment environment ) {
79+ return Collections .singletonList (new TestObjectDB ("test" , "test" ));
80+ }
81+ }
82+
83+ @ Test
84+ public void queryWithArray () {
85+ GraphQLObjectType object = GraphQLAnnotations .object (IterableTestQuery .class );
86+ GraphQLSchema schema = newSchema ().query (object ).build ();
87+
88+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute ("{array {name foo}}" );
89+ assertTrue (result .getErrors ().isEmpty ());
90+ assertEquals (((LinkedHashMap ) ((ArrayList ) (((LinkedHashMap ) result .getData ()).get ("array" ))).get (0 )).get ("name" ), "hello" );
91+ assertEquals (((LinkedHashMap ) ((ArrayList ) (((LinkedHashMap ) result .getData ()).get ("array" ))).get (0 )).get ("foo" ), "world" );
92+ }
93+
94+ @ Test
95+ public void queryWithList () {
96+ GraphQLObjectType object = GraphQLAnnotations .object (IterableTestQuery .class );
97+ GraphQLSchema schema = newSchema ().query (object ).build ();
98+
99+ ExecutionResult result = GraphQL .newGraphQL (schema ).build ().execute ("{list {name foo}}" );
100+ assertTrue (result .getErrors ().isEmpty ());
101+ assertEquals (((LinkedHashMap ) ((ArrayList ) (((LinkedHashMap ) result .getData ()).get ("list" ))).get (0 )).get ("name" ), "test" );
102+ assertEquals (((LinkedHashMap ) ((ArrayList ) (((LinkedHashMap ) result .getData ()).get ("list" ))).get (0 )).get ("foo" ), "test" );
103+ }
104+ }
0 commit comments