Skip to content

Commit 8d68ec2

Browse files
author
Jiayu Liu
committed
type param and misc.
1 parent 3abfdd7 commit 8d68ec2

File tree

9 files changed

+170
-64
lines changed

9 files changed

+170
-64
lines changed

src/main/java/graphql/relay/Connection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
/**
66
* represents a connection in relay.
77
*/
8-
public interface Connection {
8+
public interface Connection<T> {
99

10-
List<Edge> getEdges();
10+
List<Edge<T>> getEdges();
1111

1212
PageInfo getPageInfo();
1313

src/main/java/graphql/relay/ConnectionCursor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package graphql.relay;
22

33
/**
4-
* represents a connection cursor in relay.
4+
* represents a {@link Connection connection} cursor in relay.
55
*/
66
public interface ConnectionCursor {
77

src/main/java/graphql/relay/DefaultConnection.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,52 @@
11
package graphql.relay;
22

33
import java.util.ArrayList;
4+
import java.util.Collections;
45
import java.util.List;
56

6-
public class DefaultConnection implements Connection {
7+
public class DefaultConnection<T> implements Connection<T> {
78

8-
private List<Edge> edges = new ArrayList<Edge>();
9+
private List<Edge<T>> edges = new ArrayList<Edge<T>>();
910

1011
private PageInfo pageInfo;
1112

13+
/**
14+
* @deprecated prefer {@link #DefaultConnection(List, PageInfo)}
15+
*/
16+
@Deprecated
17+
public DefaultConnection() {
18+
}
19+
20+
/**
21+
* @param edges edges
22+
* @param pageInfo page info
23+
* @throws IllegalArgumentException if edges or page info is null. use {@link Collections#emptyList()} for empty edges.
24+
*/
25+
public DefaultConnection(List<Edge<T>> edges, PageInfo pageInfo) {
26+
if (edges == null) {
27+
throw new IllegalArgumentException("edges cannot be empty");
28+
}
29+
if (pageInfo == null) {
30+
throw new IllegalArgumentException("page info cannot be null");
31+
}
32+
// TODO make defensive copy
33+
this.edges = edges;
34+
this.pageInfo = pageInfo;
35+
}
36+
1237
@Override
13-
public List<Edge> getEdges() {
14-
return edges;
38+
public List<Edge<T>> getEdges() {
39+
return Collections.unmodifiableList(edges);
1540
}
1641

17-
public void setEdges(List<Edge> edges) {
42+
/**
43+
* @deprecated prefer {@link #DefaultConnection(List, PageInfo)} and avoid mutation
44+
*/
45+
@Deprecated
46+
public void setEdges(List<Edge<T>> edges) {
47+
if (edges == null) { // TODO remove setter
48+
edges = Collections.emptyList();
49+
}
1850
this.edges = edges;
1951
}
2052

@@ -23,6 +55,10 @@ public PageInfo getPageInfo() {
2355
return pageInfo;
2456
}
2557

58+
/**
59+
* @deprecated prefer {@link #DefaultConnection(List, PageInfo)} and avoid mutation
60+
*/
61+
@Deprecated
2662
public void setPageInfo(PageInfo pageInfo) {
2763
this.pageInfo = pageInfo;
2864
}

src/main/java/graphql/relay/DefaultEdge.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
package graphql.relay;
22

3-
public class DefaultEdge implements Edge {
4-
5-
public DefaultEdge(Object node, DefaultConnectionCursor cursor) {
3+
public class DefaultEdge<T> implements Edge<T> {
4+
5+
public DefaultEdge(T node, ConnectionCursor cursor) {
6+
if (node == null) {
7+
throw new IllegalArgumentException("node cannot be null");
8+
}
9+
if (cursor == null) {
10+
throw new IllegalArgumentException("cursor cannot be null");
11+
}
612
this.node = node;
713
this.cursor = cursor;
814
}
915

10-
private Object node;
16+
/**
17+
* @deprecated prefer {@link #DefaultEdge(Object, ConnectionCursor)}
18+
*/
19+
@Deprecated
20+
public DefaultEdge() {
21+
}
22+
23+
private T node;
1124
private ConnectionCursor cursor;
1225

1326
@Override
14-
public Object getNode() {
27+
public T getNode() {
1528
return node;
1629
}
1730

18-
public void setNode(Object node) {
31+
/**
32+
* @deprecated prefer {@link #DefaultEdge(Object, ConnectionCursor)} and avoid mutation.
33+
*/
34+
@Deprecated
35+
public void setNode(T node) {
1936
this.node = node;
2037
}
2138

@@ -24,6 +41,10 @@ public ConnectionCursor getCursor() {
2441
return cursor;
2542
}
2643

44+
/**
45+
* @deprecated prefer {@link #DefaultEdge(Object, ConnectionCursor)} and avoid mutation.
46+
*/
47+
@Deprecated
2748
public void setCursor(ConnectionCursor cursor) {
2849
this.cursor = cursor;
2950
}

src/main/java/graphql/relay/DefaultPageInfo.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,29 @@ public class DefaultPageInfo implements PageInfo {
77
private boolean hasPreviousPage;
88
private boolean hasNextPage;
99

10+
/**
11+
* @deprecated prefer {@link #DefaultPageInfo(ConnectionCursor, ConnectionCursor, boolean, boolean)}
12+
*/
13+
@Deprecated
14+
public DefaultPageInfo() {
15+
}
16+
17+
public DefaultPageInfo(ConnectionCursor startCursor, ConnectionCursor endCursor, boolean hasPreviousPage, boolean hasNextPage) {
18+
this.startCursor = startCursor;
19+
this.endCursor = endCursor;
20+
this.hasPreviousPage = hasPreviousPage;
21+
this.hasNextPage = hasNextPage;
22+
}
23+
1024
@Override
1125
public ConnectionCursor getStartCursor() {
1226
return startCursor;
1327
}
1428

29+
/**
30+
* @deprecated prefer {@link #DefaultPageInfo(ConnectionCursor, ConnectionCursor, boolean, boolean)} and avoid mutation
31+
*/
32+
@Deprecated
1533
public void setStartCursor(ConnectionCursor startCursor) {
1634
this.startCursor = startCursor;
1735
}
@@ -21,6 +39,10 @@ public ConnectionCursor getEndCursor() {
2139
return endCursor;
2240
}
2341

42+
/**
43+
* @deprecated prefer {@link #DefaultPageInfo(ConnectionCursor, ConnectionCursor, boolean, boolean)} and avoid mutation
44+
*/
45+
@Deprecated
2446
public void setEndCursor(ConnectionCursor endCursor) {
2547
this.endCursor = endCursor;
2648
}
@@ -30,6 +52,10 @@ public boolean isHasPreviousPage() {
3052
return hasPreviousPage;
3153
}
3254

55+
/**
56+
* @deprecated prefer {@link #DefaultPageInfo(ConnectionCursor, ConnectionCursor, boolean, boolean)} and avoid mutation
57+
*/
58+
@Deprecated
3359
public void setHasPreviousPage(boolean hasPreviousPage) {
3460
this.hasPreviousPage = hasPreviousPage;
3561
}
@@ -39,6 +65,10 @@ public boolean isHasNextPage() {
3965
return hasNextPage;
4066
}
4167

68+
/**
69+
* @deprecated prefer {@link #DefaultPageInfo(ConnectionCursor, ConnectionCursor, boolean, boolean)}
70+
*/
71+
@Deprecated
4272
public void setHasNextPage(boolean hasNextPage) {
4373
this.hasNextPage = hasNextPage;
4474
}

src/main/java/graphql/relay/Edge.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
/**
44
* represents an edge in relay.
55
*/
6-
public interface Edge {
6+
public interface Edge<T> {
77

8-
Object getNode();
8+
T getNode();
99

1010
ConnectionCursor getCursor();
1111

src/main/java/graphql/relay/PageInfo.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@
55
*/
66
public interface PageInfo {
77

8+
/**
9+
* @return cursor to the first edge, or null if this page is empty.
10+
*/
811
ConnectionCursor getStartCursor();
912

13+
/**
14+
* @return cursor to the last edge, or null if this page is empty.
15+
*/
1016
ConnectionCursor getEndCursor();
1117

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

23+
/**
24+
* @return true if and only if this page is not the last page. only meaningful when you gave {@code first} argument.
25+
*/
1426
boolean isHasNextPage();
15-
1627
}

src/main/java/graphql/relay/Relay.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
public class Relay {
1818

1919
public static final String NODE = "Node";
20+
2021
private GraphQLObjectType pageInfoType = newObject()
2122
.name("PageInfo")
2223
.description("Information about pagination in a connection.")
@@ -39,7 +40,7 @@ public class Relay {
3940
.build();
4041

4142
public GraphQLInterfaceType nodeInterface(TypeResolver typeResolver) {
42-
GraphQLInterfaceType node = newInterface()
43+
return newInterface()
4344
.name(NODE)
4445
.description("An object with an ID")
4546
.typeResolver(typeResolver)
@@ -48,11 +49,10 @@ public GraphQLInterfaceType nodeInterface(TypeResolver typeResolver) {
4849
.description("The ID of an object")
4950
.type(new GraphQLNonNull(GraphQLID)))
5051
.build();
51-
return node;
5252
}
5353

5454
public GraphQLFieldDefinition nodeField(GraphQLInterfaceType nodeInterface, DataFetcher nodeDataFetcher) {
55-
GraphQLFieldDefinition fieldDefinition = newFieldDefinition()
55+
return newFieldDefinition()
5656
.name("node")
5757
.description("Fetches an object given its ID")
5858
.type(nodeInterface)
@@ -62,91 +62,93 @@ public GraphQLFieldDefinition nodeField(GraphQLInterfaceType nodeInterface, Data
6262
.description("The ID of an object")
6363
.type(new GraphQLNonNull(GraphQLID)))
6464
.build();
65-
return fieldDefinition;
6665
}
6766

6867
public List<GraphQLArgument> getConnectionFieldArguments() {
6968
List<GraphQLArgument> args = new ArrayList<GraphQLArgument>();
70-
7169
args.add(newArgument()
7270
.name("before")
71+
.description("fetching only nodes before this node (exclusive)")
7372
.type(GraphQLString)
7473
.build());
7574
args.add(newArgument()
7675
.name("after")
76+
.description("fetching only nodes after this node (exclusive)")
7777
.type(GraphQLString)
7878
.build());
7979
args.add(newArgument()
8080
.name("first")
81+
.description("fetching only the first certain number of nodes")
8182
.type(GraphQLInt)
8283
.build());
8384
args.add(newArgument()
8485
.name("last")
86+
.description("fetching only the last certain number of nodes")
8587
.type(GraphQLInt)
8688
.build());
8789
return args;
8890
}
8991

9092
public List<GraphQLArgument> getBackwardPaginationConnectionFieldArguments() {
9193
List<GraphQLArgument> args = new ArrayList<GraphQLArgument>();
92-
9394
args.add(newArgument()
9495
.name("before")
96+
.description("fetching only nodes before this node (exclusive)")
9597
.type(GraphQLString)
9698
.build());
9799
args.add(newArgument()
98100
.name("last")
101+
.description("fetching only the last certain number of nodes")
99102
.type(GraphQLInt)
100103
.build());
101104
return args;
102105
}
103106

104107
public List<GraphQLArgument> getForwardPaginationConnectionFieldArguments() {
105108
List<GraphQLArgument> args = new ArrayList<GraphQLArgument>();
106-
107109
args.add(newArgument()
108110
.name("after")
111+
.description("fetching only nodes after this node (exclusive)")
109112
.type(GraphQLString)
110113
.build());
111114
args.add(newArgument()
112115
.name("first")
116+
.description("fetching only the first certain number of nodes")
113117
.type(GraphQLInt)
114118
.build());
115119
return args;
116120
}
117121

118122
public GraphQLObjectType edgeType(String name, GraphQLOutputType nodeType, GraphQLInterfaceType nodeInterface, List<GraphQLFieldDefinition> edgeFields) {
119-
120-
GraphQLObjectType edgeType = newObject()
123+
return newObject()
121124
.name(name + "Edge")
122-
.description("An edge in a connection.")
125+
.description("An edge in a connection")
123126
.field(newFieldDefinition()
124127
.name("node")
125128
.type(nodeType)
126129
.description("The item at the end of the edge"))
127130
.field(newFieldDefinition()
128131
.name("cursor")
129132
.type(new GraphQLNonNull(GraphQLString))
130-
.description(""))
133+
.description("cursor marks a unique position or index into the connection"))
131134
.fields(edgeFields)
132135
.build();
133-
return edgeType;
134136
}
135137

136138
public GraphQLObjectType connectionType(String name, GraphQLObjectType edgeType, List<GraphQLFieldDefinition> connectionFields) {
137-
138-
GraphQLObjectType connectionType = newObject()
139+
return newObject()
139140
.name(name + "Connection")
140141
.description("A connection to a list of items.")
141142
.field(newFieldDefinition()
142143
.name("edges")
144+
.description("a list of edges")
143145
.type(new GraphQLList(edgeType)))
144146
.field(newFieldDefinition()
145147
.name("pageInfo")
148+
.description("details about this specific page")
146149
.type(new GraphQLNonNull(pageInfoType)))
147150
.fields(connectionFields)
148151
.build();
149-
return connectionType;
150152
}
151153

152154

0 commit comments

Comments
 (0)