oxirs-gql 0.4.1

GraphQL façade for OxiRS with automatic schema generation from RDF ontologies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
//! Juniper GraphQL HTTP server implementation
//!
//! This module provides a complete HTTP server implementation using Juniper
//! for GraphQL processing and Hyper for HTTP handling.

use crate::graphiql_integration::{generate_graphiql_html, GraphiQLConfig};
use crate::juniper_schema::{create_schema, GraphQLContext, Schema};
use crate::RdfStore;
use anyhow::Result;
use chrono;
use hyper::service::service_fn;
use hyper::{body::Incoming, Method, Request, Response, StatusCode};
use hyper_util::rt::{TokioExecutor, TokioIo};
use hyper_util::server::conn::auto::Builder;
use juniper_hyper::{graphql, playground};
use serde_json;
use std::convert::Infallible;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::net::TcpListener;
use tracing::{debug, error, info, warn};

/// Configuration for the GraphQL server
#[derive(Debug, Clone)]
pub struct GraphQLServerConfig {
    /// Enable GraphiQL web interface
    pub enable_graphiql: bool,
    /// Enable GraphQL Playground web interface  
    pub enable_playground: bool,
    /// Enable introspection queries
    pub enable_introspection: bool,
    /// Maximum query depth allowed
    pub max_query_depth: Option<usize>,
    /// Maximum query complexity allowed
    pub max_query_complexity: Option<usize>,
    /// CORS configuration
    pub cors_enabled: bool,
    /// Allowed CORS origins (None means all origins allowed)
    pub cors_origins: Option<Vec<String>>,
    /// Maximum number of concurrently-served TCP connections. Excess
    /// connections back-pressure (wait for a slot) rather than being accepted
    /// unbounded, protecting against FD/memory exhaustion.
    pub max_connections: usize,
    /// Maximum accepted request body size in bytes. A `POST /graphql` whose
    /// `Content-Length` exceeds this is rejected with `413 Payload Too Large`
    /// before the body is read, so a large upload cannot OOM the process.
    pub max_body_size: usize,
    /// Hard upper bound on how long a single connection may live. Bounds
    /// slow-client (Slowloris) attacks that would otherwise pin a connection
    /// indefinitely.
    pub connection_timeout: std::time::Duration,
}

/// Default maximum number of concurrent connections.
const DEFAULT_MAX_CONNECTIONS: usize = 1024;
/// Default maximum request body size (2 MiB).
const DEFAULT_MAX_BODY_SIZE: usize = 2 * 1024 * 1024;
/// Default per-connection lifetime bound.
const DEFAULT_CONNECTION_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);

impl Default for GraphQLServerConfig {
    fn default() -> Self {
        Self {
            enable_graphiql: true,
            enable_playground: true,
            enable_introspection: true,
            max_query_depth: Some(15),
            max_query_complexity: Some(1000),
            cors_enabled: true,
            cors_origins: None, // Allow all origins by default
            max_connections: DEFAULT_MAX_CONNECTIONS,
            max_body_size: DEFAULT_MAX_BODY_SIZE,
            connection_timeout: DEFAULT_CONNECTION_TIMEOUT,
        }
    }
}

/// The main GraphQL server using Juniper
pub struct JuniperGraphQLServer {
    schema: Arc<Schema>,
    context: GraphQLContext,
    config: GraphQLServerConfig,
}

impl JuniperGraphQLServer {
    /// Create a new GraphQL server with an RDF store
    pub fn new(store: Arc<RdfStore>) -> Self {
        let schema = Arc::new(create_schema());
        let context = GraphQLContext { store };
        let config = GraphQLServerConfig::default();

        Self {
            schema,
            context,
            config,
        }
    }

    /// Create a new GraphQL server with custom configuration
    pub fn with_config(store: Arc<RdfStore>, config: GraphQLServerConfig) -> Self {
        let schema = Arc::new(create_schema());
        let context = GraphQLContext { store };

        Self {
            schema,
            context,
            config,
        }
    }

    /// Start the GraphQL server on the specified address
    pub async fn start(&self, addr: SocketAddr) -> Result<()> {
        info!("Starting Juniper GraphQL server on {}", addr);

        // Create the service with proper cloning for the closure
        let schema = self.schema.clone();
        let context = self.context.clone();
        let config = self.config.clone();

        // Create TCP listener
        let listener = TcpListener::bind(addr).await?;

        info!("GraphQL server running on http://{}", addr);
        info!("GraphQL endpoint: http://{}/graphql", addr);

        // Bound the number of concurrently-served connections.
        let connection_semaphore = Arc::new(tokio::sync::Semaphore::new(config.max_connections));
        let connection_timeout = config.connection_timeout;

        // Graceful shutdown on Ctrl-C / SIGINT.
        let shutdown = async {
            if let Err(e) = tokio::signal::ctrl_c().await {
                error!("Failed to install shutdown signal handler: {}", e);
            }
        };
        tokio::pin!(shutdown);

        // Accept connections until a shutdown signal arrives.
        loop {
            let (stream, _peer) = tokio::select! {
                accepted = listener.accept() => match accepted {
                    Ok(result) => result,
                    Err(e) => {
                        error!("Failed to accept connection: {}", e);
                        continue;
                    }
                },
                _ = &mut shutdown => {
                    info!("Shutdown signal received; stopping accept loop");
                    break;
                }
            };

            // Acquire a connection slot (back-pressure instead of unbounded growth).
            let permit = match Arc::clone(&connection_semaphore).acquire_owned().await {
                Ok(permit) => permit,
                Err(_) => {
                    // Semaphore closed — server is shutting down.
                    break;
                }
            };

            let schema_clone = schema.clone();
            let context_clone = context.clone();
            let config_clone = config.clone();

            // Handle each connection in a separate task.
            tokio::spawn(async move {
                // Hold the permit for the lifetime of the connection; it is
                // released (slot freed) when this task ends.
                let _permit = permit;

                let io = TokioIo::new(stream);
                let builder = Builder::new(TokioExecutor::new());

                let service = service_fn(move |req| {
                    Self::handle_request(
                        req,
                        schema_clone.clone(),
                        context_clone.clone(),
                        config_clone.clone(),
                    )
                });

                // Bound total connection lifetime to mitigate slow-client attacks.
                match tokio::time::timeout(
                    connection_timeout,
                    builder.serve_connection(io, service),
                )
                .await
                {
                    Ok(Ok(())) => {}
                    Ok(Err(e)) => error!("Connection error: {}", e),
                    Err(_) => warn!(
                        "Connection timed out after {:?}; closing",
                        connection_timeout
                    ),
                }
            });
        }

        // Drain in-flight connections: acquiring all permits proves every
        // outstanding connection task has released its slot.
        info!("Draining in-flight connections before shutdown");
        connection_semaphore.close();
        // Best-effort bounded drain.
        let drain_deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(10);
        while connection_semaphore.available_permits() < config.max_connections {
            if tokio::time::Instant::now() >= drain_deadline {
                warn!("Drain timeout reached; forcing shutdown");
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        }
        info!("GraphQL server shut down cleanly");
        Ok(())
    }

    /// Handle individual HTTP requests
    async fn handle_request(
        req: Request<Incoming>,
        schema: Arc<Schema>,
        context: GraphQLContext,
        config: GraphQLServerConfig,
    ) -> Result<Response<String>, Infallible> {
        let response = match Self::handle_request_inner(req, schema, context, config).await {
            Ok(response) => response,
            Err(err) => {
                error!("Request handling error: {}", err);
                Response::builder()
                    .status(StatusCode::INTERNAL_SERVER_ERROR)
                    .header("content-type", "application/json")
                    .body(format!(
                        r#"{{"errors": [{{ "message": "{}" }}]}}"#,
                        err.to_string().replace('"', "\\\"")
                    ))
                    .expect("building error response should succeed")
            }
        };

        Ok(response)
    }

    /// Inner request handling with proper error propagation
    async fn handle_request_inner(
        req: Request<Incoming>,
        schema: Arc<Schema>,
        context: GraphQLContext,
        config: GraphQLServerConfig,
    ) -> Result<Response<String>> {
        let method = req.method();
        let path = req.uri().path();

        debug!("Handling {} request to {}", method, path);

        // Apply CORS headers if enabled
        let mut response_builder = Response::builder();
        if config.cors_enabled {
            response_builder = response_builder
                .header("Access-Control-Allow-Origin", "*")
                .header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
                .header(
                    "Access-Control-Allow-Headers",
                    "Content-Type, Authorization",
                );
        }

        // Handle CORS preflight requests
        if method == Method::OPTIONS {
            return Ok(response_builder
                .status(StatusCode::OK)
                .body(String::new())?);
        }

        match (method, path) {
            // Main GraphQL endpoint
            (&Method::GET, "/graphql") | (&Method::POST, "/graphql") => {
                debug!("Processing GraphQL request");

                // Reject oversized request bodies before reading them (guards
                // against a large POST OOM-ing the process).
                if let Some(len) = req
                    .headers()
                    .get(hyper::header::CONTENT_LENGTH)
                    .and_then(|v| v.to_str().ok())
                    .and_then(|v| v.parse::<usize>().ok())
                {
                    if len > config.max_body_size {
                        warn!(
                            "Rejecting request: body {} bytes exceeds limit {}",
                            len, config.max_body_size
                        );
                        return Ok(response_builder
                            .status(StatusCode::PAYLOAD_TOO_LARGE)
                            .header("content-type", "application/json")
                            .body(
                                r#"{"errors":[{"message":"Request body too large"}]}"#.to_string(),
                            )?);
                    }
                }

                let mut response = graphql(schema, Arc::new(context), req).await;

                // Add CORS headers to GraphQL response
                if config.cors_enabled {
                    let headers = response.headers_mut();
                    headers.insert(
                        "Access-Control-Allow-Origin",
                        "*".parse().expect("parse should succeed for valid input"),
                    );
                    headers.insert(
                        "Access-Control-Allow-Methods",
                        "GET, POST, OPTIONS"
                            .parse()
                            .expect("parse should succeed for valid input"),
                    );
                    headers.insert(
                        "Access-Control-Allow-Headers",
                        "Content-Type, Authorization"
                            .parse()
                            .expect("parse should succeed for valid input"),
                    );
                }
                Ok(response)
            }

            // GraphiQL interface (enhanced)
            (&Method::GET, "/graphiql") if config.enable_graphiql => {
                debug!("Serving enhanced GraphiQL interface");
                let graphiql_config = GraphiQLConfig {
                    endpoint: "/graphql".to_string(),
                    enable_history: true,
                    enable_templates: true,
                    enable_custom_headers: true,
                    enable_metrics: true,
                    default_dark_theme: false,
                    enable_sharing: true,
                    enable_export: true,
                    custom_css: None,
                    title: "OxiRS GraphQL Explorer".to_string(),
                    subscription_endpoint: None,
                    ..Default::default()
                };

                let html = generate_graphiql_html(&graphiql_config);

                Ok(response_builder
                    .status(StatusCode::OK)
                    .header("content-type", "text/html; charset=utf-8")
                    .body(html)?)
            }

            // GraphQL Playground
            (&Method::GET, "/playground") if config.enable_playground => {
                debug!("Serving GraphQL Playground");
                let response = playground("/graphql", None).await;
                Ok(response)
            }

            // Health check endpoint
            (&Method::GET, "/health") => {
                debug!("Health check request");
                let health_info = serde_json::json!({
                    "status": "healthy",
                    "service": "oxirs-graphql",
                    "version": env!("CARGO_PKG_VERSION"),
                    "timestamp": chrono::Utc::now().to_rfc3339(),
                    "endpoints": {
                        "graphql": "/graphql",
                        "graphiql": if config.enable_graphiql { serde_json::Value::String("/graphiql".to_string()) } else { serde_json::Value::Null },
                        "playground": if config.enable_playground { serde_json::Value::String("/playground".to_string()) } else { serde_json::Value::Null }
                    }
                });

                Ok(response_builder
                    .status(StatusCode::OK)
                    .header("content-type", "application/json")
                    .body(health_info.to_string())?)
            }

            // Schema introspection endpoint (SDL)
            (&Method::GET, "/schema") if config.enable_introspection => {
                debug!("Schema introspection request");

                // Complete SDL generated from Juniper schema
                let sdl = r#"
"""
An RDF IRI (Internationalized Resource Identifier)
"""
scalar IRI

"""
An RDF Literal with optional language tag and datatype
"""
scalar RdfLiteral

"""
An RDF Named Node (IRI)
"""
type RdfNamedNode {
    """The IRI of this named node"""
    iri: IRI!
    """A human-readable label for this resource (if available)"""
    label: String
    """A description of this resource (if available)"""
    description: String
}

"""
An RDF Literal value
"""
type RdfLiteralNode {
    """The literal value"""
    literal: RdfLiteral!
    """The string representation of the value"""
    value: String!
    """The language tag if this is a language-tagged string"""
    language: String
    """The datatype IRI if this is a typed literal"""
    datatype: IRI
}

"""
An RDF Blank Node
"""
type RdfBlankNode {
    """The identifier of the blank node"""
    id: ID!
    """Human-readable representation"""
    label: String!
}

"""
An RDF term which can be an IRI, Literal, or Blank Node
"""
union RdfTerm = RdfNamedNode | RdfLiteralNode | RdfBlankNode

"""
An RDF Triple (subject-predicate-object statement)
"""
type RdfTriple {
    """The subject of the triple"""
    subject: RdfTerm!
    """The predicate of the triple"""
    predicate: RdfNamedNode!
    """The object of the triple"""
    object: RdfTerm!
}

"""
An RDF Quad (triple + named graph)
"""
type RdfQuad {
    """The subject of the quad"""
    subject: RdfTerm!
    """The predicate of the quad"""
    predicate: RdfNamedNode!
    """The object of the quad"""
    object: RdfTerm!
    """The named graph (None for default graph)"""
    graph: RdfNamedNode
}

"""
A variable binding in a SPARQL result
"""
type SparqlBinding {
    """The variable name"""
    variable: String!
    """The bound value"""
    value: RdfTerm!
}

"""
A single row from a SPARQL query result set
"""
type SparqlResultRow {
    """Variable bindings as key-value pairs"""
    bindings: [SparqlBinding!]!
}

"""
Results from a SPARQL SELECT query
"""
type SparqlSolutions {
    """Variable names in the result set"""
    variables: [String!]!
    """Result rows"""
    rows: [SparqlResultRow!]!
    """Total number of results"""
    count: Int!
}

"""
Result from a SPARQL ASK query
"""
type SparqlBoolean {
    """The boolean result"""
    result: Boolean!
}

"""
Graph results from a SPARQL CONSTRUCT or DESCRIBE query
"""
type SparqlGraph {
    """The resulting triples"""
    triples: [RdfTriple!]!
    """Total number of triples"""
    count: Int!
}

"""
Result of a SPARQL query
"""
union SparqlResult = SparqlSolutions | SparqlBoolean | SparqlGraph

"""
Information about the RDF store
"""
type StoreInfo {
    """Total number of triples in the store"""
    tripleCount: Int!
    """Version of the GraphQL server"""
    version: String!
    """Description of the store"""
    description: String!
}

"""
Input for executing SPARQL queries
"""
input SparqlQueryInput {
    """The SPARQL query string"""
    query: String!
    """Optional result limit"""
    limit: Int
    """Optional result offset"""
    offset: Int
}

"""
Filters for querying RDF data
"""
input RdfQueryFilter {
    """Filter by subject IRI pattern"""
    subject: String
    """Filter by predicate IRI pattern"""
    predicate: String
    """Filter by object value pattern"""
    object: String
    """Filter by named graph"""
    graph: String
    """Result limit"""
    limit: Int
    """Result offset"""
    offset: Int
}

"""
The root query type
"""
type Query {
    """Get basic information about the RDF store"""
    info: StoreInfo!
    """Execute a SPARQL query"""
    sparql(input: SparqlQueryInput!): SparqlResult!
    """Get all triples matching optional filters"""
    triples(filter: RdfQueryFilter): [RdfTriple!]!
    """Get all subjects in the store"""
    subjects(limit: Int): [RdfNamedNode!]!
    """Get all predicates in the store"""
    predicates(limit: Int): [RdfNamedNode!]!
    """Search for resources by label or IRI pattern"""
    search(pattern: String!, limit: Int): [RdfNamedNode!]!
}

schema {
    query: Query
}
"#;

                Ok(response_builder
                    .status(StatusCode::OK)
                    .header("content-type", "text/plain")
                    .body(sdl.to_string())?)
            }

            // Root endpoint - redirect to GraphiQL or provide info
            (&Method::GET, "/") => {
                if config.enable_graphiql {
                    Ok(Response::builder()
                        .status(StatusCode::FOUND)
                        .header("location", "/graphiql")
                        .body(String::new())?)
                } else if config.enable_playground {
                    Ok(Response::builder()
                        .status(StatusCode::FOUND)
                        .header("location", "/playground")
                        .body(String::new())?)
                } else {
                    let info = serde_json::json!({
                        "service": "OxiRS GraphQL Server",
                        "version": env!("CARGO_PKG_VERSION"),
                        "description": "GraphQL interface for RDF data using Juniper",
                        "endpoints": {
                            "graphql": "/graphql",
                            "health": "/health",
                            "schema": "/schema"
                        }
                    });

                    Ok(response_builder
                        .status(StatusCode::OK)
                        .header("content-type", "application/json")
                        .body(info.to_string())?)
                }
            }

            // 404 for unknown endpoints
            _ => {
                warn!("Unknown endpoint requested: {} {}", method, path);
                let error_response = serde_json::json!({
                    "error": "Not Found",
                    "message": format!("Endpoint {} {} not found", method, path),
                    "available_endpoints": [
                        "/graphql",
                        "/health",
                        if config.enable_graphiql { "/graphiql" } else { "" },
                        if config.enable_playground { "/playground" } else { "" },
                        if config.enable_introspection { "/schema" } else { "" }
                    ]
                });

                Ok(response_builder
                    .status(StatusCode::NOT_FOUND)
                    .header("content-type", "application/json")
                    .body(error_response.to_string())?)
            }
        }
    }
}

/// Builder for GraphQL server configuration
pub struct GraphQLServerBuilder {
    config: GraphQLServerConfig,
}

impl GraphQLServerBuilder {
    pub fn new() -> Self {
        Self {
            config: GraphQLServerConfig::default(),
        }
    }

    pub fn enable_graphiql(mut self, enable: bool) -> Self {
        self.config.enable_graphiql = enable;
        self
    }

    pub fn enable_playground(mut self, enable: bool) -> Self {
        self.config.enable_playground = enable;
        self
    }

    pub fn enable_introspection(mut self, enable: bool) -> Self {
        self.config.enable_introspection = enable;
        self
    }

    pub fn max_query_depth(mut self, depth: Option<usize>) -> Self {
        self.config.max_query_depth = depth;
        self
    }

    pub fn max_query_complexity(mut self, complexity: Option<usize>) -> Self {
        self.config.max_query_complexity = complexity;
        self
    }

    pub fn cors_enabled(mut self, enabled: bool) -> Self {
        self.config.cors_enabled = enabled;
        self
    }

    pub fn cors_origins(mut self, origins: Vec<String>) -> Self {
        self.config.cors_origins = Some(origins);
        self
    }

    pub fn build(self, store: Arc<RdfStore>) -> JuniperGraphQLServer {
        JuniperGraphQLServer::with_config(store, self.config)
    }
}

impl Default for GraphQLServerBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Convenience function to start a GraphQL server with default configuration
pub async fn start_graphql_server(store: Arc<RdfStore>, addr: SocketAddr) -> Result<()> {
    let server = JuniperGraphQLServer::new(store);
    server.start(addr).await
}

/// Convenience function to start a GraphQL server with custom configuration
pub async fn start_graphql_server_with_config(
    store: Arc<RdfStore>,
    addr: SocketAddr,
    config: GraphQLServerConfig,
) -> Result<()> {
    let server = JuniperGraphQLServer::with_config(store, config);
    server.start(addr).await
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_server_creation() {
        let store = Arc::new(RdfStore::new().expect("Failed to create store"));
        let server = JuniperGraphQLServer::new(store);

        // Just test that we can create the server
        assert!(server.config.enable_graphiql);
        assert!(server.config.enable_playground);
        assert!(server.config.enable_introspection);
    }

    #[tokio::test]
    async fn test_server_builder() {
        let store = Arc::new(RdfStore::new().expect("Failed to create store"));

        let server = GraphQLServerBuilder::new()
            .enable_graphiql(false)
            .enable_playground(true)
            .enable_introspection(false)
            .max_query_depth(Some(10))
            .cors_enabled(true)
            .build(store);

        assert!(!server.config.enable_graphiql);
        assert!(server.config.enable_playground);
        assert!(!server.config.enable_introspection);
        assert_eq!(server.config.max_query_depth, Some(10));
        assert!(server.config.cors_enabled);
    }

    #[tokio::test]
    async fn test_health_endpoint() {
        // This test would require actually starting a server
        // For now, just test the builder functionality
        let store = Arc::new(RdfStore::new().expect("Failed to create store"));
        let _server = JuniperGraphQLServer::new(store);

        // In a real test, we would:
        // 1. Start the server on a random port
        // 2. Make HTTP requests to test endpoints
        // 3. Verify responses
        // This requires more complex test infrastructure
    }

    #[test]
    fn regression_hardening_defaults_present() {
        let config = GraphQLServerConfig::default();
        assert_eq!(config.max_connections, DEFAULT_MAX_CONNECTIONS);
        assert_eq!(config.max_body_size, DEFAULT_MAX_BODY_SIZE);
        assert_eq!(config.connection_timeout, DEFAULT_CONNECTION_TIMEOUT);
        assert!(config.max_connections > 0, "must cap connections");
        assert!(config.max_body_size > 0, "must cap body size");
    }

    #[tokio::test]
    async fn regression_oversized_body_rejected_with_413() {
        // Find a free port, then hand it to the server.
        let probe = std::net::TcpListener::bind("127.0.0.1:0").expect("bind probe");
        let addr = probe.local_addr().expect("addr");
        drop(probe);

        let store = Arc::new(RdfStore::new().expect("store"));
        let config = GraphQLServerConfig {
            max_body_size: 16,
            connection_timeout: std::time::Duration::from_secs(5),
            ..Default::default()
        };
        let server = JuniperGraphQLServer::with_config(store, config);

        let server_task = tokio::spawn(async move {
            let _ = server.start(addr).await;
        });

        // Wait until the server is accepting connections.
        let client = reqwest::Client::new();
        let url = format!("http://{addr}/graphql");
        let mut connected = false;
        for _ in 0..50 {
            if tokio::net::TcpStream::connect(addr).await.is_ok() {
                connected = true;
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        }
        assert!(connected, "server did not start listening");

        // Body far larger than the 16-byte cap must be rejected with 413.
        let big_body = "x".repeat(4096);
        let resp = client
            .post(&url)
            .header("content-type", "application/json")
            .body(big_body)
            .send()
            .await
            .expect("request sent");
        assert_eq!(
            resp.status().as_u16(),
            413,
            "oversized body must be rejected with 413"
        );

        server_task.abort();
    }
}