Skip to content

Commit cb7917d

Browse files
authored
Merge pull request #360 from Jimexist/feature/relay-param
type param and misc. for relay types
2 parents be15168 + c3aeaa5 commit cb7917d

9 files changed

Lines changed: 167 additions & 61 deletions

File tree

‎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<>();
9+
private List<Edge<T>> edges = new ArrayList<>();
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
@@ -29,6 +29,7 @@
2929
public class Relay {
3030

3131
public static final String NODE = "Node";
32+
3233
private GraphQLObjectType pageInfoType = newObject()
3334
.name("PageInfo")
3435
.description("Information about pagination in a connection.")
@@ -51,7 +52,7 @@ public class Relay {
5152
.build();
5253

5354
public GraphQLInterfaceType nodeInterface(TypeResolver typeResolver) {
54-
GraphQLInterfaceType node = newInterface()
55+
return newInterface()
5556
.name(NODE)
5657
.description("An object with an ID")
5758
.typeResolver(typeResolver)
@@ -60,11 +61,10 @@ public GraphQLInterfaceType nodeInterface(TypeResolver typeResolver) {
6061
.description("The ID of an object")
6162
.type(new GraphQLNonNull(GraphQLID)))
6263
.build();
63-
return node;
6464
}
6565

6666
public GraphQLFieldDefinition nodeField(GraphQLInterfaceType nodeInterface, DataFetcher nodeDataFetcher) {
67-
GraphQLFieldDefinition fieldDefinition = newFieldDefinition()
67+
return newFieldDefinition()
6868
.name("node")
6969
.description("Fetches an object given its ID")
7070
.type(nodeInterface)
@@ -74,91 +74,93 @@ public GraphQLFieldDefinition nodeField(GraphQLInterfaceType nodeInterface, Data
7474
.description("The ID of an object")
7575
.type(new GraphQLNonNull(GraphQLID)))
7676
.build();
77-
return fieldDefinition;
7877
}
7978

8079
public List<GraphQLArgument> getConnectionFieldArguments() {
8180
List<GraphQLArgument> args = new ArrayList<>();
82-
8381
args.add(newArgument()
8482
.name("before")
83+
.description("fetching only nodes before this node (exclusive)")
8584
.type(GraphQLString)
8685
.build());
8786
args.add(newArgument()
8887
.name("after")
88+
.description("fetching only nodes after this node (exclusive)")
8989
.type(GraphQLString)
9090
.build());
9191
args.add(newArgument()
9292
.name("first")
93+
.description("fetching only the first certain number of nodes")
9394
.type(GraphQLInt)
9495
.build());
9596
args.add(newArgument()
9697
.name("last")
98+
.description("fetching only the last certain number of nodes")
9799
.type(GraphQLInt)
98100
.build());
99101
return args;
100102
}
101103

102104
public List<GraphQLArgument> getBackwardPaginationConnectionFieldArguments() {
103105
List<GraphQLArgument> args = new ArrayList<>();
104-
105106
args.add(newArgument()
106107
.name("before")
108+
.description("fetching only nodes before this node (exclusive)")
107109
.type(GraphQLString)
108110
.build());
109111
args.add(newArgument()
110112
.name("last")
113+
.description("fetching only the last certain number of nodes")
111114
.type(GraphQLInt)
112115
.build());
113116
return args;
114117
}
115118

116119
public List<GraphQLArgument> getForwardPaginationConnectionFieldArguments() {
117120
List<GraphQLArgument> args = new ArrayList<>();
118-
119121
args.add(newArgument()
120122
.name("after")
123+
.description("fetching only nodes after this node (exclusive)")
121124
.type(GraphQLString)
122125
.build());
123126
args.add(newArgument()
124127
.name("first")
128+
.description("fetching only the first certain number of nodes")
125129
.type(GraphQLInt)
126130
.build());
127131
return args;
128132
}
129133

130134
public GraphQLObjectType edgeType(String name, GraphQLOutputType nodeType, GraphQLInterfaceType nodeInterface, List<GraphQLFieldDefinition> edgeFields) {
131-
132-
GraphQLObjectType edgeType = newObject()
135+
return newObject()
133136
.name(name + "Edge")
134-
.description("An edge in a connection.")
137+
.description("An edge in a connection")
135138
.field(newFieldDefinition()
136139
.name("node")
137140
.type(nodeType)
138141
.description("The item at the end of the edge"))
139142
.field(newFieldDefinition()
140143
.name("cursor")
141144
.type(new GraphQLNonNull(GraphQLString))
142-
.description(""))
145+
.description("cursor marks a unique position or index into the connection"))
143146
.fields(edgeFields)
144147
.build();
145-
return edgeType;
146148
}
147149

148150
public GraphQLObjectType connectionType(String name, GraphQLObjectType edgeType, List<GraphQLFieldDefinition> connectionFields) {
149-
150-
GraphQLObjectType connectionType = newObject()
151+
return newObject()
151152
.name(name + "Connection")
152153
.description("A connection to a list of items.")
153154
.field(newFieldDefinition()
154155
.name("edges")
156+
.description("a list of edges")
155157
.type(new GraphQLList(edgeType)))
156158
.field(newFieldDefinition()
157159
.name("pageInfo")
160+
.description("details about this specific page")
158161
.type(new GraphQLNonNull(pageInfoType)))
159162
.fields(connectionFields)
160163
.build();
161-
return connectionType;
162164
}
163165

164166

0 commit comments

Comments
 (0)