22mod entities;
33use crate :: S3Bucket ;
44use async_graphql:: {
5+ dataloader:: { DataLoader , Loader } ,
56 ComplexObject , Context , EmptyMutation , EmptySubscription , Object , Schema , SchemaBuilder ,
67} ;
78use aws_sdk_s3:: presigning:: PresigningConfig ;
@@ -15,7 +16,9 @@ use models::{
1516 processing_job_parameter,
1617} ;
1718use sea_orm:: { ColumnTrait , DatabaseConnection , EntityTrait , QueryFilter } ;
19+ use std:: collections:: HashMap ;
1820use std:: time:: Duration ;
21+ use tracing:: instrument;
1922use url:: Url ;
2023
2124use self :: entities:: AutoProcProgram ;
@@ -24,29 +27,62 @@ use self::entities::AutoProcProgram;
2427pub type RootSchema = Schema < Query , EmptyMutation , EmptySubscription > ;
2528
2629/// A schema builder for the service
27- pub fn root_schema_builder ( ) -> SchemaBuilder < Query , EmptyMutation , EmptySubscription > {
28- Schema :: build ( Query , EmptyMutation , EmptySubscription ) . enable_federation ( )
30+ pub fn root_schema_builder (
31+ database : DatabaseConnection ,
32+ ) -> SchemaBuilder < Query , EmptyMutation , EmptySubscription > {
33+ Schema :: build ( Query , EmptyMutation , EmptySubscription )
34+ . data ( DataLoader :: new (
35+ DataCollectionLoader :: new ( database. clone ( ) ) ,
36+ tokio:: spawn,
37+ ) )
38+ . data ( database)
39+ . enable_federation ( )
2940}
3041
3142/// The root query of the service
3243#[ derive( Debug , Clone , Default ) ]
3344pub struct Query ;
3445
46+ pub struct DataCollectionLoader ( DatabaseConnection ) ;
47+
48+ impl DataCollectionLoader {
49+ fn new ( database : DatabaseConnection ) -> Self {
50+ Self ( database)
51+ }
52+ }
53+
54+ impl Loader < u32 > for DataCollectionLoader {
55+ type Value = DataProcessing ;
56+ type Error = async_graphql:: Error ;
57+
58+ async fn load ( & self , keys : & [ u32 ] ) -> Result < HashMap < u32 , Self :: Value > , Self :: Error > {
59+ let mut results = HashMap :: new ( ) ;
60+ let keys_vec: Vec < u32 > = keys. iter ( ) . cloned ( ) . collect ( ) ;
61+ let records = data_collection_file_attachment:: Entity :: find ( )
62+ . filter ( data_collection_file_attachment:: Column :: DataCollectionId . is_in ( keys_vec) )
63+ . all ( & self . 0 )
64+ . await ?;
65+
66+ for record in records {
67+ let data_collection_id = record. data_collection_id ;
68+ let data = DataProcessing :: from ( record) ;
69+
70+ results. insert ( data_collection_id, data) ;
71+ }
72+
73+ Ok ( results)
74+ }
75+ }
76+
3577#[ ComplexObject ]
3678impl DataCollection {
3779 /// Fetched all the processed data from data collection during a session
3880 async fn processed_data (
3981 & self ,
4082 ctx : & Context < ' _ > ,
41- ) -> Result < Vec < DataProcessing > , async_graphql:: Error > {
42- let database = ctx. data :: < DatabaseConnection > ( ) ?;
43- Ok ( data_collection_file_attachment:: Entity :: find ( )
44- . filter ( data_collection_file_attachment:: Column :: DataCollectionId . eq ( self . id ) )
45- . all ( database)
46- . await ?
47- . into_iter ( )
48- . map ( DataProcessing :: from)
49- . collect ( ) )
83+ ) -> Result < Option < DataProcessing > , async_graphql:: Error > {
84+ let loader = ctx. data_unchecked :: < DataLoader < DataCollectionLoader > > ( ) ;
85+ Ok ( loader. load_one ( self . id ) . await ?)
5086 }
5187
5288 /// Fetched all the processing jobs
0 commit comments