All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.graphglue.connection.model.PageInfo.kt Maven / Gradle / Ivy

Go to download

A library to develop annotation-based code-first GraphQL servers using GraphQL Kotlin, Spring Boot and Neo4j - excluding Spring GraphQL server dependencies

There is a newer version: 7.0.5
Show newest version
package io.github.graphglue.connection.model

import com.expediagroup.graphql.generator.annotations.GraphQLDescription
import com.fasterxml.jackson.databind.ObjectMapper
import io.github.graphglue.connection.order.Order
import io.github.graphglue.data.execution.NodeQueryOptions
import io.github.graphglue.model.Node

/**
 * Page info used in GraphQL connection to provide general pagination information
 *
 * @param nodeQueryOptions options used for querying the connection
 * @param allNodes the list of all nodes returned from the query, before first or last was applied to limit results
 * @param nodes the list of nodes which is returned
 * @param objectMapper used for cursor generation
 */
@GraphQLDescription("Information about the current page in a connection")
class PageInfo(
    private val nodeQueryOptions: NodeQueryOptions,
    private val allNodes: List,
    private val nodes: List,
    private val objectMapper: ObjectMapper
) {

    /**
     * Order in which nodes are sorted, used for cursor generation
     */
    @Suppress("UNCHECKED_CAST")
    private val orderBy: Order
        get() = nodeQueryOptions.orderBy as Order

    /**
     * Cursor of the first [Node] in [nodes]
     * When paginating forwards, the cursor to continue
     */
    @GraphQLDescription("When paginating forwards, the cursor to continue")
    val startCursor: String?
        get() {
            return if (nodes.isEmpty()) {
                null
            } else {
                orderBy.generateCursor(nodes.first(), objectMapper)
            }
        }

    /**
     * Cursor of the last [Node] in [nodes]
     * When paginating backwards, the cursor to continue
     */
    @GraphQLDescription("When paginating backwards, the cursor to continue")
    val endCursor: String?
        get() {
            return if (nodes.isEmpty()) {
                null
            } else {
                orderBy.generateCursor(nodes.last(), objectMapper)
            }
        }

    /**
     * When paginating forwards, are there more items?
     * Calculating using [allNodes] and [nodes]
     */
    @GraphQLDescription("When paginating forwards, are there more items?")
    val hasNextPage: Boolean
        get() {
            return if (nodeQueryOptions.first != null) {
                allNodes.size > (nodeQueryOptions.first - 1)
            } else {
                false
            }
        }

    /**
     * When paginating backwards, are there more items?
     * Calculating using [allNodes] and [nodes]
     */
    @GraphQLDescription("When paginating backwards, are there more items?")
    val hasPreviousPage: Boolean
        get() {
            return if (nodeQueryOptions.last != null) {
                allNodes.size > (nodeQueryOptions.last - 1)
            } else {
                false
            }
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy