use of graphql.relay.DefaultConnectionCursor in project blaze-persistence by Blazebit.
the class GraphQLEntityViewSupport method createRelayConnection.
/**
* Returns a relay connection from the given result list.
*
* @param list the result list
* @return the relay connection
*/
public <T> Connection<T> createRelayConnection(List<T> list) {
List<Edge<T>> edges = new ArrayList<>(list.size());
boolean hasPreviousPage;
boolean hasNextPage;
KeysetPage keysetPage;
if (list instanceof PagedList<?>) {
PagedList<T> data = (PagedList<T>) list;
hasPreviousPage = data.getFirstResult() != 0;
hasNextPage = data.getTotalSize() == -1 || data.getFirstResult() + data.getMaxResults() < data.getTotalSize();
keysetPage = data.getKeysetPage();
} else {
hasPreviousPage = true;
hasNextPage = true;
keysetPage = null;
}
if (keysetPage == null) {
for (int i = 0; i < list.size(); i++) {
edges.add(new DefaultEdge<>(list.get(i), new DefaultConnectionCursor(Integer.toString(i + 1))));
}
} else {
PagedList<T> data = (PagedList<T>) list;
List<Keyset> keysets = keysetPage.getKeysets();
int listSize = list.size();
if (listSize != 0 && keysets.size() != listSize) {
int end = listSize - 1;
edges.add(new DefaultEdge<>(list.get(0), new DefaultConnectionCursor(Base64.getEncoder().encodeToString(serializeCursor(data.getFirstResult(), data.getMaxResults(), keysetPage.getLowest().getTuple())))));
for (int i = 1; i < end; i++) {
T node = list.get(i);
edges.add(new DefaultEdge<>(node, new DefaultConnectionCursor(Integer.toString(i + 1))));
}
edges.add(new DefaultEdge<>(list.get(end), new DefaultConnectionCursor(Base64.getEncoder().encodeToString(serializeCursor(data.getFirstResult(), data.getMaxResults(), keysetPage.getHighest().getTuple())))));
} else {
for (int i = 0; i < list.size(); i++) {
T node = list.get(i);
edges.add(new DefaultEdge<>(node, new DefaultConnectionCursor(Base64.getEncoder().encodeToString(serializeCursor(data.getFirstResult(), data.getMaxResults(), keysets.get(i).getTuple())))));
}
}
}
PageInfo pageInfo;
if (edges.isEmpty()) {
pageInfo = new DefaultPageInfo(null, null, hasPreviousPage, hasNextPage);
} else {
pageInfo = new DefaultPageInfo(edges.get(0).getCursor(), edges.get(edges.size() - 1).getCursor(), hasPreviousPage, hasNextPage);
}
return new DefaultConnection<>(edges, pageInfo);
}
Aggregations