55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
77 *
8- * http://www.apache.org/licenses/LICENSE-2.0
8+ * http://www.apache.org/licenses/LICENSE-2.0
99 *
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
1717import graphql .ExecutionResult ;
1818import graphql .GraphQL ;
1919import graphql .annotations .*;
20- import graphql .annotations .connection .ConnectionFetcher ;
21- import graphql .annotations .connection .GraphQLConnection ;
2220import graphql .annotations .util .CustomRelay ;
2321import graphql .relay .Relay ;
2422import graphql .schema .DataFetchingEnvironment ;
2826import org .testng .annotations .BeforeMethod ;
2927import org .testng .annotations .Test ;
3028
31- import java .util .Arrays ;
32- import java .util .List ;
33- import java .util .Map ;
29+ import java .util .*;
30+ import java .util .stream .Collectors ;
3431import java .util .stream .Stream ;
3532
3633import static graphql .annotations .util .RelayKit .EMPTY_CONNECTION ;
@@ -68,36 +65,67 @@ public static class DuplicateTest {
6865 }
6966
7067 public static class TestListField {
71- @ GraphQLField
72- @ GraphQLConnection
7368 public List <Obj > objs ;
7469
7570 public TestListField (List <Obj > objs ) {
7671 this .objs = objs ;
7772 }
78- }
7973
80- public static class TestListField2 {
8174 @ GraphQLField
8275 @ GraphQLConnection
76+ public PaginatedData <Obj > objs () {
77+ return new AbstarctPaginatedData <Obj >(false , true , objs ) {
78+ @ Override
79+ public String getCursor (Obj entity ) {
80+ return entity .id ;
81+ }
82+ };
83+ }
84+ }
85+
86+ public static class TestListField2 {
87+
8388 public List <Obj > objs ;
8489
8590 public TestListField2 (List <Obj > objs ) {
8691 this .objs = objs ;
8792 }
93+
94+ @ GraphQLField
95+ @ GraphQLConnection
96+ public PaginatedData <Obj > objs () {
97+ return new AbstarctPaginatedData <Obj >(false , true , objs ) {
98+ @ Override
99+ public String getCursor (Obj entity ) {
100+ return entity .id ;
101+ }
102+ };
103+ }
88104 }
89105
90- @ Test
106+ public static class TestConnectionOnField {
107+ @ GraphQLField
108+ @ GraphQLConnection
109+ public PaginatedData <Obj > objs ;
110+
111+ public TestConnectionOnField (List <Obj > objs ) {
112+ this .objs = new AbstarctPaginatedData <Obj >(false , true , objs ) {
113+ @ Override
114+ public String getCursor (Obj entity ) {
115+ return entity .id ;
116+ }
117+ };
118+ }
119+ }
120+
121+ @ Test (expectedExceptions = GraphQLConnectionException .class )
91122 public void fieldList () {
92- GraphQLObjectType object = GraphQLAnnotations .object (TestListField .class );
123+ GraphQLObjectType object = GraphQLAnnotations .object (TestConnectionOnField .class );
93124 GraphQLSchema schema = newSchema ().query (object ).build ();
94125
95126 GraphQL graphQL = GraphQL .newGraphQL (schema ).build ();
96127 ExecutionResult result = graphQL .execute ("{ objs(first: 1) { edges { cursor node { id, val } } } }" ,
97128 new TestListField (Arrays .asList (new Obj ("1" , "test" ), new Obj ("2" , "hello" ), new Obj ("3" , "world" ))));
98- assertTrue (result .getErrors ().isEmpty ());
99-
100- testResult ("objs" , result );
101129 }
102130
103131 public static class TestConnections {
@@ -109,33 +137,80 @@ public TestConnections(List<Obj> objs) {
109137
110138 @ GraphQLField
111139 @ GraphQLConnection
112- public List <Obj > getObjs () {
113- return this .objs ;
140+ public PaginatedData <Obj > getObjs (DataFetchingEnvironment environment ) {
141+ Integer first = environment .getArgument ("first" );
142+ List <Obj > actualobjs = new ArrayList <>(objs );
143+
144+ if (first != null && first < objs .size ()) {
145+ actualobjs = actualobjs .subList (0 , first );
146+ }
147+ return new AbstarctPaginatedData <Obj >(false , true , actualobjs ) {
148+ @ Override
149+ public String getCursor (Obj entity ) {
150+ return entity .id ;
151+ }
152+ };
114153 }
115154
116155 @ GraphQLField
117156 @ GraphQLConnection (name = "objStream" )
118- public Stream <Obj > getObjStream () {
119- Obj [] a = new Obj [objs .size ()];
120- return Stream .of (objs .toArray (a ));
157+ public PaginatedData <Obj > getObjStream (DataFetchingEnvironment environment ) {
158+ Integer first = environment .getArgument ("first" );
159+ List <Obj > actualobjs = new ArrayList <>(objs );
160+
161+ if (first != null && first < objs .size ()) {
162+ actualobjs = actualobjs .subList (0 , first );
163+ }
164+
165+ Obj [] a = new Obj [actualobjs .size ()];
166+ Iterable <Obj > data = Stream .of (actualobjs .toArray (a ))::iterator ;
167+ return new AbstarctPaginatedData <Obj >(false , true , data ) {
168+ @ Override
169+ public String getCursor (Obj entity ) {
170+ return entity .id ;
171+ }
172+ };
121173 }
122174
123175 @ GraphQLField
124176 @ GraphQLConnection (name = "objStreamWithParam" )
125- public Stream <Obj > getObjStreamWithParam (@ GraphQLName ("filter" ) String filter ) {
126- return this .objs .stream ().filter ( obj -> obj .val .startsWith (filter ));
177+ public PaginatedData <Obj > getObjStreamWithParam (DataFetchingEnvironment environment , @ GraphQLName ("filter" ) String filter ) {
178+ Integer first = environment .getArgument ("first" );
179+ List <Obj > actualobjs = new ArrayList <>(objs );
180+ List <Obj > filteredObjs = actualobjs .stream ().filter (obj -> obj .val .startsWith (filter )).collect (Collectors .toList ());
181+ if (first != null && first < filteredObjs .size ()) {
182+ filteredObjs = filteredObjs .subList (0 , first );
183+ }
184+ Iterable <Obj > objIterable = filteredObjs ::iterator ;
185+ return new AbstarctPaginatedData <Obj >(false , true , objIterable ) {
186+ @ Override
187+ public String getCursor (Obj entity ) {
188+ return entity .id ;
189+ }
190+ };
127191 }
128192
129193 @ GraphQLField
130194 @ GraphQLConnection (name = "nonNullObjs" )
131195 @ GraphQLNonNull
132- public List <Obj > getNonNullObjs () {
133- return this .objs ;
196+ public PaginatedData <Obj > getNonNullObjs (DataFetchingEnvironment environment ) {
197+ Integer first = environment .getArgument ("first" );
198+ List <Obj > actualobjs = new ArrayList <>(objs );
199+
200+ if (first != null && first < objs .size ()) {
201+ actualobjs = actualobjs .subList (0 , first );
202+ }
203+ return new AbstarctPaginatedData <Obj >(false , true , actualobjs ) {
204+ @ Override
205+ public String getCursor (Obj entity ) {
206+ return entity .id ;
207+ }
208+ };
134209 }
135210
136211 @ GraphQLField
137212 @ GraphQLConnection (name = "nullObj" )
138- public List <Obj > getNullObj () {
213+ public PaginatedData <Obj > getNullObj () {
139214 return null ;
140215 }
141216
@@ -167,7 +242,7 @@ public void customRelayMethodList() {
167242 graphql .schema .GraphQLObjectType f = (GraphQLObjectType ) schema .getType ("ObjConnection" );
168243 assertTrue (f .getFieldDefinitions ().size () == 4 );
169244 assertTrue (f .getFieldDefinition ("nodes" ).getType () instanceof GraphQLList );
170- assertEquals (((GraphQLList )f .getFieldDefinition ("nodes" ).getType ()).getWrappedType ().getName (), "Obj" );
245+ assertEquals (((GraphQLList ) f .getFieldDefinition ("nodes" ).getType ()).getWrappedType ().getName (), "Obj" );
171246
172247 GraphQLObjectType pageInfo = (GraphQLObjectType ) schema .getType ("PageInfo" );
173248 assertTrue (pageInfo .getFieldDefinition ("additionalInfo" ) != null );
@@ -271,8 +346,13 @@ public TestCustomConnection(List<Obj> objs) {
271346
272347 @ GraphQLField
273348 @ GraphQLConnection (connection = CustomConnection .class )
274- public List <Obj > getObjs () {
275- return this .objs ;
349+ public PaginatedData <Obj > getObjs () {
350+ return new AbstarctPaginatedData <Obj >(true , false , objs ) {
351+ @ Override
352+ public String getCursor (Obj entity ) {
353+ return entity .id ;
354+ }
355+ };
276356 }
277357 }
278358
@@ -298,7 +378,7 @@ public void duplicateConnection() {
298378 GraphQLObjectType object = GraphQLAnnotations .object (DuplicateTest .class );
299379 GraphQLSchema schema = newSchema ().query (object ).build ();
300380 } catch (GraphQLAnnotationsException e ) {
301- fail ("Schema cannot be created" ,e );
381+ fail ("Schema cannot be created" , e );
302382 }
303383 }
304384
0 commit comments