Skip to content

Commit c00e910

Browse files
committed
Fixed IndexOutOfBound issue when we have Empty list in a Paginated Data
1 parent 86924d2 commit c00e910

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/main/java/graphql/annotations/connection/PaginatedDataConnectionFetcher.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public Connection<T> get(DataFetchingEnvironment environment) {
5252
}
5353

5454
private PageInfo getPageInfo(List<Edge<T>> edges, PaginatedData<T> paginatedData) {
55+
if (edges.isEmpty()) {
56+
return new DefaultPageInfo(null,null,false,false);
57+
}
5558
ConnectionCursor firstCursor = edges.get(0).getCursor();
5659
ConnectionCursor lastCursor = edges.get(edges.size() - 1).getCursor();
5760
return new DefaultPageInfo(

src/test/java/graphql/annotations/connection/GraphQLConnectionTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import static graphql.annotations.processor.util.RelayKit.EMPTY_CONNECTION;
3838
import static graphql.schema.GraphQLSchema.newSchema;
39+
import static java.util.Collections.emptyList;
3940
import static org.testng.Assert.assertEquals;
4041
import static org.testng.Assert.assertTrue;
4142
import static org.testng.Assert.fail;
@@ -308,6 +309,22 @@ public void methodNull() {
308309
assertEquals(data.get("nullObj").get("edges").size(), 0);
309310
}
310311

312+
@Test
313+
public void emptyListData() {
314+
GraphQLObjectType object = GraphQLAnnotations.object(TestConnections.class);
315+
GraphQLSchema schema = newSchema().query(object).build();
316+
317+
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
318+
ExecutionResult result = graphQL.execute("{ objStreamWithParam(first: 1, filter:\"hel\") { edges { cursor node { id, val } } } }",
319+
new TestConnections(emptyList()));
320+
assertTrue(result.getErrors().isEmpty());
321+
322+
Map<String, Map<String, List<Map<String, Map<String, Object>>>>> data = result.getData();
323+
List<Map<String, Map<String, Object>>> edges = data.get("objStreamWithParam").get("edges");
324+
325+
assertEquals(edges.size(), 0);
326+
}
327+
311328
@Test
312329
public void methodListWithParam() {
313330
GraphQLObjectType object = GraphQLAnnotations.object(TestConnections.class);

0 commit comments

Comments
 (0)