summaryrefslogtreecommitdiffstats
path: root/src/graphql/types
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2024-08-22 19:29:37 +0000
committerSanto Cariotti <santo@dcariotti.me>2024-08-22 19:29:37 +0000
commit8738cf2c6b1ce9f99e3399f35ba9f49832ffed52 (patch)
treef68b15aa606078a486c8cbc21771032a48f61427 /src/graphql/types
parentf70715333fff23230b8281843a0f6873fc5b8ba9 (diff)
Add pagination
Query is `users(limit: X offset Y)` with defaults X=20 Y=0
Diffstat (limited to 'src/graphql/types')
-rw-r--r--src/graphql/types/user.rs11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/graphql/types/user.rs b/src/graphql/types/user.rs
index b675f1f..21554b9 100644
--- a/src/graphql/types/user.rs
+++ b/src/graphql/types/user.rs
@@ -31,7 +31,11 @@ impl User {
}
}
-pub async fn get_users<'ctx>(ctx: &Context<'ctx>) -> Result<Option<Vec<User>>, String> {
+pub async fn get_users<'ctx>(
+ ctx: &Context<'ctx>,
+ limit: Option<i64>,
+ offset: Option<i64>,
+) -> Result<Option<Vec<User>>, String> {
let state = ctx.data::<AppState>().expect("Can't connect to db");
let client = &*state.client;
let auth: &Authentication = ctx.data().unwrap();
@@ -39,7 +43,10 @@ pub async fn get_users<'ctx>(ctx: &Context<'ctx>) -> Result<Option<Vec<User>>, S
Authentication::NotLogged => Err("Unauthorized".to_string()),
Authentication::Logged(_claims) => {
let rows = client
- .query("SELECT id, email, password, is_admin FROM users", &[])
+ .query(
+ "SELECT id, email, password, is_admin FROM users LIMIT $1 OFFSET $2",
+ &[&limit.unwrap_or(20), &offset.unwrap_or(0)],
+ )
.await
.unwrap();