Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/main/java/graphql/relay/InvalidPageSizeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package graphql.relay;

import graphql.ErrorType;
import graphql.GraphQLError;
import graphql.GraphqlErrorHelper;
import graphql.language.SourceLocation;

import java.util.List;

import static graphql.ErrorType.DataFetchingException;

public class InvalidPageSizeException extends RuntimeException implements GraphQLError {

InvalidPageSizeException(String message) {
this(message, null);
}

InvalidPageSizeException(String message, Throwable cause) {
super(message, cause);
}

@Override
public List<SourceLocation> getLocations() {
return null;
}

@Override
public ErrorType getErrorType() {
return DataFetchingException;
}

@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
@Override
public boolean equals(Object o) {
return GraphqlErrorHelper.equals(this, o);
}

@Override
public int hashCode() {
return GraphqlErrorHelper.hashCode(this);
}

}
10 changes: 8 additions & 2 deletions src/main/java/graphql/relay/SimpleListConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,22 @@ public Connection<T> get(DataFetchingEnvironment environment) {
return emptyConnection();
}

Integer first = environment.<Integer>getArgument("first");
Integer last = environment.<Integer>getArgument("last");
Integer first = environment.getArgument("first");
Integer last = environment.getArgument("last");

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

if (first != null) {
if (first < 0) {
throw new InvalidPageSizeException(format("The page size must not be negative: 'first'=%s", first));
}
edges = edges.subList(0, first <= edges.size() ? first : edges.size());
}
if (last != null) {
if (last < 0) {
throw new InvalidPageSizeException(format("The page size must not be negative: 'last'=%s", last));
}
edges = edges.subList(last > edges.size() ? 0 : edges.size() - last, edges.size());
}

Expand Down