Skip to content

Commit

Permalink
Add pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvigneshwars committed May 1, 2024
1 parent 9f63b43 commit a8b7a88
Showing 1 changed file with 55 additions and 8 deletions.
63 changes: 55 additions & 8 deletions datasets/src/graphql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
mod entity;

use async_graphql::{
ComplexObject, Context, EmptyMutation, EmptySubscription, Object, Schema, SchemaBuilder,
types::connection::*, ComplexObject, Context, EmptyMutation, EmptySubscription, Object, Schema,
SchemaBuilder,
};
use entity::{DataCollection, Session};
use models::data_collection;
use sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter};
use sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter, QuerySelect};

/// The GraphQL schema exposed by the service
pub type RootSchema = Schema<Query, EmptyMutation, EmptySubscription>;
Expand All @@ -22,25 +23,71 @@ pub struct Query;

#[ComplexObject]
impl Session {
/// Fetches all the data collected during a session
/// Fetches all the data collected during a session with pagination
async fn data_collection(
&self,
ctx: &Context<'_>,
) -> async_graphql::Result<Vec<DataCollection>> {
after: Option<String>,
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
) -> async_graphql::Result<Connection<usize, DataCollection, EmptyFields, EmptyFields>> {
let database = ctx.data::<DatabaseConnection>()?;
Ok(data_collection::Entity::find()
let mut start: usize = after
.map(|after| after.parse::<usize>().ok().unwrap_or(0))
.unwrap_or(0);
let mut end: usize = before
.map(|before| before.parse::<usize>().ok().unwrap_or(usize::MAX))
.unwrap_or(usize::MAX);

if let Some(first) = first {
end = (start + first as usize).min(end);
}
if let Some(last) = last {
start = if last as usize > end - start {
end
} else {
end - last as usize
};
}

let limit: u64 = match end.checked_sub(start) {
Some(diff) => diff as u64,
None => return Err(async_graphql::Error::new("Pagination limit overflow")),
};

let data_collection_entities = data_collection::Entity::find()
.filter(data_collection::Column::Sessionid.eq(self.id))
.limit(limit)
.offset(start as u64)
.all(database)
.await?
.await?;

let data_collections: Vec<DataCollection> = data_collection_entities
.into_iter()
.map(DataCollection::from)
.collect())
.collect::<Vec<_>>();

let mut connection: Connection<usize, DataCollection> =
Connection::new(start > 0, end < usize::MAX);
connection
.edges
.extend(
data_collections
.into_iter()
.enumerate()
.map(|(index, data_collection)| {
Edge::with_additional_fields(index + start, data_collection, EmptyFields)
}),
);

Ok(connection)
}
}

#[Object]
impl Query {
// /// Reference sessions resolver for the router
/// Reference sessions resolver for the router
#[graphql(entity)]
async fn router_sessions(&self, id: i32) -> Session {
Session { id }
Expand Down

0 comments on commit a8b7a88

Please sign in to comment.