Skip to content

Commit dc57aa2

Browse files
RusticFlarebbakerman
authored andcommitted
Error if 'first' or 'last' is negative for a Relay connection argument (#840)
1 parent 05b3694 commit dc57aa2

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package graphql.relay;
2+
3+
import graphql.ErrorType;
4+
import graphql.GraphQLError;
5+
import graphql.GraphqlErrorHelper;
6+
import graphql.language.SourceLocation;
7+
8+
import java.util.List;
9+
10+
import static graphql.ErrorType.DataFetchingException;
11+
12+
public class InvalidPageSizeException extends RuntimeException implements GraphQLError {
13+
14+
InvalidPageSizeException(String message) {
15+
this(message, null);
16+
}
17+
18+
InvalidPageSizeException(String message, Throwable cause) {
19+
super(message, cause);
20+
}
21+
22+
@Override
23+
public List<SourceLocation> getLocations() {
24+
return null;
25+
}
26+
27+
@Override
28+
public ErrorType getErrorType() {
29+
return DataFetchingException;
30+
}
31+
32+
@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
33+
@Override
34+
public boolean equals(Object o) {
35+
return GraphqlErrorHelper.equals(this, o);
36+
}
37+
38+
@Override
39+
public int hashCode() {
40+
return GraphqlErrorHelper.hashCode(this);
41+
}
42+
43+
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,22 @@ public Connection<T> get(DataFetchingEnvironment environment) {
5858
return emptyConnection();
5959
}
6060

61-
Integer first = environment.<Integer>getArgument("first");
62-
Integer last = environment.<Integer>getArgument("last");
61+
Integer first = environment.getArgument("first");
62+
Integer last = environment.getArgument("last");
6363

6464
ConnectionCursor firstPresliceCursor = edges.get(0).getCursor();
6565
ConnectionCursor lastPresliceCursor = edges.get(edges.size() - 1).getCursor();
6666

6767
if (first != null) {
68+
if (first < 0) {
69+
throw new InvalidPageSizeException(format("The page size must not be negative: 'first'=%s", first));
70+
}
6871
edges = edges.subList(0, first <= edges.size() ? first : edges.size());
6972
}
7073
if (last != null) {
74+
if (last < 0) {
75+
throw new InvalidPageSizeException(format("The page size must not be negative: 'last'=%s", last));
76+
}
7177
edges = edges.subList(last > edges.size() ? 0 : edges.size() - last, edges.size());
7278
}
7379

0 commit comments

Comments
 (0)