Skip to content

Commit 3255d48

Browse files
committed
ConnectionFetcher now extends DataFetcher<Connection<T>>
1 parent 7653b6b commit 3255d48

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/main/java/graphql/annotations/connection/ConnectionFetcher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
*/
1515
package graphql.annotations.connection;
1616

17+
import graphql.relay.Connection;
1718
import graphql.schema.DataFetcher;
1819

19-
public interface ConnectionFetcher<T> extends DataFetcher<T> {
20+
public interface ConnectionFetcher<T> extends DataFetcher<Connection<T>> {
2021
}

src/main/java/graphql/annotations/connection/EnhancedConnectionFetcher.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,59 @@
1515
package graphql.annotations.connection;
1616

1717
import graphql.relay.*;
18+
import graphql.schema.DataFetcher;
1819
import graphql.schema.DataFetchingEnvironment;
1920

2021
import java.util.ArrayList;
22+
import java.util.Collections;
23+
import java.util.Iterator;
2124
import java.util.List;
2225

2326
/**
2427
* Use this class in {@link GraphQLConnection} to do a real pagination,
2528
* i.e you fetch each time the relevant data, you make the cursors and
2629
* you decide if there are previous or next pages
30+
* <p>
31+
* Note: If you are using the connection, the return type of the associated dataFetcher must implement {@link PaginatedData}
2732
*
28-
* Note: If you are using the connection, the relevant dataFetcher must implements {@link PaginationDataFetcher}
2933
* @param <T> the entity type that is paginated
3034
*/
31-
public class EnhancedConnectionFetcher<T> implements ConnectionFetcher<Connection<T>> {
35+
public class EnhancedConnectionFetcher<T> implements ConnectionFetcher<T> {
3236

33-
private PaginationDataFetcher<T> paginationDataFetcher;
37+
private DataFetcher<PaginatedData<T>> paginationDataFetcher;
3438

35-
public EnhancedConnectionFetcher(PaginationDataFetcher<T> paginationDataFetcher) {
39+
public EnhancedConnectionFetcher(DataFetcher<PaginatedData<T>> paginationDataFetcher) {
3640
this.paginationDataFetcher = paginationDataFetcher;
3741
}
3842

3943
@Override
4044
public Connection<T> get(DataFetchingEnvironment environment) {
41-
List<Edge<T>> edges = buildEdges(paginationDataFetcher.get(environment));
42-
PageInfo pageInfo = getPageInfo(edges);
45+
PaginatedData<T> paginatedData = paginationDataFetcher.get(environment);
46+
if (paginatedData == null) {
47+
return new DefaultConnection<>(Collections.emptyList(), new DefaultPageInfo(null,null,false,false));
48+
}
49+
List<Edge<T>> edges = buildEdges(paginatedData);
50+
PageInfo pageInfo = getPageInfo(edges, paginatedData);
4351
return new DefaultConnection<>(edges, pageInfo);
4452
}
4553

46-
private PageInfo getPageInfo(List<Edge<T>> edges) {
54+
private PageInfo getPageInfo(List<Edge<T>> edges, PaginatedData<T> paginatedData) {
4755
ConnectionCursor firstCursor = edges.get(0).getCursor();
48-
ConnectionCursor lastCursor = edges.get(edges.size()-1).getCursor();
56+
ConnectionCursor lastCursor = edges.get(edges.size() - 1).getCursor();
4957
return new DefaultPageInfo(
5058
firstCursor,
5159
lastCursor,
52-
paginationDataFetcher.hasPreviousPage(firstCursor.getValue()),
53-
paginationDataFetcher.hasNextPage(lastCursor.getValue())
60+
paginatedData.hasPreviousPage(),
61+
paginatedData.hasNextPage()
5462
);
5563
}
5664

57-
private List<Edge<T>> buildEdges(List<T> data) {
65+
private List<Edge<T>> buildEdges(PaginatedData<T> paginatedData) {
66+
Iterator<T> data = paginatedData.iterator();
5867
List<Edge<T>> edges = new ArrayList<>();
59-
for (T object : data) {
60-
edges.add(new DefaultEdge<>(object, new DefaultConnectionCursor(paginationDataFetcher.getCursor(object))));
68+
for (; data.hasNext(); ) {
69+
T entity = data.next();
70+
edges.add(new DefaultEdge<>(entity, new DefaultConnectionCursor(paginatedData.getCursor(entity))));
6171
}
6272
return edges;
6373
}

0 commit comments

Comments
 (0)