Skip to content

Commit b5bb623

Browse files
committed
2 parents 9f1efa1 + b638274 commit b5bb623

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/main/java/graphql/relay/SimpleListConnection.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
public class SimpleListConnection<T> implements DataFetcher<Connection<T>> {
1515

16-
private static final String DUMMY_CURSOR_PREFIX = "simple-cursor";
16+
static final String DUMMY_CURSOR_PREFIX = "simple-cursor";
1717
private final String prefix;
1818
private final List<T> data;
1919

@@ -48,6 +48,8 @@ public Connection<T> get(DataFetchingEnvironment environment) {
4848
int beforeOffset = getOffsetFromCursor(environment.getArgument("before"), edges.size());
4949
int end = Math.min(beforeOffset, edges.size());
5050

51+
if (begin > end) begin = end;
52+
5153
edges = edges.subList(begin, end);
5254
if (edges.size() == 0) {
5355
return emptyConnection();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package graphql.relay
2+
3+
import graphql.schema.DataFetchingEnvironment
4+
import graphql.schema.DataFetchingEnvironmentImpl
5+
import spock.lang.Specification
6+
7+
import java.nio.charset.StandardCharsets
8+
9+
class SimpleListConnectionTest extends Specification {
10+
def "invalid list indices handled"() {
11+
given:
12+
def testList = ["a", "b"]
13+
def listConnection = new SimpleListConnection(testList)
14+
def env = new DataFetchingEnvironmentImpl(null, ["after": createCursor(3)], null, null, null, null, null, null, null, null);
15+
16+
when:
17+
Object item = listConnection.get(env);
18+
19+
then:
20+
item instanceof graphql.relay.Connection
21+
}
22+
23+
24+
private String createCursor(int offset) {
25+
def string = SimpleListConnection.DUMMY_CURSOR_PREFIX + Integer.toString(offset)
26+
return Base64.getEncoder().encodeToString(string.getBytes(StandardCharsets.UTF_8))
27+
}
28+
}

0 commit comments

Comments
 (0)