com.introproventures.graphql.jpa.query.schema.relay.PageFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of graphql-jpa-query-schema Show documentation
Show all versions of graphql-jpa-query-schema Show documentation
Provides GraphQL JPA Query Schema Generation and Execution Support
The newest version!
package com.introproventures.graphql.jpa.query.schema.relay;
import graphql.relay.ConnectionCursor;
import graphql.relay.DefaultEdge;
import graphql.relay.DefaultPageInfo;
import graphql.relay.Edge;
import graphql.relay.PageInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
public class PageFactory {
public static Page createOffsetBasedPage(List nodes, long count, long offset) {
return createOffsetBasedPage(
nodes,
offset,
hasNextPage(nodes.size(), count, offset),
hasPreviousPage(count, offset)
);
}
public static Page createOffsetBasedPage(
List nodes,
long offset,
boolean hasNextPage,
boolean hasPreviousPage
) {
return createPage(nodes, offsetBasedCursorProvider(offset), hasNextPage, hasPreviousPage);
}
public static > P createOffsetBasedPage(
List nodes,
long count,
long offset,
BiFunction>, PageInfo, P> pageCreator
) {
BiFunction> edgeCreator = DefaultEdge::new;
return createOffsetBasedConnection(nodes, count, offset, edgeCreator, pageCreator);
}
public static , C extends Connection> C createOffsetBasedConnection(
List nodes,
long count,
long offset,
BiFunction edgeCreator,
BiFunction, PageInfo, C> connectionCreator
) {
List edges = createEdges(nodes, offsetBasedCursorProvider(offset), edgeCreator);
return connectionCreator.apply(
edges,
createPageInfo(edges, hasNextPage(nodes.size(), count, offset), hasPreviousPage(count, offset))
);
}
public static Page createPage(
List nodes,
CursorProvider cursorProvider,
boolean hasNextPage,
boolean hasPreviousPage
) {
List> edges = createEdges(nodes, cursorProvider);
return new GenericPage<>(edges, createPageInfo(edges, hasNextPage, hasPreviousPage));
}
public static List> createEdges(List nodes, CursorProvider cursorProvider) {
BiFunction> edgeCreator = DefaultEdge::new;
return createEdges(nodes, cursorProvider, edgeCreator);
}
public static > List createEdges(
List nodes,
CursorProvider cursorProvider,
BiFunction edgeCreator
) {
List edges = new ArrayList<>(nodes.size());
int index = 0;
for (N node : nodes) {
edges.add(edgeCreator.apply(node, cursorProvider.createCursor(node, index++)));
}
return edges;
}
public static > PageInfo createOffsetBasedPageInfo(List edges, long count, long offset) {
return createPageInfo(edges, hasNextPage(edges.size(), count, offset), hasPreviousPage(count, offset));
}
public static > PageInfo createPageInfo(
List edges,
boolean hasNextPage,
boolean hasPreviousPage
) {
ConnectionCursor firstCursor = null;
ConnectionCursor lastCursor = null;
if (!edges.isEmpty()) {
firstCursor = edges.get(0).getCursor();
lastCursor = edges.get(edges.size() - 1).getCursor();
}
return new DefaultPageInfo(firstCursor, lastCursor, hasPreviousPage, hasNextPage);
}
public static CursorProvider offsetBasedCursorProvider(long offset) {
return (node, index) -> new OffsetBasedCursor(offset + index + 1).toConnectionCursor();
}
public static boolean hasNextPage(long nodes, long count, long offset) {
return offset + nodes < count;
}
public static boolean hasPreviousPage(long count, long offset) {
return offset > 0 && count > 0;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy