Skip to content

Commit 7927b9a

Browse files
author
Jiayu Liu
committed
relay interfaces
1 parent b549805 commit 7927b9a

File tree

8 files changed

+163
-96
lines changed

8 files changed

+163
-96
lines changed
Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
package graphql.relay;
22

3-
4-
import java.util.ArrayList;
53
import java.util.List;
64

7-
public class Connection {
8-
private List<Edge> edges = new ArrayList<Edge>();
9-
10-
private PageInfo pageInfo;
11-
12-
public List<Edge> getEdges() {
13-
return edges;
14-
}
5+
/**
6+
* represents a connection in relay.
7+
*/
8+
public interface Connection {
159

16-
public void setEdges(List<Edge> edges) {
17-
this.edges = edges;
18-
}
10+
List<Edge> getEdges();
1911

20-
public PageInfo getPageInfo() {
21-
return pageInfo;
22-
}
12+
PageInfo getPageInfo();
2313

24-
public void setPageInfo(PageInfo pageInfo) {
25-
this.pageInfo = pageInfo;
26-
}
2714
}
Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,10 @@
11
package graphql.relay;
22

3+
/**
4+
* represents a connection cursor in relay.
5+
*/
6+
public interface ConnectionCursor {
37

4-
public class ConnectionCursor {
8+
String getValue();
59

6-
private final String value;
7-
8-
public ConnectionCursor(String value) {
9-
this.value = value;
10-
}
11-
12-
public String getValue() {
13-
return value;
14-
}
15-
16-
@Override
17-
public boolean equals(Object o) {
18-
19-
20-
if (this == o) return true;
21-
if (o == null || getClass() != o.getClass()) return false;
22-
23-
ConnectionCursor that = (ConnectionCursor) o;
24-
25-
if (value != null ? !value.equals(that.value) : that.value != null) return false;
26-
27-
return true;
28-
}
29-
30-
@Override
31-
public int hashCode() {
32-
return value != null ? value.hashCode() : 0;
33-
}
34-
35-
@Override
36-
public String toString() {
37-
return value;
38-
}
3910
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package graphql.relay;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class DefaultConnection implements Connection {
7+
8+
private List<Edge> edges = new ArrayList<Edge>();
9+
10+
private PageInfo pageInfo;
11+
12+
@Override
13+
public List<Edge> getEdges() {
14+
return edges;
15+
}
16+
17+
public void setEdges(List<Edge> edges) {
18+
this.edges = edges;
19+
}
20+
21+
@Override
22+
public PageInfo getPageInfo() {
23+
return pageInfo;
24+
}
25+
26+
public void setPageInfo(PageInfo pageInfo) {
27+
this.pageInfo = pageInfo;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
final StringBuilder sb = new StringBuilder("DefaultConnection{");
33+
sb.append("edges=").append(edges);
34+
sb.append(", pageInfo=").append(pageInfo);
35+
sb.append('}');
36+
return sb.toString();
37+
}
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package graphql.relay;
2+
3+
public class DefaultConnectionCursor implements ConnectionCursor {
4+
5+
private final String value;
6+
7+
public DefaultConnectionCursor(String value) {
8+
this.value = value;
9+
}
10+
11+
@Override
12+
public String getValue() {
13+
return value;
14+
}
15+
16+
@Override
17+
public boolean equals(Object o) {
18+
if (this == o) {
19+
return true;
20+
}
21+
if (o == null || getClass() != o.getClass()) {
22+
return false;
23+
}
24+
DefaultConnectionCursor that = (DefaultConnectionCursor) o;
25+
if (value != null ? !value.equals(that.value) : that.value != null) {
26+
return false;
27+
}
28+
return true;
29+
}
30+
31+
@Override
32+
public int hashCode() {
33+
return value != null ? value.hashCode() : 0;
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return value;
39+
}
40+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package graphql.relay;
2+
3+
public class DefaultPageInfo implements PageInfo {
4+
5+
private ConnectionCursor startCursor;
6+
private ConnectionCursor endCursor;
7+
private boolean hasPreviousPage;
8+
private boolean hasNextPage;
9+
10+
@Override
11+
public ConnectionCursor getStartCursor() {
12+
return startCursor;
13+
}
14+
15+
public void setStartCursor(ConnectionCursor startCursor) {
16+
this.startCursor = startCursor;
17+
}
18+
19+
@Override
20+
public ConnectionCursor getEndCursor() {
21+
return endCursor;
22+
}
23+
24+
public void setEndCursor(ConnectionCursor endCursor) {
25+
this.endCursor = endCursor;
26+
}
27+
28+
@Override
29+
public boolean isHasPreviousPage() {
30+
return hasPreviousPage;
31+
}
32+
33+
public void setHasPreviousPage(boolean hasPreviousPage) {
34+
this.hasPreviousPage = hasPreviousPage;
35+
}
36+
37+
@Override
38+
public boolean isHasNextPage() {
39+
return hasNextPage;
40+
}
41+
42+
public void setHasNextPage(boolean hasNextPage) {
43+
this.hasNextPage = hasNextPage;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
final StringBuilder sb = new StringBuilder("DefaultPageInfo{");
49+
sb.append("startCursor=").append(startCursor);
50+
sb.append(", endCursor=").append(endCursor);
51+
sb.append(", hasPreviousPage=").append(hasPreviousPage);
52+
sb.append(", hasNextPage=").append(hasNextPage);
53+
sb.append('}');
54+
return sb.toString();
55+
}
56+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
public class Edge {
55

6-
public Edge(Object node, ConnectionCursor cursor) {
6+
public Edge(Object node, DefaultConnectionCursor cursor) {
77
this.node = node;
88
this.cursor = cursor;
99
}
1010

1111
Object node;
12-
ConnectionCursor cursor;
12+
DefaultConnectionCursor cursor;
1313

1414
public Object getNode() {
1515
return node;
@@ -19,11 +19,11 @@ public void setNode(Object node) {
1919
this.node = node;
2020
}
2121

22-
public ConnectionCursor getCursor() {
22+
public DefaultConnectionCursor getCursor() {
2323
return cursor;
2424
}
2525

26-
public void setCursor(ConnectionCursor cursor) {
26+
public void setCursor(DefaultConnectionCursor cursor) {
2727
this.cursor = cursor;
2828
}
2929
}
Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,16 @@
11
package graphql.relay;
22

3+
/**
4+
* represents a page in relay.
5+
*/
6+
public interface PageInfo {
37

4-
public class PageInfo {
5-
private ConnectionCursor startCursor;
6-
private ConnectionCursor endCursor;
7-
private boolean hasPreviousPage;
8-
private boolean hasNextPage;
8+
ConnectionCursor getStartCursor();
99

10-
public ConnectionCursor getStartCursor() {
11-
return startCursor;
12-
}
10+
ConnectionCursor getEndCursor();
1311

14-
public void setStartCursor(ConnectionCursor startCursor) {
15-
this.startCursor = startCursor;
16-
}
12+
boolean isHasPreviousPage();
1713

18-
public ConnectionCursor getEndCursor() {
19-
return endCursor;
20-
}
14+
boolean isHasNextPage();
2115

22-
public void setEndCursor(ConnectionCursor endCursor) {
23-
this.endCursor = endCursor;
24-
}
25-
26-
public boolean isHasPreviousPage() {
27-
return hasPreviousPage;
28-
}
29-
30-
public void setHasPreviousPage(boolean hasPreviousPage) {
31-
this.hasPreviousPage = hasPreviousPage;
32-
}
33-
34-
public boolean isHasNextPage() {
35-
return hasNextPage;
36-
}
37-
38-
public void setHasNextPage(boolean hasNextPage) {
39-
this.hasNextPage = hasNextPage;
40-
}
4116
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private List<Edge> buildEdges() {
2222
List<Edge> edges = new ArrayList<Edge>();
2323
int ix = 0;
2424
for (Object object : data) {
25-
edges.add(new Edge(object, new ConnectionCursor(createCursor(ix++))));
25+
edges.add(new Edge(object, new DefaultConnectionCursor(createCursor(ix++))));
2626
}
2727
return edges;
2828
}
@@ -65,30 +65,30 @@ public Object get(DataFetchingEnvironment environment) {
6565
Edge firstEdge = edges.get(0);
6666
Edge lastEdge = edges.get(edges.size() - 1);
6767

68-
PageInfo pageInfo = new PageInfo();
68+
DefaultPageInfo pageInfo = new DefaultPageInfo();
6969
pageInfo.setStartCursor(firstEdge.getCursor());
7070
pageInfo.setEndCursor(lastEdge.getCursor());
7171
pageInfo.setHasPreviousPage(!firstEdge.getCursor().equals(firstPresliceCursor));
7272
pageInfo.setHasNextPage(!lastEdge.getCursor().equals(lastPresliceCursor));
7373

74-
Connection connection = new Connection();
74+
DefaultConnection connection = new DefaultConnection();
7575
connection.setEdges(edges);
7676
connection.setPageInfo(pageInfo);
7777

7878
return connection;
7979
}
8080

8181
private Connection emptyConnection() {
82-
Connection connection = new Connection();
83-
connection.setPageInfo(new PageInfo());
82+
DefaultConnection connection = new DefaultConnection();
83+
connection.setPageInfo(new DefaultPageInfo());
8484
return connection;
8585
}
8686

8787

8888
public ConnectionCursor cursorForObjectInConnection(Object object) {
8989
int index = data.indexOf(object);
9090
String cursor = createCursor(index);
91-
return new ConnectionCursor(cursor);
91+
return new DefaultConnectionCursor(cursor);
9292
}
9393

9494

0 commit comments

Comments
 (0)