Skip to main content

fraiseql_server/
lib.rs

1//! FraiseQL HTTP Server
2//!
3//! HTTP server for FraiseQL v2 compiled GraphQL execution engine.
4//!
5//! # Architecture
6//!
7//! The server exposes a GraphQL HTTP endpoint that:
8//! 1. Receives GraphQL queries via POST
9//! 2. Executes queries using the runtime Executor
10//! 3. Returns GraphQL-compliant JSON responses
11//!
12//! # Features
13//!
14//! - GraphQL endpoint (`/graphql`)
15//! - Health check endpoint (`/health`)
16//! - Schema introspection endpoint (`/introspection`)
17//! - CORS support
18//! - Compression (gzip, br, zstd)
19//! - Request tracing
20//! - APQ (Automatic Persisted Queries)
21//! - Query caching
22//! - Authentication middleware (optional)
23
24#![forbid(unsafe_code)]
25
26// CLI argument parsing (shared with fraiseql-cli) — requires `cli` feature
27#[cfg(feature = "cli")]
28pub mod cli;
29// API key authentication
30pub mod api_key;
31// Token revocation
32pub mod token_revocation;
33
34// Original fraiseql-server modules
35pub mod api;
36pub mod error;
37pub mod extractors;
38#[cfg(feature = "federation")]
39pub mod federation;
40pub mod logging;
41pub mod middleware;
42pub mod routes;
43pub mod schema;
44pub mod server;
45pub mod server_config;
46pub mod subscriptions;
47pub mod validation;
48
49// Renamed to avoid conflicts with runtime modules
50pub mod metrics_server;
51
52// fraiseql-runtime modules (merged)
53
54/// Runtime configuration types loaded from `fraiseql.toml` or environment variables.
55pub mod config;
56/// Resilience primitives: backpressure and retry policies.
57pub mod resilience;
58/// Utilities for distributed tracing, span propagation, and trace context formatting.
59#[cfg(feature = "federation")]
60pub mod tracing_utils;
61#[cfg(not(feature = "federation"))]
62pub mod tracing_utils {
63    //! Stub tracing utilities when federation is disabled.
64    use axum::http::HeaderMap;
65
66    /// Stub trace context extraction when federation is disabled.
67    #[allow(clippy::missing_const_for_fn)] // Reason: signature must match federation-enabled version which is not const
68    pub fn extract_trace_context(_headers: &HeaderMap) -> Option<()> {
69        None
70    }
71}
72
73// Webhooks (extracted to fraiseql-webhooks crate) — optional, enable with `features = ["webhooks"]`
74// Authentication (extracted to fraiseql-auth crate) — optional, enable with `features =
75// ["auth"]`
76#[cfg(feature = "auth")]
77pub use fraiseql_auth as auth;
78#[cfg(feature = "webhooks")]
79pub use fraiseql_webhooks as webhooks;
80
81/// Stub auth types compiled when the `auth` feature is disabled.
82///
83/// These zero-sized types allow internal code that references `crate::auth::*` to compile
84/// in no-auth builds without requiring every call-site to be cfg-gated.  All stub methods
85/// are pure stubs that the compiler will dead-code-eliminate.
86#[cfg(not(feature = "auth"))]
87pub mod auth {
88    use std::sync::Arc;
89
90    /// Stub for `fraiseql_auth::state_encryption::StateEncryptionService`.
91    pub mod state_encryption {
92        /// Zero-sized stub; never instantiated when `auth` feature is off.
93        pub struct StateEncryptionService;
94        impl StateEncryptionService {
95            /// Stub: returns `None`.
96            ///
97            /// # Errors
98            ///
99            /// Currently infallible — always returns `Ok(None)`.
100            /// Errors may be returned when the `auth` feature is enabled.
101            pub fn from_compiled_schema(
102                _s: &serde_json::Value,
103            ) -> crate::Result<Option<std::sync::Arc<Self>>> {
104                Ok(None)
105            }
106        }
107    }
108
109    /// Stub for `fraiseql_auth::PkceStateStore`.
110    pub struct PkceStateStore;
111    impl PkceStateStore {
112        /// Stub: always returns `true` (in-memory).
113        pub fn is_in_memory(&self) -> bool {
114            true
115        }
116
117        /// Stub: no-op.
118        pub async fn cleanup_expired(&self) {}
119    }
120
121    /// Stub for `fraiseql_auth::OidcServerClient`.
122    pub struct OidcServerClient;
123    impl OidcServerClient {
124        /// Stub: always returns `None`.
125        pub fn from_compiled_schema(_schema_json: &serde_json::Value) -> Option<Arc<Self>> {
126            None
127        }
128    }
129}
130
131// Secrets management and encryption (extracted to fraiseql-secrets crate) — optional, enable with
132// `features = ["secrets"]`
133#[cfg(feature = "secrets")]
134pub use fraiseql_secrets::{encryption, secrets_manager};
135
136// TLS/SSL and encryption
137pub mod tls;
138
139// Observer management - optional
140#[cfg(feature = "observers")]
141pub mod observers;
142
143// Arrow Flight integration - optional
144#[cfg(feature = "arrow")]
145pub mod arrow;
146
147// MCP (Model Context Protocol) server - optional
148#[cfg(feature = "mcp")]
149pub mod mcp;
150
151// Connection pool management and auto-tuning
152pub mod pool;
153
154// Object storage backends (local, S3, GCS, Azure Blob)
155pub mod storage;
156
157// Trusted documents (query allowlist)
158pub mod trusted_documents;
159
160// Multi-tenancy: pool factory, executor construction, health monitoring
161pub mod tenancy;
162
163// Testing utilities
164#[cfg(any(test, feature = "testing"))]
165pub mod testing;
166
167#[cfg(feature = "cli")]
168pub use cli::{Cli, ServerArgs};
169pub use logging::{
170    ErrorDetails, LogLevel, LogMetrics, RequestContext, RequestId, RequestLogger, SourceLocation,
171    StructuredLogEntry,
172};
173pub use metrics_server::{MetricsCollector, PrometheusMetrics};
174pub use schema::CompiledSchemaLoader;
175pub use server::Server;
176pub use server_config::ServerConfig;
177pub use tls::TlsSetup;
178pub use validation::{ComplexityValidationError, RequestValidator};
179
180/// Convenience re-exports for common server types.
181///
182/// ```rust
183/// use fraiseql_server::prelude::*;
184/// ```
185pub mod prelude {
186    pub use fraiseql_core::schema::CompiledSchema;
187
188    pub use crate::{
189        ComplexityValidationError, RequestValidator, Server, ServerConfig, ServerError, TlsSetup,
190    };
191}
192
193/// Server error type.
194#[derive(Debug, thiserror::Error)]
195#[non_exhaustive]
196pub enum ServerError {
197    /// Server binding error.
198    #[error("Failed to bind server: {0}")]
199    BindError(String),
200
201    /// Configuration error.
202    #[error("Configuration error: {0}")]
203    ConfigError(String),
204
205    /// Runtime error.
206    #[error("Runtime error: {0}")]
207    RuntimeError(#[from] fraiseql_core::error::FraiseQLError),
208
209    /// IO error.
210    #[error("IO error: {0}")]
211    IoError(#[from] std::io::Error),
212
213    /// Database error.
214    #[error("Database error: {0}")]
215    Database(String),
216
217    /// Validation error.
218    #[error("Validation error: {0}")]
219    Validation(String),
220
221    /// Resource conflict error.
222    #[error("Conflict: {0}")]
223    Conflict(String),
224
225    /// Resource not found error.
226    #[error("Not found: {0}")]
227    NotFound(String),
228}
229
230/// Server result type.
231pub type Result<T> = std::result::Result<T, ServerError>;