Skip to content

Commit f4ebaa1

Browse files
authored
Merge pull request graphql-java#267 from Jimexist/feature/connection-interface
extract relay classes to interfaces and implementations
2 parents 5e9f899 + adb7fed commit f4ebaa1

9 files changed

Lines changed: 209 additions & 117 deletions
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package graphql.relay;
2+
3+
public class DefaultConnectionCursor implements ConnectionCursor {
4+
5+
private final String value;
6+
7+
public DefaultConnectionCursor(String value) {
8+
if (value == null || value.isEmpty()) {
9+
throw new IllegalArgumentException("connection value cannot be null or empty");
10+
}
11+
this.value = value;
12+
}
13+
14+
@Override
15+
public String getValue() {
16+
return value;
17+
}
18+
19+
@Override
20+
public boolean equals(Object o) {
21+
if (this == o) {
22+
return true;
23+
}
24+
if (o == null || getClass() != o.getClass()) {
25+
return false;
26+
}
27+
DefaultConnectionCursor that = (DefaultConnectionCursor) o;
28+
if (value != null ? !value.equals(that.value) : that.value != null) {
29+
return false;
30+
}
31+
return true;
32+
}
33+
34+
@Override
35+
public int hashCode() {
36+
return value != null ? value.hashCode() : 0;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return value;
42+
}
43+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package graphql.relay;
2+
3+
public class DefaultEdge implements Edge {
4+
5+
public DefaultEdge(Object node, DefaultConnectionCursor cursor) {
6+
this.node = node;
7+
this.cursor = cursor;
8+
}
9+
10+
private Object node;
11+
private ConnectionCursor cursor;
12+
13+
@Override
14+
public Object getNode() {
15+
return node;
16+
}
17+
18+
public void setNode(Object node) {
19+
this.node = node;
20+
}
21+
22+
@Override
23+
public ConnectionCursor getCursor() {
24+
return cursor;
25+
}
26+
27+
public void setCursor(ConnectionCursor cursor) {
28+
this.cursor = cursor;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
final StringBuilder sb = new StringBuilder("DefaultEdge{");
34+
sb.append("node=").append(node);
35+
sb.append(", cursor=").append(cursor);
36+
sb.append('}');
37+
return sb.toString();
38+
}
39+
}
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+
}
Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
package graphql.relay;
22

3+
/**
4+
* represents an edge in relay.
5+
*/
6+
public interface Edge {
37

4-
public class Edge {
8+
Object getNode();
59

6-
public Edge(Object node, ConnectionCursor cursor) {
7-
this.node = node;
8-
this.cursor = cursor;
9-
}
10+
ConnectionCursor getCursor();
1011

11-
Object node;
12-
ConnectionCursor cursor;
13-
14-
public Object getNode() {
15-
return node;
16-
}
17-
18-
public void setNode(Object node) {
19-
this.node = node;
20-
}
21-
22-
public ConnectionCursor getCursor() {
23-
return cursor;
24-
}
25-
26-
public void setCursor(ConnectionCursor cursor) {
27-
this.cursor = cursor;
28-
}
2912
}
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
}

0 commit comments

Comments
 (0)