Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Paginate library pins query
Browse files Browse the repository at this point in the history
  • Loading branch information
garryod committed Oct 9, 2023
1 parent ddf96e7 commit cf83e9f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/pin_packing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = { workspace = true }
async-graphql = { workspace = true }
axum = { workspace = true }
clap = { workspace = true }
Expand Down
49 changes: 45 additions & 4 deletions backend/pin_packing/src/resolvers/pin_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ use crate::tables::{
pin_library::{self, PinStatus},
pin_mount,
};
use async_graphql::{ComplexObject, Context, Object};
use async_graphql::{
connection::{query, Connection, Edge, EmptyFields},
ComplexObject, Context, Object,
};
use opa_client::subject_authorization;
use sea_orm::{ActiveValue, DatabaseConnection, EntityTrait, IntoActiveModel, ModelTrait};
use sea_orm::{
ActiveValue, CursorTrait, DatabaseConnection, EntityTrait, IntoActiveModel, ModelTrait,
};

#[ComplexObject]
impl pin_library::Model {
Expand All @@ -23,10 +28,46 @@ impl PinLibraryQuery {
async fn library_pins(
&self,
ctx: &Context<'_>,
) -> async_graphql::Result<Vec<pin_library::Model>> {
after: Option<String>,
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
) -> async_graphql::Result<Connection<String, pin_library::Model, EmptyFields, EmptyFields>>
{
subject_authorization!("xchemlab.pin_packing.read_pin_library", ctx).await?;
let database = ctx.data::<DatabaseConnection>()?;
Ok(pin_library::Entity::find().all(database).await?)
let pin_query = pin_library::Entity::find();
query(
after,
before,
first,
last,
|after, before, first, last| async move {
let mut pin_cursor = pin_query.cursor_by(pin_library::Column::Barcode);
if let Some(after) = after {
pin_cursor.after(after);
}
if let Some(before) = before {
pin_cursor.before(before);
}
if let Some(first) = first {
pin_cursor.first(first as u64);
}
if let Some(last) = last {
pin_cursor.last(last as u64);
}

let pins = pin_cursor.all(database).await?;

let mut connection = Connection::new(true, true);
connection.edges.extend(
pins.into_iter()
.map(|pin| Edge::new(pin.barcode.clone(), pin)),
);
Ok::<_, async_graphql::Error>(connection)
},
)
.await
}
}

Expand Down

0 comments on commit cf83e9f

Please sign in to comment.