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
4 changes: 2 additions & 2 deletions src/main/java/graphql/relay/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
/**
* represents a connection in relay.
*/
public interface Connection {
public interface Connection<T> {

List<Edge> getEdges();
List<Edge<T>> getEdges();

PageInfo getPageInfo();

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/graphql/relay/ConnectionCursor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package graphql.relay;

/**
* represents a connection cursor in relay.
* represents a {@link Connection connection} cursor in relay.
*/
public interface ConnectionCursor {

Expand Down
46 changes: 41 additions & 5 deletions src/main/java/graphql/relay/DefaultConnection.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
package graphql.relay;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DefaultConnection implements Connection {
public class DefaultConnection<T> implements Connection<T> {

private List<Edge> edges = new ArrayList<>();
private List<Edge<T>> edges = new ArrayList<>();

private PageInfo pageInfo;

/**
* @deprecated prefer {@link #DefaultConnection(List, PageInfo)}
*/
@Deprecated
public DefaultConnection() {
}

/**
* @param edges edges
* @param pageInfo page info
* @throws IllegalArgumentException if edges or page info is null. use {@link Collections#emptyList()} for empty edges.
*/
public DefaultConnection(List<Edge<T>> edges, PageInfo pageInfo) {
if (edges == null) {
throw new IllegalArgumentException("edges cannot be empty");
}
if (pageInfo == null) {
throw new IllegalArgumentException("page info cannot be null");
}
// TODO make defensive copy
this.edges = edges;
this.pageInfo = pageInfo;
}

@Override
public List<Edge> getEdges() {
return edges;
public List<Edge<T>> getEdges() {
return Collections.unmodifiableList(edges);
}

public void setEdges(List<Edge> edges) {
/**
* @deprecated prefer {@link #DefaultConnection(List, PageInfo)} and avoid mutation
*/
@Deprecated
public void setEdges(List<Edge<T>> edges) {
if (edges == null) { // TODO remove setter
edges = Collections.emptyList();
}
this.edges = edges;
}

Expand All @@ -23,6 +55,10 @@ public PageInfo getPageInfo() {
return pageInfo;
}

/**
* @deprecated prefer {@link #DefaultConnection(List, PageInfo)} and avoid mutation
*/
@Deprecated
public void setPageInfo(PageInfo pageInfo) {
this.pageInfo = pageInfo;
}
Expand Down
33 changes: 27 additions & 6 deletions src/main/java/graphql/relay/DefaultEdge.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
package graphql.relay;

public class DefaultEdge implements Edge {

public DefaultEdge(Object node, DefaultConnectionCursor cursor) {
public class DefaultEdge<T> implements Edge<T> {

public DefaultEdge(T node, ConnectionCursor cursor) {
if (node == null) {
throw new IllegalArgumentException("node cannot be null");
}
if (cursor == null) {
throw new IllegalArgumentException("cursor cannot be null");
}
this.node = node;
this.cursor = cursor;
}

private Object node;
/**
* @deprecated prefer {@link #DefaultEdge(Object, ConnectionCursor)}
*/
@Deprecated
public DefaultEdge() {
}

private T node;
private ConnectionCursor cursor;

@Override
public Object getNode() {
public T getNode() {
return node;
}

public void setNode(Object node) {
/**
* @deprecated prefer {@link #DefaultEdge(Object, ConnectionCursor)} and avoid mutation.
*/
@Deprecated
public void setNode(T node) {
this.node = node;
}

Expand All @@ -24,6 +41,10 @@ public ConnectionCursor getCursor() {
return cursor;
}

/**
* @deprecated prefer {@link #DefaultEdge(Object, ConnectionCursor)} and avoid mutation.
*/
@Deprecated
public void setCursor(ConnectionCursor cursor) {
this.cursor = cursor;
}
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/graphql/relay/DefaultPageInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,29 @@ public class DefaultPageInfo implements PageInfo {
private boolean hasPreviousPage;
private boolean hasNextPage;

/**
* @deprecated prefer {@link #DefaultPageInfo(ConnectionCursor, ConnectionCursor, boolean, boolean)}
*/
@Deprecated
public DefaultPageInfo() {
}

public DefaultPageInfo(ConnectionCursor startCursor, ConnectionCursor endCursor, boolean hasPreviousPage, boolean hasNextPage) {
this.startCursor = startCursor;
this.endCursor = endCursor;
this.hasPreviousPage = hasPreviousPage;
this.hasNextPage = hasNextPage;
}

@Override
public ConnectionCursor getStartCursor() {
return startCursor;
}

/**
* @deprecated prefer {@link #DefaultPageInfo(ConnectionCursor, ConnectionCursor, boolean, boolean)} and avoid mutation
*/
@Deprecated
public void setStartCursor(ConnectionCursor startCursor) {
this.startCursor = startCursor;
}
Expand All @@ -21,6 +39,10 @@ public ConnectionCursor getEndCursor() {
return endCursor;
}

/**
* @deprecated prefer {@link #DefaultPageInfo(ConnectionCursor, ConnectionCursor, boolean, boolean)} and avoid mutation
*/
@Deprecated
public void setEndCursor(ConnectionCursor endCursor) {
this.endCursor = endCursor;
}
Expand All @@ -30,6 +52,10 @@ public boolean isHasPreviousPage() {
return hasPreviousPage;
}

/**
* @deprecated prefer {@link #DefaultPageInfo(ConnectionCursor, ConnectionCursor, boolean, boolean)} and avoid mutation
*/
@Deprecated
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
Expand All @@ -39,6 +65,10 @@ public boolean isHasNextPage() {
return hasNextPage;
}

/**
* @deprecated prefer {@link #DefaultPageInfo(ConnectionCursor, ConnectionCursor, boolean, boolean)}
*/
@Deprecated
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/graphql/relay/Edge.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
/**
* represents an edge in relay.
*/
public interface Edge {
public interface Edge<T> {

Object getNode();
T getNode();

ConnectionCursor getCursor();

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/graphql/relay/PageInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@
*/
public interface PageInfo {

/**
* @return cursor to the first edge, or null if this page is empty.
*/
ConnectionCursor getStartCursor();

/**
* @return cursor to the last edge, or null if this page is empty.
*/
ConnectionCursor getEndCursor();

/**
* @return true if and only if this page is not the first page. only meaningful when you gave {@code last} argument.
*/
boolean isHasPreviousPage();

/**
* @return true if and only if this page is not the last page. only meaningful when you gave {@code first} argument.
*/
boolean isHasNextPage();

}
32 changes: 17 additions & 15 deletions src/main/java/graphql/relay/Relay.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
public class Relay {

public static final String NODE = "Node";

private GraphQLObjectType pageInfoType = newObject()
.name("PageInfo")
.description("Information about pagination in a connection.")
Expand All @@ -51,7 +52,7 @@ public class Relay {
.build();

public GraphQLInterfaceType nodeInterface(TypeResolver typeResolver) {
GraphQLInterfaceType node = newInterface()
return newInterface()
.name(NODE)
.description("An object with an ID")
.typeResolver(typeResolver)
Expand All @@ -60,11 +61,10 @@ public GraphQLInterfaceType nodeInterface(TypeResolver typeResolver) {
.description("The ID of an object")
.type(new GraphQLNonNull(GraphQLID)))
.build();
return node;
}

public GraphQLFieldDefinition nodeField(GraphQLInterfaceType nodeInterface, DataFetcher nodeDataFetcher) {
GraphQLFieldDefinition fieldDefinition = newFieldDefinition()
return newFieldDefinition()
.name("node")
.description("Fetches an object given its ID")
.type(nodeInterface)
Expand All @@ -74,91 +74,93 @@ public GraphQLFieldDefinition nodeField(GraphQLInterfaceType nodeInterface, Data
.description("The ID of an object")
.type(new GraphQLNonNull(GraphQLID)))
.build();
return fieldDefinition;
}

public List<GraphQLArgument> getConnectionFieldArguments() {
List<GraphQLArgument> args = new ArrayList<>();

args.add(newArgument()
.name("before")
.description("fetching only nodes before this node (exclusive)")
.type(GraphQLString)
.build());
args.add(newArgument()
.name("after")
.description("fetching only nodes after this node (exclusive)")
.type(GraphQLString)
.build());
args.add(newArgument()
.name("first")
.description("fetching only the first certain number of nodes")
.type(GraphQLInt)
.build());
args.add(newArgument()
.name("last")
.description("fetching only the last certain number of nodes")
.type(GraphQLInt)
.build());
return args;
}

public List<GraphQLArgument> getBackwardPaginationConnectionFieldArguments() {
List<GraphQLArgument> args = new ArrayList<>();

args.add(newArgument()
.name("before")
.description("fetching only nodes before this node (exclusive)")
.type(GraphQLString)
.build());
args.add(newArgument()
.name("last")
.description("fetching only the last certain number of nodes")
.type(GraphQLInt)
.build());
return args;
}

public List<GraphQLArgument> getForwardPaginationConnectionFieldArguments() {
List<GraphQLArgument> args = new ArrayList<>();

args.add(newArgument()
.name("after")
.description("fetching only nodes after this node (exclusive)")
.type(GraphQLString)
.build());
args.add(newArgument()
.name("first")
.description("fetching only the first certain number of nodes")
.type(GraphQLInt)
.build());
return args;
}

public GraphQLObjectType edgeType(String name, GraphQLOutputType nodeType, GraphQLInterfaceType nodeInterface, List<GraphQLFieldDefinition> edgeFields) {

GraphQLObjectType edgeType = newObject()
return newObject()
.name(name + "Edge")
.description("An edge in a connection.")
.description("An edge in a connection")
.field(newFieldDefinition()
.name("node")
.type(nodeType)
.description("The item at the end of the edge"))
.field(newFieldDefinition()
.name("cursor")
.type(new GraphQLNonNull(GraphQLString))
.description(""))
.description("cursor marks a unique position or index into the connection"))
.fields(edgeFields)
.build();
return edgeType;
}

public GraphQLObjectType connectionType(String name, GraphQLObjectType edgeType, List<GraphQLFieldDefinition> connectionFields) {

GraphQLObjectType connectionType = newObject()
return newObject()
.name(name + "Connection")
.description("A connection to a list of items.")
.field(newFieldDefinition()
.name("edges")
.description("a list of edges")
.type(new GraphQLList(edgeType)))
.field(newFieldDefinition()
.name("pageInfo")
.description("details about this specific page")
.type(new GraphQLNonNull(pageInfoType)))
.fields(connectionFields)
.build();
return connectionType;
}


Expand Down
Loading