Search in sources :

Example 1 with Edge

use of graphql.relay.Edge in project synapse by americanexpress.

the class ConnectionUtil method create.

/**
 * Create the {@link Connection}.
 * @param <T> type of {@link UniversallyUniqueIdentifiable} element
 * @param elements used to create the edges of the connection
 * @param first number of elements
 * @param after the opaque cursor
 * @return the {@link Connection}
 */
public static final <T extends UniversallyUniqueIdentifiable> Connection<T> create(List<T> elements, long first, String after) {
    // Get the limit of this stream which is either the number of elements
    // specified by "first"; otherwise the full elements size
    long limit = first > 0 ? first : elements.size();
    // Create the edges for the connection
    List<Edge<T>> edges = elements.stream().limit(limit).map(element -> new DefaultEdge<>(element, ConnectionCursorUtil.from(element.getId()))).collect(Collectors.toList());
    // Create the page information for the connection
    ConnectionCursor startCursor = ConnectionCursorUtil.getStartCursor(edges);
    ConnectionCursor endCursor = ConnectionCursorUtil.getEndCursor(edges);
    boolean hasPreviousPage = after != null;
    boolean hasNextPage = edges.size() >= first;
    PageInfo pageInfo = new DefaultPageInfo(startCursor, endCursor, hasPreviousPage, hasNextPage);
    return new DefaultConnection<>(edges, pageInfo);
}
Also used : PageInfo(graphql.relay.PageInfo) List(java.util.List) DefaultPageInfo(graphql.relay.DefaultPageInfo) Connection(graphql.relay.Connection) DefaultConnection(graphql.relay.DefaultConnection) UniversallyUniqueIdentifiable(io.americanexpress.synapse.service.graphql.model.UniversallyUniqueIdentifiable) Edge(graphql.relay.Edge) ConnectionCursor(graphql.relay.ConnectionCursor) DefaultEdge(graphql.relay.DefaultEdge) Collectors(java.util.stream.Collectors) ConnectionCursor(graphql.relay.ConnectionCursor) PageInfo(graphql.relay.PageInfo) DefaultPageInfo(graphql.relay.DefaultPageInfo) DefaultConnection(graphql.relay.DefaultConnection) DefaultEdge(graphql.relay.DefaultEdge) Edge(graphql.relay.Edge) DefaultEdge(graphql.relay.DefaultEdge) DefaultPageInfo(graphql.relay.DefaultPageInfo)

Aggregations

Connection (graphql.relay.Connection)1 ConnectionCursor (graphql.relay.ConnectionCursor)1 DefaultConnection (graphql.relay.DefaultConnection)1 DefaultEdge (graphql.relay.DefaultEdge)1 DefaultPageInfo (graphql.relay.DefaultPageInfo)1 Edge (graphql.relay.Edge)1 PageInfo (graphql.relay.PageInfo)1 UniversallyUniqueIdentifiable (io.americanexpress.synapse.service.graphql.model.UniversallyUniqueIdentifiable)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1