From 653e71badb50a43924ce8b82429df086ab959f99 Mon Sep 17 00:00:00 2001 From: Garry O'Donnell Date: Fri, 15 Mar 2024 14:20:01 +0000 Subject: [PATCH] Add basic resolvers for session and sessions --- Cargo.lock | 4 +++ Cargo.toml | 5 +++- sessions/Cargo.toml | 2 ++ sessions/src/graphql.rs | 60 ++++++++++++++++++++++++++++++++++++++--- 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 57446c1..26f8e21 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -160,6 +160,7 @@ dependencies = [ "async-trait", "base64", "bytes", + "chrono", "fnv", "futures-util", "handlebars", @@ -563,8 +564,10 @@ checksum = "8eaf5903dcbc0a39312feb77df2ff4c76387d591b9fc7b04a238dcf8bb62639a" dependencies = [ "android-tzdata", "iana-time-zone", + "js-sys", "num-traits", "serde", + "wasm-bindgen", "windows-targets 0.52.4", ] @@ -2468,6 +2471,7 @@ dependencies = [ "async-graphql-axum", "axum 0.7.4", "built", + "chrono", "clap", "dotenvy", "models", diff --git a/Cargo.toml b/Cargo.toml index 5eb12ca..7928a68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,7 @@ members = ["models", "sessions"] resolver = "2" [workspace.dependencies] -sea-orm = { version = "0.12.14", features = ["sqlx-mysql"] } +sea-orm = { version = "0.12.14", features = [ + "runtime-tokio-rustls", + "sqlx-mysql", +] } diff --git a/sessions/Cargo.toml b/sessions/Cargo.toml index 4e722a8..f537e20 100644 --- a/sessions/Cargo.toml +++ b/sessions/Cargo.toml @@ -6,11 +6,13 @@ edition = "2021" [dependencies] anyhow = "1.0.81" async-graphql = { version = "7.0.2", default-features = false, features = [ + "chrono", "graphiql", "tracing", ] } async-graphql-axum = { version = "7.0.2" } axum = { version = "0.7.4", features = ["ws"] } +chrono = { version = "0.4.35" } clap = { version = "4.5.2", features = ["derive", "env"] } dotenvy = { version = "0.15.7" } models = { path = "../models" } diff --git a/sessions/src/graphql.rs b/sessions/src/graphql.rs index f66f7c6..8f6e9de 100644 --- a/sessions/src/graphql.rs +++ b/sessions/src/graphql.rs @@ -1,4 +1,9 @@ -use async_graphql::{EmptyMutation, EmptySubscription, MergedObject, Schema, SchemaBuilder}; +use async_graphql::{ + Context, EmptyMutation, EmptySubscription, Object, Schema, SchemaBuilder, SimpleObject, +}; +use chrono::{DateTime, Utc}; +use models::bl_session; +use sea_orm::{DatabaseConnection, EntityTrait}; /// The GraphQL schema exposed by the service pub type RootSchema = Schema; @@ -8,8 +13,57 @@ pub fn root_schema_builder() -> SchemaBuilder, + /// The date and time at which the Session began + start: Option>, + /// The date and time at which the Session ended + end: Option>, +} + +impl From for Session { + fn from(value: bl_session::Model) -> Self { + Self { + session_id: value.session_id, + visit_number: value.visit_number, + start: value.start_date.map(|date| date.and_utc()), + end: value.end_date.map(|date| date.and_utc()), + } + } +} + /// The root query of the service -#[derive(Debug, Clone, MergedObject, Default)] +#[derive(Debug, Clone, Default)] pub struct RootQuery; -impl RootQuery {} +#[Object] +impl RootQuery { + /// Retrieves all Beamline Sessions + async fn sessions(&self, ctx: &Context<'_>) -> Result, async_graphql::Error> { + let database = ctx.data::()?; + Ok(bl_session::Entity::find() + .all(database) + .await? + .into_iter() + .map(Session::from) + .collect()) + } + + /// Retrieves a Beamline Session + async fn session( + &self, + ctx: &Context<'_>, + session_id: u32, + ) -> Result, async_graphql::Error> { + let database = ctx.data::()?; + Ok(bl_session::Entity::find_by_id(session_id) + .one(database) + .await? + .map(Session::from)) + } +}