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 cabb7a5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 111 deletions.
1 change: 1 addition & 0 deletions .github/workflows/code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ jobs:
--no-deps
--all-targets
--all-features
--
--deny warnings
test:
Expand Down
75 changes: 0 additions & 75 deletions .github/workflows/policy.yml

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
command: run
args: >
schema
--path datasets.graphql
-- datasets.graphql
- name: Upload Schema Artifact
uses: actions/upload-artifact@v4.3.1
Expand Down Expand Up @@ -67,7 +67,7 @@ jobs:

- name: Check Subgraph Schema
run: >
rover subgraph check data-gateway@current
rover subgraph check data-gateway-n63jcf@current
--schema datasets.graphql
--name datasets
env:
Expand All @@ -76,7 +76,7 @@ jobs:
- name: Publish Subgraph Schema to Apollo Studio
if: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags') }}
run: >
rover subgraph publish data-gateway@current
rover subgraph publish data-gateway-n63jcf-gateway@current
--routing-url http://datasets:80
--schema datasets.graphql
--name datasets
Expand Down
20 changes: 10 additions & 10 deletions datasets/src/graphql/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ use chrono::{DateTime, Utc};

use models::data_collection;

/// Represents data collected during the sessions
#[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 +35,26 @@ 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>,
}

/// Extended subraph from session service/subgraph
#[derive(SimpleObject)]
#[graphql(name = "sessions", complex)]
#[graphql(name = "Session", complex)]
pub struct Session {
pub session_id: i32,
/// An opaque unique identifier for the sessions
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 +69,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
42 changes: 19 additions & 23 deletions datasets/src/graphql/mod.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
/// Collection of graphql entities
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 {
/// Fetches all the data collected during a 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 All @@ -63,6 +63,7 @@ impl RootQuery {
.map(DataCollection::from))
}

/// Reference dataset resolver for the router
#[graphql(entity)]
async fn router_dataset(
&self,
Expand All @@ -72,14 +73,9 @@ impl RootQuery {
self.dataset(ctx, data_collection_id).await
}

/// Reference sessions resolver for the router
#[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 cabb7a5

Please sign in to comment.