-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
998083d
commit ca54df7
Showing
15 changed files
with
172 additions
and
5 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
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,39 @@ | ||
use actix_web::web::Data; | ||
use anymap::{any::Any, Map}; | ||
use async_graphql::dataloader::DataLoader; | ||
|
||
use service::service_provider::ServiceProvider; | ||
|
||
use super::ProductLoader; | ||
|
||
pub type LoaderMap = Map<AnyLoader>; | ||
pub type AnyLoader = dyn Any + Send + Sync; | ||
|
||
pub struct LoaderRegistry { | ||
pub loaders: LoaderMap, | ||
} | ||
|
||
impl LoaderRegistry { | ||
pub fn get<T: anymap::any::Any + Send + Sync>(&self) -> &T { | ||
match self.loaders.get::<T>() { | ||
Some(loader) => loader, | ||
None => unreachable!("{} not found", std::any::type_name::<T>()), | ||
} | ||
} | ||
} | ||
|
||
pub async fn get_loaders_v1(service_provider: Data<ServiceProvider>) -> LoaderMap { | ||
let mut loaders: LoaderMap = LoaderMap::new(); | ||
|
||
let dgraph_client = service_provider.dgraph_client(); | ||
|
||
let product_loader = DataLoader::new( | ||
ProductLoader { | ||
dgraph_client: dgraph_client.clone(), | ||
}, | ||
async_std::task::spawn, | ||
); | ||
loaders.insert(product_loader); | ||
|
||
loaders | ||
} |
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,5 @@ | ||
mod loader_registry; | ||
mod product; | ||
|
||
pub use loader_registry::{get_loaders_v1, LoaderMap, LoaderRegistry}; | ||
pub use product::*; |
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,49 @@ | ||
use async_graphql::dataloader::*; | ||
use async_graphql::*; | ||
use dgraph::{entity_with_parents_by_code, DgraphClient, Entity}; | ||
use std::collections::HashMap; | ||
|
||
pub struct ProductLoader { | ||
pub dgraph_client: DgraphClient, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Loader<String> for ProductLoader { | ||
type Value = Option<Entity>; | ||
type Error = async_graphql::Error; | ||
|
||
async fn load( | ||
&self, | ||
record_ids: &[String], | ||
) -> Result<HashMap<String, Self::Value>, Self::Error> { | ||
let mut result_map: HashMap<String, Option<Entity>> = HashMap::new(); | ||
|
||
for record_id in record_ids { | ||
// Get the entity with all parents with the given record_id | ||
let entity = | ||
entity_with_parents_by_code(&self.dgraph_client, record_id.to_owned()).await?; | ||
let product_entity = get_product_from_entity(entity); | ||
|
||
result_map.insert(record_id.to_string(), product_entity); | ||
} | ||
Ok(result_map) | ||
} | ||
} | ||
|
||
// Recursively get the product entity from the given entity | ||
fn get_product_from_entity(entity: Option<Entity>) -> Option<Entity> { | ||
println!("get_product_from_entity: {:?}", entity); | ||
|
||
let entity = match entity { | ||
Some(entity) => entity, | ||
None => return None, | ||
}; | ||
if entity.r#type == "Product" { | ||
return Some(entity); | ||
} | ||
|
||
if entity.parents.len() > 0 { | ||
return get_product_from_entity(Some(entity.parents[0].clone())); | ||
} | ||
None | ||
} |
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