From cf68386cb189e4301559cc29257068e6b305f016 Mon Sep 17 00:00:00 2001 From: iamvigneshwars Date: Wed, 20 Mar 2024 12:53:23 +0000 Subject: [PATCH] Update types and resolvers for federation --- .github/workflows/code.yml | 1 + .github/workflows/policy.yml | 75 ---------------------------------- .github/workflows/schema.yml | 4 +- datasets/src/graphql/entity.rs | 20 ++++----- datasets/src/graphql/mod.rs | 42 +++++++++---------- datasets/src/main.rs | 1 + 6 files changed, 33 insertions(+), 110 deletions(-) delete mode 100644 .github/workflows/policy.yml diff --git a/.github/workflows/code.yml b/.github/workflows/code.yml index 8f21e7f..95e0a2e 100644 --- a/.github/workflows/code.yml +++ b/.github/workflows/code.yml @@ -59,6 +59,7 @@ jobs: --no-deps --all-targets --all-features + -- --deny warnings test: diff --git a/.github/workflows/policy.yml b/.github/workflows/policy.yml deleted file mode 100644 index d582ca3..0000000 --- a/.github/workflows/policy.yml +++ /dev/null @@ -1,75 +0,0 @@ -name: Policy - -on: - push: - pull_request: - -jobs: - lint: - # Deduplicate jobs from pull requests and branch pushes within the same repo. - if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository - runs-on: ubuntu-latest - steps: - - name: Checkout source - uses: actions/checkout@v4.1.2 - - - name: Setup Regal - uses: StyraInc/setup-regal@v1.0.0 - with: - version: latest - - - name: Lint - run: regal lint --format github ./policy - - test: - # Deduplicate jobs from pull requests and branch pushes within the same repo. - if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository - runs-on: ubuntu-latest - steps: - - name: Checkout source - uses: actions/checkout@v4.1.2 - - - name: Setup OPA - uses: open-policy-agent/setup-opa@v2.2.0 - with: - version: latest - - - name: Test - run: opa test ./policy -v - - build_bundle: - needs: - - lint - - test - # Deduplicate jobs from pull requests and branch pushes within the same repo. - if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - steps: - - name: Checkout source - uses: actions/checkout@v4.1.2 - - - name: Generate Image Name - run: echo IMAGE_REPOSITORY=ghcr.io/$(echo "${{ github.repository }}-policy" | tr '[:upper:]' '[:lower:]' | tr '[_]' '[\-]') >> $GITHUB_ENV - - - name: Log in to GitHub Docker Registry - uses: docker/login-action@v3.1.0 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Setup OPA - uses: open-policy-agent/setup-opa@v2.2.0 - with: - version: latest - - - name: Build OPA Policy # If this is a tag, use it as a revision string - run: opa build -b policy -r ${{ github.ref_name }} --ignore *_test.rego - - - name: Publish OPA Bundle - if: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags') }} - run: oras push ${{ env.IMAGE_REPOSITORY }}:${{ github.ref_name }} bundle.tar.gz:application/vnd.oci.image.layer.v1.tar+gzip - diff --git a/.github/workflows/schema.yml b/.github/workflows/schema.yml index 47cde04..7cf1c48 100644 --- a/.github/workflows/schema.yml +++ b/.github/workflows/schema.yml @@ -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: @@ -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@current --routing-url http://datasets:80 --schema datasets.graphql --name datasets diff --git a/datasets/src/graphql/entity.rs b/datasets/src/graphql/entity.rs index a1b19ab..4f1b3ba 100644 --- a/datasets/src/graphql/entity.rs +++ b/datasets/src/graphql/entity.rs @@ -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, /// The date time and which data collection began pub start_time: Option>, /// The date time and which data collection ended @@ -36,25 +35,26 @@ pub struct DataCollection { pub data_collection_group_id: i32, /// An opaque unique identifier for the detector pub detector_id: Option, - /// Location of the image stored - pub image_directory: Option, + /// Location of the image stored + pub image_directory: Option, /// Image file name without extension pub image_suffix: Option, /// Image file extension - pub image_prefix: Option, + pub image_prefix: Option, } +/// 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 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, @@ -69,7 +69,7 @@ impl From 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, } } diff --git a/datasets/src/graphql/mod.rs b/datasets/src/graphql/mod.rs index b0d8349..1f12251 100644 --- a/datasets/src/graphql/mod.rs +++ b/datasets/src/graphql/mod.rs @@ -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; +pub type RootSchema = Schema; /// A schema builder for the service -pub fn root_schema_builder() -> SchemaBuilder { - Schema::build(RootQuery, EmptyMutation, EmptySubscription).enable_federation() +pub fn root_schema_builder() -> SchemaBuilder { + 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> { +impl Session { + /// Fetches all the data collected during a session + async fn datasets(&self, ctx: &Context<'_>) -> async_graphql::Result> { let database = ctx.data::()?; 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, @@ -63,6 +63,7 @@ impl RootQuery { .map(DataCollection::from)) } + /// Reference dataset resolver for the router #[graphql(entity)] async fn router_dataset( &self, @@ -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 } } - } diff --git a/datasets/src/main.rs b/datasets/src/main.rs index ae5dec2..68b83d7 100644 --- a/datasets/src/main.rs +++ b/datasets/src/main.rs @@ -53,6 +53,7 @@ struct ServeArgs { #[derive(Debug, Parser)] struct SchemaArgs { /// The path to write the schema to, if not set the schema will be printed to stdout + #[arg(short, long)] path: Option, }