Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collections Api New #140

Merged
merged 5 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions api/src/dataloaders/collection_drop.rs
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>>>()
}
}
6 changes: 6 additions & 0 deletions api/src/dataloaders/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
mod collection;
mod collection_drop;
mod collection_mints;
mod creators;
mod drop;
mod drops;
mod holders;
mod metadata_json;
mod project_collection;
mod project_collections;
mod purchases;

pub use collection::Loader as CollectionLoader;
pub use collection_drop::Loader as CollectionDropLoader;
pub use collection_mints::{
Loader as CollectionMintsLoader, OwnerLoader as CollectionMintsOwnerLoader,
};
Expand All @@ -18,6 +22,8 @@ pub use holders::Loader as HoldersLoader;
pub use metadata_json::{
AttributesLoader as MetadataJsonAttributesLoader, Loader as MetadataJsonLoader,
};
pub use project_collection::ProjectCollectionLoader;
pub use project_collections::ProjectCollectionsLoader;
pub use purchases::{
CollectionLoader as CollectionPurchasesLoader, DropLoader as DropPurchasesLoader,
};
37 changes: 37 additions & 0 deletions api/src/dataloaders/project_collection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::collections::HashMap;

use async_graphql::{dataloader::Loader as DataLoader, FieldError, Result};
use poem::async_trait;
use sea_orm::prelude::*;

use crate::{db::Connection, entities::collections, objects::Collection};

#[derive(Debug, Clone)]
pub struct ProjectCollectionLoader {
pub db: Connection,
}

impl ProjectCollectionLoader {
#[must_use]
pub fn new(db: Connection) -> Self {
Self { db }
}
}

#[async_trait]
impl DataLoader<Uuid> for ProjectCollectionLoader {
type Error = FieldError;
type Value = Collection;

async fn load(&self, keys: &[Uuid]) -> Result<HashMap<Uuid, Self::Value>, Self::Error> {
let collections = collections::Entity::find()
.filter(collections::Column::ProjectId.is_in(keys.iter().map(ToOwned::to_owned)))
.all(self.db.get())
.await?;

Ok(collections
.into_iter()
.map(|collection| (collection.id, collection.into()))
.collect())
}
}
43 changes: 43 additions & 0 deletions api/src/dataloaders/project_collections.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::collections::HashMap;

use async_graphql::{dataloader::Loader as DataLoader, FieldError, Result};
use poem::async_trait;
use sea_orm::prelude::*;

use crate::{db::Connection, entities::collections, objects::Collection};

#[derive(Debug, Clone)]
pub struct ProjectCollectionsLoader {
pub db: Connection,
}

impl ProjectCollectionsLoader {
#[must_use]
pub fn new(db: Connection) -> Self {
Self { db }
}
}

#[async_trait]
impl DataLoader<Uuid> for ProjectCollectionsLoader {
type Error = FieldError;
type Value = Vec<Collection>;

async fn load(&self, keys: &[Uuid]) -> Result<HashMap<Uuid, Self::Value>, Self::Error> {
let collections = collections::Entity::find()
.filter(collections::Column::ProjectId.is_in(keys.iter().map(ToOwned::to_owned)))
.all(self.db.get())
.await?;

Ok(collections
.into_iter()
.fold(HashMap::new(), |mut acc, collection| {
acc.entry(collection.project_id).or_insert_with(Vec::new);

acc.entry(collection.project_id)
.and_modify(|collections| collections.push(collection.into()));

acc
}))
}
}
19 changes: 16 additions & 3 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
};
use blockchains::{polygon::Polygon, solana::Solana};
use dataloaders::{
CollectionLoader, CollectionMintsLoader, CollectionMintsOwnerLoader, CollectionPurchasesLoader,
CreatorsLoader, DropLoader, DropPurchasesLoader, HoldersLoader, MetadataJsonAttributesLoader,
MetadataJsonLoader, ProjectDropsLoader,
CollectionDropLoader, CollectionLoader, CollectionMintsLoader, CollectionMintsOwnerLoader,
CollectionPurchasesLoader, CreatorsLoader, DropLoader, DropPurchasesLoader, HoldersLoader,
MetadataJsonAttributesLoader, MetadataJsonLoader, ProjectCollectionLoader,
ProjectCollectionsLoader, ProjectDropsLoader,
};
use db::Connection;
use hub_core::{
Expand Down Expand Up @@ -255,11 +256,14 @@
organization_id: OrganizationId,
balance: Balance,
project_drops_loader: DataLoader<ProjectDropsLoader>,
project_collections_loader: DataLoader<ProjectCollectionsLoader>,
project_collection_loader: DataLoader<ProjectCollectionLoader>,
collection_loader: DataLoader<CollectionLoader>,
metadata_json_loader: DataLoader<MetadataJsonLoader>,
metadata_json_attributes_loader: DataLoader<MetadataJsonAttributesLoader>,
collection_mints_loader: DataLoader<CollectionMintsLoader>,
collection_mints_owner_loader: DataLoader<CollectionMintsOwnerLoader>,
collection_drop_loader: DataLoader<CollectionDropLoader>,
drop_loader: DataLoader<DropLoader>,
creators_loader: DataLoader<CreatorsLoader>,
holders_loader: DataLoader<HoldersLoader>,
Expand All @@ -278,6 +282,10 @@
let project_drops_loader =
DataLoader::new(ProjectDropsLoader::new(db.clone()), tokio::spawn);
let collection_loader = DataLoader::new(CollectionLoader::new(db.clone()), tokio::spawn);
let project_collections_loader =
DataLoader::new(ProjectCollectionsLoader::new(db.clone()), tokio::spawn);
let project_collection_loader =

Check warning on line 287 in api/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy/check/doc

binding's name is too similar to existing binding
DataLoader::new(ProjectCollectionLoader::new(db.clone()), tokio::spawn);
let metadata_json_loader =
DataLoader::new(MetadataJsonLoader::new(db.clone()), tokio::spawn);
let metadata_json_attributes_loader =
Expand All @@ -286,6 +294,8 @@
DataLoader::new(CollectionMintsLoader::new(db.clone()), tokio::spawn);
let collection_mints_owner_loader =
DataLoader::new(CollectionMintsOwnerLoader::new(db.clone()), tokio::spawn);
let collection_drop_loader: DataLoader<_> =
DataLoader::new(CollectionDropLoader::new(db.clone()), tokio::spawn);
let drop_loader = DataLoader::new(DropLoader::new(db.clone()), tokio::spawn);
let creators_loader = DataLoader::new(CreatorsLoader::new(db.clone()), tokio::spawn);
let holders_loader = DataLoader::new(HoldersLoader::new(db.clone()), tokio::spawn);
Expand All @@ -300,11 +310,14 @@
organization_id,
balance,
project_drops_loader,
project_collections_loader,
project_collection_loader,
collection_loader,
metadata_json_loader,
metadata_json_attributes_loader,
collection_mints_loader,
collection_mints_owner_loader,
collection_drop_loader,
drop_loader,
creators_loader,
holders_loader,
Expand Down
11 changes: 10 additions & 1 deletion api/src/objects/collection.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_graphql::{Context, Object, Result};
use sea_orm::entity::prelude::*;

use super::{metadata_json::MetadataJson, Holder};
use super::{metadata_json::MetadataJson, Drop, Holder};
use crate::{
entities::{
collection_creators, collection_mints,
Expand Down Expand Up @@ -138,6 +138,15 @@ impl Collection {

collection_purchases_loader.load_one(self.id).await
}

async fn drop(&self, ctx: &Context<'_>) -> Result<Option<Drop>> {
let AppContext {
collection_drop_loader,
..
} = ctx.data::<AppContext>()?;

collection_drop_loader.load_one(self.id).await
}
}

impl From<Model> for Collection {
Expand Down
24 changes: 23 additions & 1 deletion api/src/objects/project.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use async_graphql::{ComplexObject, Context, Result, SimpleObject};
use hub_core::uuid::Uuid;

use crate::{objects::Drop, AppContext};
use crate::{
objects::{Collection, Drop},
AppContext,
};

#[derive(SimpleObject, Debug, Clone)]
#[graphql(complex)]
Expand All @@ -27,4 +30,23 @@ impl Project {

drop_loader.load_one(id).await
}

/// The collections associated with the project.
async fn collections(&self, ctx: &Context<'_>) -> Result<Option<Vec<Collection>>> {
let AppContext {
project_collections_loader,
..
} = ctx.data::<AppContext>()?;

project_collections_loader.load_one(self.id).await
}

async fn collection(&self, ctx: &Context<'_>, id: Uuid) -> Result<Option<Collection>> {
let AppContext {
project_collection_loader,
..
} = ctx.data::<AppContext>()?;

project_collection_loader.load_one(id).await
}
}
Loading