server: fix backward pagination

This commit is contained in:
Bill Thiede 2023-11-25 08:39:56 -08:00
parent f7df834325
commit 24414b04bb
3 changed files with 10 additions and 20 deletions

View File

@ -17,7 +17,7 @@ log = "0.4.17"
tokio = "1.26.0" tokio = "1.26.0"
glog = "0.1.0" glog = "0.1.0"
urlencoding = "2.1.3" urlencoding = "2.1.3"
async-graphql = "6.0.11" async-graphql = { version = "6.0.11", features = ["log"] }
async-graphql-rocket = "6.0.11" async-graphql-rocket = "6.0.11"
rocket_cors = "0.6.0" rocket_cors = "0.6.0"
rayon = "1.8.0" rayon = "1.8.0"

View File

@ -167,6 +167,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription) let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription)
.data(Notmuch::default()) .data(Notmuch::default())
.extension(async_graphql::extensions::Logger)
.finish(); .finish();
let _ = rocket::build() let _ = rocket::build()

View File

@ -64,31 +64,20 @@ impl QueryRoot {
last, last,
|after, before, first, last| async move { |after, before, first, last| async move {
info!("{after:?} {before:?} {first:?} {last:?} {query}"); info!("{after:?} {before:?} {first:?} {last:?} {query}");
let mut start = 0usize;
let total = nm.count(&query)?; let total = nm.count(&query)?;
let page_size = first.unwrap_or(20);
if let Some(after) = after { let mut start = after.map(|after| after + 1).unwrap_or(0);
if after >= total { let mut end = before.unwrap_or(total);
return Ok(Connection::new(false, false)); if let Some(first) = first {
} end = (start + first).min(end);
start = after + 1;
} }
let mut end = start + page_size; if let Some(last) = last {
if end > total { start = if last > end - start { end } else { end - last };
end = total;
}
// TODO(wathiede): handle last/end.
if let Some(before) = before {
if before == 0 {
return Ok(Connection::new(false, false));
}
end = before;
} }
let count = end - start;
let slice: Vec<ThreadSummary> = nm let slice: Vec<ThreadSummary> = nm
.search(&query, start, first.unwrap_or(20))? .search(&query, start, count)?
.0 .0
.into_iter() .into_iter()
.map(|ts| ThreadSummary { .map(|ts| ThreadSummary {