mirror of
https://github.com/lldap/lldap.git
synced 2026-07-25 17:58:44 +00:00
server: extract graphql crate
This commit is contained in:
committed by
nitnelave
parent
db77a0f023
commit
d38a2cd08b
@@ -1,3 +0,0 @@
|
||||
pub mod api;
|
||||
pub mod mutation;
|
||||
pub mod query;
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,109 +1,18 @@
|
||||
use crate::infra::{
|
||||
auth_service::check_if_token_is_valid,
|
||||
cli::ExportGraphQLSchemaOpts,
|
||||
graphql::{mutation::Mutation, query::Query},
|
||||
tcp_server::AppState,
|
||||
};
|
||||
use crate::infra::{auth_service::check_if_token_is_valid, tcp_server::AppState};
|
||||
use actix_web::FromRequest;
|
||||
use actix_web::HttpMessage;
|
||||
use actix_web::{Error, HttpRequest, HttpResponse, error::JsonPayloadError, web};
|
||||
use actix_web_httpauth::extractors::bearer::BearerAuth;
|
||||
use juniper::{
|
||||
EmptySubscription, FieldError, RootNode, ScalarValue,
|
||||
ScalarValue,
|
||||
http::{
|
||||
GraphQLBatchRequest, GraphQLRequest, graphiql::graphiql_source,
|
||||
playground::playground_source,
|
||||
},
|
||||
};
|
||||
use lldap_access_control::{
|
||||
AccessControlledBackendHandler, AdminBackendHandler, ReadonlyBackendHandler,
|
||||
UserReadableBackendHandler, UserWriteableBackendHandler,
|
||||
};
|
||||
use lldap_auth::{access_control::ValidationResults, types::UserId};
|
||||
use lldap_domain_handlers::handler::BackendHandler;
|
||||
use tracing::debug;
|
||||
|
||||
pub struct Context<Handler: BackendHandler> {
|
||||
pub handler: AccessControlledBackendHandler<Handler>,
|
||||
pub validation_result: ValidationResults,
|
||||
}
|
||||
|
||||
pub fn field_error_callback<'a>(
|
||||
span: &'a tracing::Span,
|
||||
error_message: &'a str,
|
||||
) -> impl 'a + FnOnce() -> FieldError {
|
||||
move || {
|
||||
span.in_scope(|| debug!("Unauthorized"));
|
||||
FieldError::from(error_message)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Handler: BackendHandler> Context<Handler> {
|
||||
#[cfg(test)]
|
||||
pub fn new_for_tests(handler: Handler, validation_result: ValidationResults) -> Self {
|
||||
Self {
|
||||
handler: AccessControlledBackendHandler::new(handler),
|
||||
validation_result,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_admin_handler(&self) -> Option<&(impl AdminBackendHandler + use<Handler>)> {
|
||||
self.handler.get_admin_handler(&self.validation_result)
|
||||
}
|
||||
|
||||
pub fn get_readonly_handler(&self) -> Option<&(impl ReadonlyBackendHandler + use<Handler>)> {
|
||||
self.handler.get_readonly_handler(&self.validation_result)
|
||||
}
|
||||
|
||||
pub fn get_writeable_handler(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
) -> Option<&(impl UserWriteableBackendHandler + use<Handler>)> {
|
||||
self.handler
|
||||
.get_writeable_handler(&self.validation_result, user_id)
|
||||
}
|
||||
|
||||
pub fn get_readable_handler(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
) -> Option<&(impl UserReadableBackendHandler + use<Handler>)> {
|
||||
self.handler
|
||||
.get_readable_handler(&self.validation_result, user_id)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Handler: BackendHandler> juniper::Context for Context<Handler> {}
|
||||
|
||||
type Schema<Handler> =
|
||||
RootNode<'static, Query<Handler>, Mutation<Handler>, EmptySubscription<Context<Handler>>>;
|
||||
|
||||
fn schema<Handler: BackendHandler>() -> Schema<Handler> {
|
||||
Schema::new(
|
||||
Query::<Handler>::new(),
|
||||
Mutation::<Handler>::new(),
|
||||
EmptySubscription::<Context<Handler>>::new(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn export_schema(opts: ExportGraphQLSchemaOpts) -> anyhow::Result<()> {
|
||||
use anyhow::Context;
|
||||
use lldap_sql_backend_handler::SqlBackendHandler;
|
||||
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(())
|
||||
}
|
||||
use lldap_graphql_server::api::Context;
|
||||
use lldap_graphql_server::api::schema;
|
||||
|
||||
async fn graphiql_route() -> Result<HttpResponse, Error> {
|
||||
let html = graphiql_source("/api/graphql", None);
|
||||
@@ -3,7 +3,7 @@ pub mod cli;
|
||||
pub mod configuration;
|
||||
pub mod database_string;
|
||||
pub mod db_cleaner;
|
||||
pub mod graphql;
|
||||
pub mod graphql_server;
|
||||
pub mod healthcheck;
|
||||
pub mod jwt_sql_tables;
|
||||
pub mod ldap_server;
|
||||
|
||||
@@ -145,7 +145,7 @@ fn http_config<Backend>(
|
||||
.service(
|
||||
web::scope("/api")
|
||||
.wrap(auth_service::CookieToHeaderTranslatorFactory)
|
||||
.configure(super::graphql::api::configure_endpoint::<Backend>),
|
||||
.configure(crate::infra::graphql_server::configure_endpoint::<Backend>),
|
||||
)
|
||||
.service(
|
||||
web::resource("/pkg/lldap_app_bg.wasm.gz")
|
||||
|
||||
+3
-1
@@ -274,7 +274,9 @@ async fn create_schema_command(opts: RunOpts) -> Result<()> {
|
||||
async fn main() -> Result<()> {
|
||||
let cli_opts = infra::cli::init();
|
||||
match cli_opts.command {
|
||||
Command::ExportGraphQLSchema(opts) => infra::graphql::api::export_schema(opts),
|
||||
Command::ExportGraphQLSchema(opts) => {
|
||||
lldap_graphql_server::api::export_schema(opts.output_file)
|
||||
}
|
||||
Command::Run(opts) => run_server_command(opts).await,
|
||||
Command::HealthCheck(opts) => run_healthcheck(opts).await,
|
||||
Command::SendTestEmail(opts) => send_test_email_command(opts).await,
|
||||
|
||||
Reference in New Issue
Block a user