|
15 | 15 | package graphql.annotations.connection; |
16 | 16 |
|
17 | 17 | import graphql.relay.*; |
| 18 | +import graphql.schema.DataFetcher; |
18 | 19 | import graphql.schema.DataFetchingEnvironment; |
19 | 20 |
|
20 | 21 | import java.util.ArrayList; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.Iterator; |
21 | 24 | import java.util.List; |
22 | 25 |
|
23 | 26 | /** |
24 | 27 | * Use this class in {@link GraphQLConnection} to do a real pagination, |
25 | 28 | * i.e you fetch each time the relevant data, you make the cursors and |
26 | 29 | * 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} |
27 | 32 | * |
28 | | - * Note: If you are using the connection, the relevant dataFetcher must implements {@link PaginationDataFetcher} |
29 | 33 | * @param <T> the entity type that is paginated |
30 | 34 | */ |
31 | | -public class EnhancedConnectionFetcher<T> implements ConnectionFetcher<Connection<T>> { |
| 35 | +public class EnhancedConnectionFetcher<T> implements ConnectionFetcher<T> { |
32 | 36 |
|
33 | | - private PaginationDataFetcher<T> paginationDataFetcher; |
| 37 | + private DataFetcher<PaginatedData<T>> paginationDataFetcher; |
34 | 38 |
|
35 | | - public EnhancedConnectionFetcher(PaginationDataFetcher<T> paginationDataFetcher) { |
| 39 | + public EnhancedConnectionFetcher(DataFetcher<PaginatedData<T>> paginationDataFetcher) { |
36 | 40 | this.paginationDataFetcher = paginationDataFetcher; |
37 | 41 | } |
38 | 42 |
|
39 | 43 | @Override |
40 | 44 | 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); |
43 | 51 | return new DefaultConnection<>(edges, pageInfo); |
44 | 52 | } |
45 | 53 |
|
46 | | - private PageInfo getPageInfo(List<Edge<T>> edges) { |
| 54 | + private PageInfo getPageInfo(List<Edge<T>> edges, PaginatedData<T> paginatedData) { |
47 | 55 | ConnectionCursor firstCursor = edges.get(0).getCursor(); |
48 | | - ConnectionCursor lastCursor = edges.get(edges.size()-1).getCursor(); |
| 56 | + ConnectionCursor lastCursor = edges.get(edges.size() - 1).getCursor(); |
49 | 57 | return new DefaultPageInfo( |
50 | 58 | firstCursor, |
51 | 59 | lastCursor, |
52 | | - paginationDataFetcher.hasPreviousPage(firstCursor.getValue()), |
53 | | - paginationDataFetcher.hasNextPage(lastCursor.getValue()) |
| 60 | + paginatedData.hasPreviousPage(), |
| 61 | + paginatedData.hasNextPage() |
54 | 62 | ); |
55 | 63 | } |
56 | 64 |
|
57 | | - private List<Edge<T>> buildEdges(List<T> data) { |
| 65 | + private List<Edge<T>> buildEdges(PaginatedData<T> paginatedData) { |
| 66 | + Iterator<T> data = paginatedData.iterator(); |
58 | 67 | 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)))); |
61 | 71 | } |
62 | 72 | return edges; |
63 | 73 | } |
|
0 commit comments