query.rs

Overview

This file defines core components for implementing cursor-based pagination in data queries. It provides an enumeration for pagination direction and a struct encapsulating pagination parameters typically used in paginated data fetching scenarios. The functionality supports both forward and backward pagination, enabling efficient navigation through large datasets by managing limits, cursors, and page boundaries.

Components

Enum: PaginateDirection

Represents the direction of pagination.

Struct: PaginationArgs

Encapsulates the arguments necessary for paginating over a collection of items.

These fields correspond to standard cursor-based pagination parameters, supporting both forward and backward traversal of data.


Methods

get_limit(&self) -> usize

Returns the maximum number of items to fetch based on the pagination arguments.

let args = PaginationArgs { first: Some(10), after: None, last: None, before: None };
let limit = args.get_limit(); // returns 11

shrink_portion<T>(&self, portion: &mut Vec<T>)

Adjusts the vector of fetched items by removing one item depending on the pagination direction, ensuring the returned portion respects the requested limit.

let mut items = vec![1, 2, 3, 4];
let args = PaginationArgs { first: Some(3), after: None, last: None, before: None };
args.shrink_portion(&mut items);
// items now contains three elements, one removed to match the limit

get_direction(&self) -> PaginateDirection

Determines the direction of pagination based on which arguments are set.

let args = PaginationArgs { first: None, after: None, last: Some(5), before: None };
assert_eq!(args.get_direction(), PaginateDirection::Backward);

get_bound_markers(&self, num_nodes: usize) -> (bool, bool)

Determines if there are previous or next pages available given the number of nodes fetched.

let args = PaginationArgs { first: Some(10), after: None, last: None, before: None };
let (prev, next) = args.get_bound_markers(11);

has_next_page(&self, num_nodes: usize) -> bool

Checks if a next page exists based on the current pagination arguments and the number of nodes fetched.

let args = PaginationArgs { first: Some(10), after: None, last: None, before: None };
let has_next = args.has_next_page(12); // true

has_previous_page(&self, num_nodes: usize) -> bool

Checks if a previous page exists based on the current pagination arguments and the number of nodes fetched.

let args = PaginationArgs { first: None, after: Some("cursor".to_string()), last: Some(5), before: None };
let has_prev = args.has_previous_page(7); // true

Implementation Details


Interaction with Other System Components


Structure Diagram

classDiagram
class PaginationArgs {
+first: Option<usize>
+after: Option<String>
+last: Option<usize>
+before: Option<String>
+get_limit() usize
+shrink_portion(&mut Vec<T>)
+get_direction() PaginateDirection
+get_bound_markers(num_nodes: usize) (bool, bool)
+has_next_page(num_nodes: usize) bool
+has_previous_page(num_nodes: usize) bool
}
class PaginateDirection {
<<enumeration>>
+Forward
+Backward
}
PaginationArgs --> PaginateDirection : uses