-
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.
sample implementation of dataloader for canteen
- Loading branch information
1 parent
abd23fc
commit 47e1b83
Showing
11 changed files
with
158 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,47 @@ | ||
use std::collections::HashMap; | ||
|
||
use async_trait::async_trait; | ||
|
||
use crate::{util::Uuid, interface::persistent_data::{DataError, RequestDataAccess, model::Canteen}}; | ||
|
||
|
||
use async_graphql::dataloader::Loader; | ||
|
||
|
||
|
||
pub struct CanteenDataLoader { | ||
data: Box<dyn RequestDataAccess> | ||
} | ||
|
||
impl CanteenDataLoader { | ||
pub fn new(data: impl RequestDataAccess + 'static) -> Self { | ||
Self { | ||
data: Box::new(data), | ||
} | ||
} | ||
} | ||
|
||
// little bit useless here... maybe useful fore cashing | ||
#[async_trait] | ||
impl Loader<()> for CanteenDataLoader { | ||
type Value = Vec<Canteen>; | ||
type Error = DataError; | ||
|
||
async fn load(&self, _keys: &[()]) -> Result<HashMap<(), Self::Value>, Self::Error> { | ||
self.data.get_canteens().await.map(|c| HashMap::from([((), c)])) | ||
} | ||
|
||
|
||
} | ||
|
||
#[async_trait] | ||
impl Loader<Uuid> for CanteenDataLoader { | ||
type Value = Option<Canteen>; | ||
type Error = DataError; | ||
|
||
async fn load(&self, keys: &[Uuid]) -> Result<HashMap<Uuid, Self::Value>, Self::Error> { | ||
self.data.get_canteen_multi(keys).await | ||
} | ||
|
||
|
||
} |
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 |
---|---|---|
|
@@ -6,5 +6,6 @@ pub mod query; | |
pub mod server; | ||
mod types; | ||
pub mod util; | ||
pub mod dataloader; | ||
|
||
mod tests; |
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