Skip to content

Commit

Permalink
Add basic resolvers for session and sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
garryod committed Mar 15, 2024
1 parent 350a3f4 commit 653e71b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
] }
2 changes: 2 additions & 0 deletions sessions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
60 changes: 57 additions & 3 deletions sessions/src/graphql.rs
Original file line number Diff line number Diff line change
@@ -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<RootQuery, EmptyMutation, EmptySubscription>;
Expand All @@ -8,8 +13,57 @@ pub fn root_schema_builder() -> SchemaBuilder<RootQuery, EmptyMutation, EmptySub
Schema::build(RootQuery, EmptyMutation, EmptySubscription)
}

/// A Beamline Session
#[derive(Debug, SimpleObject)]
struct Session {
/// An opaque unique identifier for the session
session_id: u32,
/// The number of session within the Proposal
visit_number: Option<u32>,
/// The date and time at which the Session began
start: Option<DateTime<Utc>>,
/// The date and time at which the Session ended
end: Option<DateTime<Utc>>,
}

impl From<bl_session::Model> 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<Vec<Session>, async_graphql::Error> {
let database = ctx.data::<DatabaseConnection>()?;
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<Option<Session>, async_graphql::Error> {
let database = ctx.data::<DatabaseConnection>()?;
Ok(bl_session::Entity::find_by_id(session_id)
.one(database)
.await?
.map(Session::from))
}
}

0 comments on commit 653e71b

Please sign in to comment.