Move backend source to server/ subpackage
To clarify the organization.
This commit is contained in:
committed by
nitnelave
parent
3eb53ba5bf
commit
d8df47b35d
100
server/src/infra/graphql/api.rs
Normal file
100
server/src/infra/graphql/api.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
use crate::{
|
||||
domain::handler::BackendHandler,
|
||||
infra::{
|
||||
auth_service::{check_if_token_is_valid, ValidationResults},
|
||||
cli::ExportGraphQLSchemaOpts,
|
||||
tcp_server::AppState,
|
||||
},
|
||||
};
|
||||
use actix_web::{web, Error, HttpResponse};
|
||||
use actix_web_httpauth::extractors::bearer::BearerAuth;
|
||||
use juniper::{EmptySubscription, RootNode};
|
||||
use juniper_actix::{graphiql_handler, graphql_handler, playground_handler};
|
||||
|
||||
use super::{mutation::Mutation, query::Query};
|
||||
|
||||
pub struct Context<Handler: BackendHandler> {
|
||||
pub handler: Box<Handler>,
|
||||
pub validation_result: ValidationResults,
|
||||
}
|
||||
|
||||
impl<Handler: BackendHandler> juniper::Context for Context<Handler> {}
|
||||
|
||||
type Schema<Handler> =
|
||||
RootNode<'static, Query<Handler>, Mutation<Handler>, EmptySubscription<Context<Handler>>>;
|
||||
|
||||
fn schema<Handler: BackendHandler + Sync>() -> Schema<Handler> {
|
||||
Schema::new(
|
||||
Query::<Handler>::new(),
|
||||
Mutation::<Handler>::new(),
|
||||
EmptySubscription::<Context<Handler>>::new(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn export_schema(opts: ExportGraphQLSchemaOpts) -> anyhow::Result<()> {
|
||||
use crate::domain::sql_backend_handler::SqlBackendHandler;
|
||||
use anyhow::Context;
|
||||
let output = schema::<SqlBackendHandler>().as_schema_language();
|
||||
match opts.output_file {
|
||||
None => println!("{}", output),
|
||||
Some(path) => {
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path::Path;
|
||||
let path = Path::new(&path);
|
||||
let mut file =
|
||||
File::create(&path).context(format!("unable to open '{}'", path.display()))?;
|
||||
file.write_all(output.as_bytes())
|
||||
.context(format!("unable to write in '{}'", path.display()))?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn graphiql_route() -> Result<HttpResponse, Error> {
|
||||
graphiql_handler("/api/graphql", None).await
|
||||
}
|
||||
async fn playground_route() -> Result<HttpResponse, Error> {
|
||||
playground_handler("/api/graphql", None).await
|
||||
}
|
||||
|
||||
async fn graphql_route<Handler: BackendHandler + Sync>(
|
||||
req: actix_web::HttpRequest,
|
||||
mut payload: actix_web::web::Payload,
|
||||
data: web::Data<AppState<Handler>>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
use actix_web::FromRequest;
|
||||
let bearer = BearerAuth::from_request(&req, &mut payload.0).await?;
|
||||
let validation_result = check_if_token_is_valid(&data, bearer.token())?;
|
||||
let context = Context::<Handler> {
|
||||
handler: Box::new(data.backend_handler.clone()),
|
||||
validation_result,
|
||||
};
|
||||
graphql_handler(&schema(), &context, req, payload).await
|
||||
}
|
||||
|
||||
pub fn configure_endpoint<Backend>(cfg: &mut web::ServiceConfig)
|
||||
where
|
||||
Backend: BackendHandler + Sync + 'static,
|
||||
{
|
||||
let json_config = web::JsonConfig::default()
|
||||
.limit(4096)
|
||||
.error_handler(|err, _req| {
|
||||
// create custom error response
|
||||
log::error!("API error: {}", err);
|
||||
let msg = err.to_string();
|
||||
actix_web::error::InternalError::from_response(
|
||||
err,
|
||||
HttpResponse::BadRequest().body(msg),
|
||||
)
|
||||
.into()
|
||||
});
|
||||
cfg.app_data(json_config);
|
||||
cfg.service(
|
||||
web::resource("/graphql")
|
||||
.route(web::post().to(graphql_route::<Backend>))
|
||||
.route(web::get().to(graphql_route::<Backend>)),
|
||||
);
|
||||
cfg.service(web::resource("/graphql/playground").route(web::get().to(playground_route)));
|
||||
cfg.service(web::resource("/graphql/graphiql").route(web::get().to(graphiql_route)));
|
||||
}
|
||||
3
server/src/infra/graphql/mod.rs
Normal file
3
server/src/infra/graphql/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod api;
|
||||
pub mod mutation;
|
||||
pub mod query;
|
||||
55
server/src/infra/graphql/mutation.rs
Normal file
55
server/src/infra/graphql/mutation.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use crate::domain::handler::{BackendHandler, CreateUserRequest};
|
||||
use juniper::{graphql_object, FieldResult, GraphQLInputObject};
|
||||
|
||||
use super::api::Context;
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
/// The top-level GraphQL mutation type.
|
||||
pub struct Mutation<Handler: BackendHandler> {
|
||||
_phantom: std::marker::PhantomData<Box<Handler>>,
|
||||
}
|
||||
|
||||
impl<Handler: BackendHandler> Mutation<Handler> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
_phantom: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, GraphQLInputObject)]
|
||||
/// The details required to create a user.
|
||||
pub struct UserInput {
|
||||
id: String,
|
||||
email: String,
|
||||
display_name: Option<String>,
|
||||
first_name: Option<String>,
|
||||
last_name: Option<String>,
|
||||
}
|
||||
|
||||
#[graphql_object(context = Context<Handler>)]
|
||||
impl<Handler: BackendHandler + Sync> Mutation<Handler> {
|
||||
async fn create_user(
|
||||
context: &Context<Handler>,
|
||||
user: UserInput,
|
||||
) -> FieldResult<super::query::User<Handler>> {
|
||||
if !context.validation_result.is_admin {
|
||||
return Err("Unauthorized user creation".into());
|
||||
}
|
||||
context
|
||||
.handler
|
||||
.create_user(CreateUserRequest {
|
||||
user_id: user.id.clone(),
|
||||
email: user.email,
|
||||
display_name: user.display_name,
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
})
|
||||
.await?;
|
||||
Ok(context
|
||||
.handler
|
||||
.get_user_details(&user.id)
|
||||
.await
|
||||
.map(Into::into)?)
|
||||
}
|
||||
}
|
||||
348
server/src/infra/graphql/query.rs
Normal file
348
server/src/infra/graphql/query.rs
Normal file
@@ -0,0 +1,348 @@
|
||||
use crate::domain::handler::BackendHandler;
|
||||
use juniper::{graphql_object, FieldResult, GraphQLInputObject};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::convert::TryInto;
|
||||
|
||||
type DomainRequestFilter = crate::domain::handler::RequestFilter;
|
||||
type DomainUser = crate::domain::handler::User;
|
||||
use super::api::Context;
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, GraphQLInputObject)]
|
||||
/// A filter for requests, specifying a boolean expression based on field constraints. Only one of
|
||||
/// the fields can be set at a time.
|
||||
pub struct RequestFilter {
|
||||
any: Option<Vec<RequestFilter>>,
|
||||
all: Option<Vec<RequestFilter>>,
|
||||
not: Option<Box<RequestFilter>>,
|
||||
eq: Option<EqualityConstraint>,
|
||||
}
|
||||
|
||||
impl TryInto<DomainRequestFilter> for RequestFilter {
|
||||
type Error = String;
|
||||
fn try_into(self) -> Result<DomainRequestFilter, Self::Error> {
|
||||
let mut field_count = 0;
|
||||
if self.any.is_some() {
|
||||
field_count += 1;
|
||||
}
|
||||
if self.all.is_some() {
|
||||
field_count += 1;
|
||||
}
|
||||
if self.not.is_some() {
|
||||
field_count += 1;
|
||||
}
|
||||
if self.eq.is_some() {
|
||||
field_count += 1;
|
||||
}
|
||||
if field_count == 0 {
|
||||
return Err("No field specified in request filter".to_string());
|
||||
}
|
||||
if field_count > 1 {
|
||||
return Err("Multiple fields specified in request filter".to_string());
|
||||
}
|
||||
if let Some(e) = self.eq {
|
||||
return Ok(DomainRequestFilter::Equality(e.field, e.value));
|
||||
}
|
||||
if let Some(c) = self.any {
|
||||
return Ok(DomainRequestFilter::Or(
|
||||
c.into_iter()
|
||||
.map(TryInto::try_into)
|
||||
.collect::<Result<Vec<_>, String>>()?,
|
||||
));
|
||||
}
|
||||
if let Some(c) = self.all {
|
||||
return Ok(DomainRequestFilter::And(
|
||||
c.into_iter()
|
||||
.map(TryInto::try_into)
|
||||
.collect::<Result<Vec<_>, String>>()?,
|
||||
));
|
||||
}
|
||||
if let Some(c) = self.not {
|
||||
return Ok(DomainRequestFilter::Not(Box::new((*c).try_into()?)));
|
||||
}
|
||||
unreachable!();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, GraphQLInputObject)]
|
||||
pub struct EqualityConstraint {
|
||||
field: String,
|
||||
value: String,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
/// The top-level GraphQL query type.
|
||||
pub struct Query<Handler: BackendHandler> {
|
||||
_phantom: std::marker::PhantomData<Box<Handler>>,
|
||||
}
|
||||
|
||||
impl<Handler: BackendHandler> Query<Handler> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
_phantom: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[graphql_object(context = Context<Handler>)]
|
||||
impl<Handler: BackendHandler + Sync> Query<Handler> {
|
||||
fn api_version() -> &'static str {
|
||||
"1.0"
|
||||
}
|
||||
|
||||
pub async fn user(context: &Context<Handler>, user_id: String) -> FieldResult<User<Handler>> {
|
||||
if !context.validation_result.can_access(&user_id) {
|
||||
return Err("Unauthorized access to user data".into());
|
||||
}
|
||||
Ok(context
|
||||
.handler
|
||||
.get_user_details(&user_id)
|
||||
.await
|
||||
.map(Into::into)?)
|
||||
}
|
||||
|
||||
async fn users(
|
||||
context: &Context<Handler>,
|
||||
#[graphql(name = "where")] filters: Option<RequestFilter>,
|
||||
) -> FieldResult<Vec<User<Handler>>> {
|
||||
if !context.validation_result.is_admin {
|
||||
return Err("Unauthorized access to user list".into());
|
||||
}
|
||||
Ok(context
|
||||
.handler
|
||||
.list_users(filters.map(TryInto::try_into).transpose()?)
|
||||
.await
|
||||
.map(|v| v.into_iter().map(Into::into).collect())?)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
|
||||
/// Represents a single user.
|
||||
pub struct User<Handler: BackendHandler> {
|
||||
user: DomainUser,
|
||||
_phantom: std::marker::PhantomData<Box<Handler>>,
|
||||
}
|
||||
|
||||
impl<Handler: BackendHandler> Default for User<Handler> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
user: DomainUser::default(),
|
||||
_phantom: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[graphql_object(context = Context<Handler>)]
|
||||
impl<Handler: BackendHandler + Sync> User<Handler> {
|
||||
fn id(&self) -> &str {
|
||||
&self.user.user_id
|
||||
}
|
||||
|
||||
fn email(&self) -> &str {
|
||||
&self.user.email
|
||||
}
|
||||
|
||||
fn display_name(&self) -> Option<&String> {
|
||||
self.user.display_name.as_ref()
|
||||
}
|
||||
|
||||
fn first_name(&self) -> Option<&String> {
|
||||
self.user.first_name.as_ref()
|
||||
}
|
||||
|
||||
fn last_name(&self) -> Option<&String> {
|
||||
self.user.last_name.as_ref()
|
||||
}
|
||||
|
||||
fn creation_date(&self) -> chrono::DateTime<chrono::Utc> {
|
||||
self.user.creation_date
|
||||
}
|
||||
|
||||
/// The groups to which this user belongs.
|
||||
async fn groups(&self, context: &Context<Handler>) -> FieldResult<Vec<Group<Handler>>> {
|
||||
Ok(context
|
||||
.handler
|
||||
.get_user_groups(&self.user.user_id)
|
||||
.await
|
||||
.map(|set| set.into_iter().map(Into::into).collect())?)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Handler: BackendHandler> From<DomainUser> for User<Handler> {
|
||||
fn from(user: DomainUser) -> Self {
|
||||
Self {
|
||||
user,
|
||||
_phantom: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
|
||||
/// Represents a single group.
|
||||
pub struct Group<Handler: BackendHandler> {
|
||||
group_id: String,
|
||||
_phantom: std::marker::PhantomData<Box<Handler>>,
|
||||
}
|
||||
|
||||
#[graphql_object(context = Context<Handler>)]
|
||||
impl<Handler: BackendHandler + Sync> Group<Handler> {
|
||||
fn id(&self) -> String {
|
||||
self.group_id.clone()
|
||||
}
|
||||
/// The groups to which this user belongs.
|
||||
async fn users(&self, context: &Context<Handler>) -> FieldResult<Vec<User<Handler>>> {
|
||||
if !context.validation_result.is_admin {
|
||||
return Err("Unauthorized access to group data".into());
|
||||
}
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Handler: BackendHandler> From<String> for Group<Handler> {
|
||||
fn from(group_id: String) -> Self {
|
||||
Self {
|
||||
group_id,
|
||||
_phantom: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{domain::handler::MockTestBackendHandler, infra::auth_service::ValidationResults};
|
||||
use juniper::{
|
||||
execute, graphql_value, DefaultScalarValue, EmptyMutation, EmptySubscription, GraphQLType,
|
||||
RootNode, Variables,
|
||||
};
|
||||
use mockall::predicate::eq;
|
||||
use std::collections::HashSet;
|
||||
|
||||
fn schema<'q, C, Q>(query_root: Q) -> RootNode<'q, Q, EmptyMutation<C>, EmptySubscription<C>>
|
||||
where
|
||||
Q: GraphQLType<DefaultScalarValue, Context = C, TypeInfo = ()> + 'q,
|
||||
{
|
||||
RootNode::new(
|
||||
query_root,
|
||||
EmptyMutation::<C>::new(),
|
||||
EmptySubscription::<C>::new(),
|
||||
)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_user_by_id() {
|
||||
const QUERY: &str = r#"{
|
||||
user(userId: "bob") {
|
||||
id
|
||||
email
|
||||
groups {
|
||||
id
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
let mut mock = MockTestBackendHandler::new();
|
||||
mock.expect_get_user_details()
|
||||
.with(eq("bob"))
|
||||
.return_once(|_| {
|
||||
Ok(DomainUser {
|
||||
user_id: "bob".to_string(),
|
||||
email: "bob@bobbers.on".to_string(),
|
||||
..Default::default()
|
||||
})
|
||||
});
|
||||
let mut groups = HashSet::<String>::new();
|
||||
groups.insert("Bobbersons".to_string());
|
||||
mock.expect_get_user_groups()
|
||||
.with(eq("bob"))
|
||||
.return_once(|_| Ok(groups));
|
||||
|
||||
let context = Context::<MockTestBackendHandler> {
|
||||
handler: Box::new(mock),
|
||||
validation_result: ValidationResults::admin(),
|
||||
};
|
||||
|
||||
let schema = schema(Query::<MockTestBackendHandler>::new());
|
||||
assert_eq!(
|
||||
execute(QUERY, None, &schema, &Variables::new(), &context).await,
|
||||
Ok((
|
||||
graphql_value!(
|
||||
{
|
||||
"user": {
|
||||
"id": "bob",
|
||||
"email": "bob@bobbers.on",
|
||||
"groups": [{"id": "Bobbersons"}]
|
||||
}
|
||||
}),
|
||||
vec![]
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn list_users() {
|
||||
const QUERY: &str = r#"{
|
||||
users(filters: {
|
||||
any: [
|
||||
{eq: {
|
||||
field: "id"
|
||||
value: "bob"
|
||||
}},
|
||||
{eq: {
|
||||
field: "email"
|
||||
value: "robert@bobbers.on"
|
||||
}}
|
||||
]}) {
|
||||
id
|
||||
email
|
||||
}
|
||||
}"#;
|
||||
|
||||
let mut mock = MockTestBackendHandler::new();
|
||||
use crate::domain::handler::RequestFilter;
|
||||
mock.expect_list_users()
|
||||
.with(eq(Some(RequestFilter::Or(vec![
|
||||
RequestFilter::Equality("id".to_string(), "bob".to_string()),
|
||||
RequestFilter::Equality("email".to_string(), "robert@bobbers.on".to_string()),
|
||||
]))))
|
||||
.return_once(|_| {
|
||||
Ok(vec![
|
||||
DomainUser {
|
||||
user_id: "bob".to_string(),
|
||||
email: "bob@bobbers.on".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
DomainUser {
|
||||
user_id: "robert".to_string(),
|
||||
email: "robert@bobbers.on".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
])
|
||||
});
|
||||
|
||||
let context = Context::<MockTestBackendHandler> {
|
||||
handler: Box::new(mock),
|
||||
validation_result: ValidationResults::admin(),
|
||||
};
|
||||
|
||||
let schema = schema(Query::<MockTestBackendHandler>::new());
|
||||
assert_eq!(
|
||||
execute(QUERY, None, &schema, &Variables::new(), &context).await,
|
||||
Ok((
|
||||
graphql_value!(
|
||||
{
|
||||
"users": [
|
||||
{
|
||||
"id": "bob",
|
||||
"email": "bob@bobbers.on"
|
||||
},
|
||||
{
|
||||
"id": "robert",
|
||||
"email": "robert@bobbers.on"
|
||||
},
|
||||
]
|
||||
}),
|
||||
vec![]
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user