Skip to content

Commit

Permalink
Update types and resolvers for federation
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvigneshwars committed Mar 20, 2024
1 parent 1e8da8f commit df0f1ae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
17 changes: 7 additions & 10 deletions datasets/src/graphql/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ use chrono::{DateTime, Utc};
use models::data_collection;

#[derive(Clone, Debug, PartialEq, SimpleObject)]
#[graphql(name = "datasets")]
#[graphql(name = "Datasets")]
pub struct DataCollection {
/// An opaque unique identifier for the data collection
pub data_collection_id: u32,
/// An opaque unique identifier for the session
pub sessionid: Option<u32>,
/// The date time and which data collection began
pub start_time: Option<DateTime<Utc>>,
/// The date time and which data collection ended
Expand All @@ -36,25 +34,24 @@ pub struct DataCollection {
pub data_collection_group_id: i32,
/// An opaque unique identifier for the detector
pub detector_id: Option<i32>,
/// Location of the image stored
pub image_directory: Option<String>,
/// Location of the image stored
pub image_directory: Option<String>,
/// Image file name without extension
pub image_suffix: Option<String>,
/// Image file extension
pub image_prefix: Option<String>,
pub image_prefix: Option<String>,
}

#[derive(SimpleObject)]
#[graphql(name = "sessions", complex)]
#[graphql(name = "Session", complex)]
pub struct Session {
pub session_id: i32,
pub id: i32,
}

impl From<data_collection::Model> for DataCollection {
fn from(values: data_collection::Model) -> Self {
Self {
data_collection_id: values.data_collection_id,
sessionid: values.sessionid,
start_time: values.start_time.map(|time| time.and_utc()),
end_time: values.end_time.map(|time| time.and_utc()),
number_of_images: values.number_of_images,
Expand All @@ -69,7 +66,7 @@ impl From<data_collection::Model> for DataCollection {
data_collection_group_id: values.data_collection_group_id,
detector_id: values.detector_id,
image_directory: values.image_directory,
image_suffix: values.image_suffix,
image_suffix: values.image_suffix,
image_prefix: values.image_prefix,
}
}
Expand Down
38 changes: 15 additions & 23 deletions datasets/src/graphql/mod.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
mod entity;

use async_graphql::{ComplexObject, Context, EmptyMutation, EmptySubscription, Object, Schema, SchemaBuilder};
use async_graphql::{
ComplexObject, Context, EmptyMutation, EmptySubscription, Object, Schema, SchemaBuilder,
};
use entity::{DataCollection, Session};
use models::data_collection;
use sea_orm::{DatabaseConnection, EntityTrait, QueryFilter, ColumnTrait};
use sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter};

/// The GraphQL schema exposed by the service
pub type RootSchema = Schema<RootQuery, EmptyMutation, EmptySubscription>;
pub type RootSchema = Schema<Query, EmptyMutation, EmptySubscription>;

/// A schema builder for the service
pub fn root_schema_builder() -> SchemaBuilder<RootQuery, EmptyMutation, EmptySubscription> {
Schema::build(RootQuery, EmptyMutation, EmptySubscription).enable_federation()
pub fn root_schema_builder() -> SchemaBuilder<Query, EmptyMutation, EmptySubscription> {
Schema::build(Query, EmptyMutation, EmptySubscription).enable_federation()
}

/// The root query of the service
#[derive(Debug, Clone, Default)]
pub struct RootQuery;
pub struct Query;

#[ComplexObject]
impl Session{
async fn datasets(
&self,
ctx: &Context<'_>,
) -> async_graphql::Result<Vec<DataCollection>> {
impl Session {
async fn datasets(&self, ctx: &Context<'_>) -> async_graphql::Result<Vec<DataCollection>> {
let database = ctx.data::<DatabaseConnection>()?;
Ok(data_collection::Entity::find()
.filter(data_collection::Column::Sessionid.eq(self.session_id))
.filter(data_collection::Column::Sessionid.eq(self.id))
.all(database)
.await?
.into_iter()
.map(DataCollection::from)
.collect()
)
.collect())
}
}

#[Object]
impl RootQuery {
impl Query {
/// Retrieves all datasets collected during Sessions
async fn datasets(
&self,
Expand Down Expand Up @@ -73,13 +71,7 @@ impl RootQuery {
}

#[graphql(entity)]
async fn router_sessions(
&self,
session_id: i32
) -> Session {
Session {
session_id,
}
async fn router_sessions(&self, id: i32) -> Session {
Session { id }
}

}

0 comments on commit df0f1ae

Please sign in to comment.