Files
axum-async-graphql/src/dataloader/benutzer_rollen.rs

52 lines
1.3 KiB
Rust
Raw Normal View History

use async_graphql::dataloader::*;
use async_graphql::*;
use itertools::Itertools;
use std::collections::HashMap;
use std::sync::Arc;
use crate::models::rolle::Rolle;
use crate::scalar::Id;
pub struct BenutzerRollenLoader {
pub pool: sqlx::PgPool,
}
impl Loader<Id> for BenutzerRollenLoader {
type Value = Vec<Rolle>;
type Error = Arc<sqlx::Error>;
async fn load(&self, keys: &[Id]) -> Result<HashMap<Id, Self::Value>, Self::Error> {
let rows = sqlx::query!(
r#"
SELECT
br.benutzer_id,
r.id,
r.rollenname,
r.erstellt_am,
r.geaendert_am
FROM rollen AS r
LEFT JOIN benutzer_rollen AS br ON r.id = br.rolle_id
WHERE br.benutzer_id = ANY($1);
"#,
keys
)
.fetch_all(&self.pool)
.await?
.into_iter()
.map(|row| {
(
row.benutzer_id,
Rolle {
id: row.id,
rollenname: row.rollenname,
erstellt_am: row.erstellt_am,
geaendert_am: row.geaendert_am,
},
)
})
.into_group_map();
Ok(rows)
}
}