generated from holaplex/hub-rust-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from holaplex/main
Release 07/27
- Loading branch information
Showing
38 changed files
with
2,394 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::collections::HashMap; | ||
|
||
use async_graphql::{dataloader::Loader as DataLoader, FieldError, Result}; | ||
use poem::async_trait; | ||
use sea_orm::{prelude::*, JoinType, QuerySelect}; | ||
|
||
use crate::{ | ||
db::Connection, | ||
entities::{collections, drops}, | ||
objects::Drop, | ||
}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Loader { | ||
pub db: Connection, | ||
} | ||
|
||
impl Loader { | ||
#[must_use] | ||
pub fn new(db: Connection) -> Self { | ||
Self { db } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl DataLoader<Uuid> for Loader { | ||
type Error = FieldError; | ||
type Value = Drop; | ||
|
||
async fn load(&self, keys: &[Uuid]) -> Result<HashMap<Uuid, Self::Value>, Self::Error> { | ||
let drops = drops::Entity::find() | ||
.join(JoinType::InnerJoin, drops::Relation::Collections.def()) | ||
.select_also(collections::Entity) | ||
.filter(drops::Column::CollectionId.is_in(keys.iter().map(ToOwned::to_owned))) | ||
.all(self.db.get()) | ||
.await?; | ||
|
||
drops | ||
.into_iter() | ||
.map(|(drop, collection)| { | ||
Ok(( | ||
drop.collection_id, | ||
Drop::new( | ||
drop.clone(), | ||
collection.ok_or(FieldError::new(format!( | ||
"no collection for the drop {}", | ||
drop.id | ||
)))?, | ||
), | ||
)) | ||
}) | ||
.collect::<Result<HashMap<Uuid, Self::Value>>>() | ||
} | ||
} |
Oops, something went wrong.