hive-router 0.0.49

GraphQL router/gateway for Federation
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
use http::Method;
use ntex::channel::oneshot;
use ntex::http::{header::HeaderName, header::HeaderValue, HeaderMap};
use ntex::router::Path;
use ntex::service::{fn_factory_with_config, fn_service, fn_shutdown, Service};
use ntex::web::{self, ws, Error, HttpRequest, HttpResponse};
use ntex::{chain, rt};
use sonic_rs::{JsonContainerTrait, JsonValueTrait, Value};
use std::cell::RefCell;
use std::collections::HashMap;
use std::io;
use std::rc::Rc;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::mpsc;
use tracing::{debug, error, trace, warn, Instrument};

use hive_router_internal::telemetry::traces::spans::graphql::GraphQLOperationSpan;
use hive_router_plan_executor::executors::graphql_transport_ws::{
    ClientMessage, CloseCode, ConnectionInitPayload, ServerMessage, WS_SUBPROTOCOL,
};
use hive_router_plan_executor::executors::websocket_common::{
    handshake_timeout, heartbeat, parse_frame_to_text, FrameNotParsedToText, WsState,
};
use hive_router_plan_executor::hooks::on_graphql_params::GraphQLParams;
use hive_router_plan_executor::plugin_context::{
    PluginContext, PluginRequestState, RouterHttpRequest,
};
use hive_router_plan_executor::response::graphql_error::{GraphQLError, GraphQLErrorExtensions};
use hive_router_query_planner::state::supergraph_state::OperationKind;

use crate::jwt::errors::JwtError;
use crate::pipeline::active_subscriptions::SubscriptionEvent;
use crate::pipeline::error::PipelineError;
use crate::pipeline::execute_planned_request;
use crate::pipeline::header::{ResponseMode, SingleContentType, StreamContentType};
use crate::pipeline::{
    hash_graphql_extensions, hash_graphql_variables, inbound_request_fingerprint,
    normalize::normalize_request_with_cache, parser::parse_operation_with_cache, usage_reporting,
    validation::validate_operation_with_cache,
};
use crate::schema_state::SchemaState;
use crate::shared_state::{RouterSharedState, SharedRouterResponse};

type WsStateRef = Rc<RefCell<WsState<tokio::sync::mpsc::Sender<()>>>>;

pub async fn ws_index(
    req: HttpRequest,
    schema_state: web::types::State<Arc<SchemaState>>,
    shared_state: web::types::State<Arc<RouterSharedState>>,
) -> Result<HttpResponse, Error> {
    let schema_state = schema_state.get_ref().clone();
    let shared_state = shared_state.get_ref().clone();

    let accepted_subprotocol = ws::subprotocols(&req)
        .find(|p| *p == WS_SUBPROTOCOL)
        .map(|_| WS_SUBPROTOCOL);

    let plugin_context = req.extensions().get::<Arc<PluginContext>>().cloned();

    ws::start(
        req,
        accepted_subprotocol,
        fn_factory_with_config(move |sink: ws::WsSink| {
            let schema_state = schema_state.clone();
            let shared_state = shared_state.clone();
            let plugin_context = plugin_context.clone();
            async move {
                ws_service(
                    accepted_subprotocol.is_some(),
                    sink,
                    schema_state,
                    shared_state,
                    plugin_context,
                )
                .await
            }
        }),
    )
    .await
}

async fn ws_service(
    has_accepted_subprotocol: bool,
    sink: ws::WsSink,
    schema_state: Arc<SchemaState>,
    shared_state: Arc<RouterSharedState>,
    plugin_context: Option<Arc<PluginContext>>,
) -> Result<impl Service<ws::Frame, Response = Option<ws::Message>, Error = io::Error>, web::Error>
{
    if !has_accepted_subprotocol {
        debug!("WebSocket connection rejecting due to unacceptable subprotocol");
        let _ = sink.send(CloseCode::SubprotocolNotAcceptable.into()).await;
        // we dont return an Err here because we want to gracefully close the
        // connection for the client side with a close frame. returning an Err
        // would result in an abrupt termination of the connection
    } else {
        debug!("WebSocket connection accepted");
    }

    let ws_uri: Rc<http::Uri> = Rc::new(
        shared_state
            .router_config
            .websocket_path()
            .expect("websocket path must exist because the websocket handler wouldn't have been mounted otherwise")
            .parse()
            .unwrap_or_else(|_| http::Uri::from_static("/graphql")),
    );
    let ws_path: Rc<Path<http::Uri>> = Rc::new(Path::new((*ws_uri).clone()));

    let (heartbeat_tx, heartbeat_rx) = oneshot::channel();
    let (acknowledged_tx, acknowledged_rx) = oneshot::channel();

    let state: WsStateRef = Rc::new(RefCell::new(WsState::new(acknowledged_tx)));

    rt::spawn(heartbeat(state.clone(), sink.clone(), heartbeat_rx));
    rt::spawn(handshake_timeout(
        state.clone(),
        sink.clone(),
        acknowledged_rx,
        CloseCode::ConnectionInitTimeout,
    ));

    let state_for_service = state.clone();
    let service = fn_service(move |frame| {
        let sink = sink.clone();
        let state = state_for_service.clone();
        let schema_state = schema_state.clone();
        let shared_state = shared_state.clone();
        let plugin_context = plugin_context.clone();
        let ws_uri = ws_uri.clone();
        let ws_path = ws_path.clone();
        async move {
            match parse_frame_to_text(frame, &state) {
                Ok(text) => Ok(handle_text_frame(
                    text,
                    sink,
                    state,
                    &schema_state,
                    &shared_state,
                    plugin_context,
                    &ws_uri,
                    &ws_path,
                )
                .await),
                Err(FrameNotParsedToText::Message(msg)) => Ok(Some(msg)),
                Err(FrameNotParsedToText::Closed) => {
                    // we dont need to emit anything here because the conneciton is already closed
                    Ok(None)
                }
                Err(FrameNotParsedToText::None) => Ok(None),
            }
        }
    });

    let on_shutdown = fn_shutdown(async move || {
        // stop heartbeat and handshake timeout tasks on shutdown
        let _ = heartbeat_tx.send(());
        if let Some(tx) = state.borrow_mut().acknowledged_tx.take() {
            let _ = tx.send(());
        }
        // clearing the map will drop all the senders, which will
        // in turn cancel all active subscription streams and perform
        // the cleanup in there
        state.borrow_mut().subscriptions.clear();
    });

    Ok(chain(service).and_then(on_shutdown))
}

/// Ensure a subscription is removed from active subscriptions when dropped (server-side).
struct SubscriptionGuard {
    state: WsStateRef,
    id: String,
}

impl Drop for SubscriptionGuard {
    fn drop(&mut self) {
        self.state.borrow_mut().subscriptions.remove(&self.id);
        trace!(id = %self.id, "Subscription removed from active subscriptions");
    }
}

lazy_static::lazy_static! {
    static ref WS_URI_PATH: http::Uri = http::Uri::from_static("/graphql");
}

#[allow(clippy::too_many_arguments)]
async fn handle_text_frame(
    text: String,
    sink: ws::WsSink,
    state: WsStateRef,
    schema_state: &Arc<SchemaState>,
    shared_state: &Arc<RouterSharedState>,
    plugin_context: Option<Arc<PluginContext>>,
    ws_uri: &http::Uri,
    ws_path: &Path<http::Uri>,
) -> Option<ws::Message> {
    let client_msg: ClientMessage = match sonic_rs::from_str(&text) {
        Ok(msg) => msg,
        Err(e) => {
            error!("Failed to parse client message to JSON: {}", e);
            return Some(CloseCode::BadRequest("Invalid message received from client").into());
        }
    };

    trace!("type" = client_msg.as_ref(), "Received client message");

    match client_msg {
        ClientMessage::ConnectionInit { payload } => {
            if state.borrow().handshake_received {
                return Some(CloseCode::TooManyInitialisationRequests.into());
            }
            state.borrow_mut().handshake_received = true;
            state.borrow_mut().init_payload = payload;
            state.borrow_mut().complete_handshake();

            let _ = sink.send(ServerMessage::ack()).await;

            debug!("Connection acknowledged");

            let header_map =
                parse_headers_from_connection_init_payload(state.borrow().init_payload.as_ref());
            if !header_map.is_empty() {
                trace!("Connection init message contains headers in the payload");
            } else {
                trace!("Connection init message does not contain headers in the payload");
            }

            None
        }
        ClientMessage::Subscribe { id, payload } => {
            if let Some(msg) = state.borrow().check_acknowledged() {
                return Some(msg);
            }

            if state.borrow().subscriptions.contains_key(&id) {
                return Some(CloseCode::SubscriberAlreadyExists(id).into());
            }

            let started_at = Instant::now();
            let operation_span = GraphQLOperationSpan::new();
            let span_clone = operation_span.clone();

            let result = async {
                let maybe_supergraph = schema_state.current_supergraph();
                let supergraph = match maybe_supergraph.as_ref() {
                    Some(supergraph) => supergraph,
                    None => {
                        warn!(
                            "No supergraph available yet, unable to process client subscribe message"
                        );
                        return Some(ServerMessage::error(
                            &id,
                            &[GraphQLError::from_message_and_extensions(
                                "No supergraph available yet".to_string(),
                                GraphQLErrorExtensions::new_from_code("SERVICE_UNAVAILABLE"),
                            )],
                        ));
                    }
                };

                let config = &shared_state.router_config.websocket;

                let connection_init_headers = if config.headers.accepts_connection_headers() {
                    let state_borrow = state.borrow();
                    parse_headers_from_connection_init_payload(state_borrow.init_payload.as_ref())
                } else {
                    HeaderMap::new()
                };

                let extensions_headers = if config.headers.accepts_operation_headers() {
                    parse_headers_from_extensions(payload.extensions.as_ref())
                } else {
                    HeaderMap::new()
                };

                // merge, extensions have precedence
                let mut headers = connection_init_headers;
                for (key, value) in extensions_headers.iter() {
                    headers.insert(key.clone(), value.clone());
                }

                // store the merged headers back to init_payload if configured to do so
                if config.headers.persist {
                    if let Some(ref mut init_payload) = state.borrow_mut().init_payload {
                        for (key, value) in headers.iter() {
                            if let Ok(val_str) = value.to_str() {
                                init_payload
                                    .fields
                                    .insert(key.to_string(), Value::from(val_str));
                            }
                        }
                    }
                }

                let payload = GraphQLParams {
                    query: Some(payload.query),
                    operation_name: payload.operation_name,
                    variables: payload.variables.unwrap_or_default(),
                    extensions: payload.extensions,
                };

                // synthetic router http request for plugins - there's no real http request
                // in the ws subscribe flow, so we assemble one from the ws path and merged headers.
                // of course there is the http upgrade request, but that one is useless for the plugin system
                let plugin_req_state = if let (Some(plugins), Some(ref plugin_context)) = (
                    shared_state.plugins.as_ref(),
                    plugin_context,
                ) {
                    Some(PluginRequestState {
                        plugins: plugins.clone(),
                        router_http_request: RouterHttpRequest {
                            uri: ws_uri,
                            method: &Method::POST,
                            version: http::Version::HTTP_11,
                            headers: &headers,
                            path: ws_uri.path(),
                            query_string: ws_uri.query().unwrap_or(""),
                            match_info: ws_path,
                        },
                        context: plugin_context.clone(),
                    })
                } else {
                    None
                };

                let client_name = headers
                    .get(
                        &shared_state
                            .router_config
                            .telemetry
                            .client_identification
                            .name_header,
                    )
                    .and_then(|v| v.to_str().ok());
                let client_version = headers
                    .get(
                        &shared_state
                            .router_config
                            .telemetry
                            .client_identification
                            .version_header,
                    )
                    .and_then(|v| v.to_str().ok());

                let parser_result =
                    match parse_operation_with_cache(shared_state, &payload, &plugin_req_state).await {
                        Ok(result) => result,
                        Err(err) => return Some(err.into_server_message(&id)),
                    };

                let parser_payload = match parser_result {
                    crate::pipeline::parser::ParseResult::Payload(payload) => payload,
                    crate::pipeline::parser::ParseResult::EarlyResponse(_) => {
                        return Some(ServerMessage::error(
                            &id,
                            &[GraphQLError::from_message_and_code(
                                "Unexpected early response during parse",
                                "INTERNAL_SERVER_ERROR",
                            )],
                        ));
                    }
                };

                operation_span.record_details(
                    &parser_payload.minified_document,
                    (&parser_payload).into(),
                    client_name,
                    client_version,
                    &parser_payload.hive_operation_hash,
                );

                match validate_operation_with_cache(
                    supergraph,
                    schema_state,
                    shared_state,
                    &parser_payload,
                    &plugin_req_state,
                )
                .await
                {
                    Ok(Some(_)) => {
                        return Some(ServerMessage::error(
                            &id,
                            &[GraphQLError::from_message_and_code(
                                "Unexpected early response during validation",
                                "INTERNAL_SERVER_ERROR",
                            )],
                        ));
                    }
                    Ok(None) => {}
                    Err(err) => return Some(err.into_server_message(&id)),
                }

                let normalize_payload = match normalize_request_with_cache(
                    supergraph,
                    schema_state,
                    &payload,
                    &parser_payload,
                )
                .await
                {
                    Ok(payload) => payload,
                    Err(err) => return Some(err.into_server_message(&id)),
                };

                let is_subscription = matches!(
                    normalize_payload.operation_for_plan.operation_kind,
                    Some(OperationKind::Subscription)
                );

                if is_subscription && !shared_state.router_config.subscriptions.enabled {
                    return Some(PipelineError::SubscriptionsNotSupported.into_server_message(&id));
                }

                let request_dedupe_enabled =
                    shared_state.router_config.traffic_shaping.router.dedupe.enabled;

                let fingerprint = if request_dedupe_enabled
                    && matches!(
                        normalize_payload.operation_for_plan.operation_kind,
                        // same deduplication applies for queries and subscriptions
                        None | Some(OperationKind::Query) | Some(OperationKind::Subscription)
                    ) {
                    let variables_hash = hash_graphql_variables(&payload.variables);
                    let extensions_hash = payload
                        .extensions
                        .as_ref()
                        .map_or(0, hash_graphql_extensions);
                    Some(inbound_request_fingerprint(
                        &Method::POST,
                        ws_uri.path(),
                        &headers,
                        &shared_state.in_flight_requests_header_policy,
                        supergraph.schema_checksum(),
                        normalize_payload.normalized_operation_hash,
                        variables_hash,
                        extensions_hash,
                    ))
                } else {
                    None
                };

                // synthetic request details for plan executor
                let response_mode = ResponseMode::Dual(
                    SingleContentType::default(),
                    StreamContentType::default(),
                );
                let exec = |guard| execute_planned_request(
                    &Method::POST,
                    ws_uri,
                    &headers,
                    payload,
                    &normalize_payload,
                    supergraph,
                    shared_state,
                    schema_state,
                    operation_span,
                    plugin_req_state,
                    &response_mode,
                    guard,
                );

                let shared_response = if let Some(fp) = fingerprint {
                    let result = if is_subscription {
                        shared_state
                            .in_flight_requests
                            .claim(fp)
                            .get_or_try_init_with_guard(|guard| exec(Some(guard)))
                            .await
                    } else {
                        shared_state
                            .in_flight_requests
                            .claim(fp)
                            .get_or_try_init(|| exec(None))
                            .await
                    };
                    let (shared_response, _role) = match result {
                        Ok(result) => result,
                        Err(PipelineError::JwtError(err)) => {
                            let _ = sink.send(err.clone().into_server_message(&id)).await;
                            // we report error as graphql error, but we also close the
                            // connection since we're dealing with auth so let's be safe
                            return Some(err.into_close_message());
                        },
                        Err(err) => return Some(err.into_server_message(&id)),
                    };
                    Arc::unwrap_or_clone(shared_response)
                } else {
                    match exec(None).await {
                        Ok(result) => result,
                        Err(PipelineError::JwtError(err)) => {
                            let _ = sink.send(err.clone().into_server_message(&id)).await;
                            // we report error as graphql error, but we also close the
                            // connection since we're dealing with auth so let's be safe
                            return Some(err.into_close_message());
                        },
                        Err(err) => return Some(err.into_server_message(&id)),
                    }
                };

                if let Some(hive_usage_agent) = &shared_state.hive_usage_agent {
                    usage_reporting::collect_usage_report(
                        supergraph.supergraph_schema.clone(),
                        started_at.elapsed(),
                        client_name,
                        client_version,
                        normalize_payload.operation_for_plan.name.as_deref(),
                        &parser_payload.minified_document,
                        hive_usage_agent,
                        shared_state
                            .router_config
                            .telemetry
                            .hive
                            .as_ref()
                            .map(|c| &c.usage_reporting)
                            .expect("Expected Usage Reporting options to be present when Hive Usage Agent is initialized"),
                        shared_response.error_count(),
                    )
                    .await;
                }

                match shared_response {
                    SharedRouterResponse::Single(response) => {
                        let _ = sink.send(ServerMessage::next(&id, &response.body)).await;
                        Some(ServerMessage::complete(&id))
                    }
                    SharedRouterResponse::Stream(response) => {
                        let (cancel_tx, mut cancel_rx) = mpsc::channel::<()>(1);

                        state
                            .borrow_mut()
                            .subscriptions
                            .insert(id.clone(), cancel_tx);

                        let guard = SubscriptionGuard {
                            state: state.clone(),
                            id: id.clone(),
                        };

                        let mut receiver = response
                            .receiver
                            .unwrap_or_else(|| response.body.subscribe());

                        trace!(id = %id, "Subscription started");

                        let sink = sink.clone();
                        let id_for_loop = id.clone();
                        // must be spawned - blocking the frame handler would prevent
                        // ClientMessage::Complete from being received and processed,
                        // making cancellation impossible
                        rt::spawn(async move {
                            let _guard = guard;
                            let mut cancelled = false;

                            loop {
                                tokio::select! {
                                    maybe_item = receiver.recv() => {
                                        match maybe_item {
                                            Ok(SubscriptionEvent::Raw(data)) => {
                                                let _ = sink.send(ServerMessage::next(&id_for_loop, &data)).await;
                                            }
                                            Ok(SubscriptionEvent::Error(errors)) => {
                                                let _ = sink.send(ServerMessage::error(&id_for_loop, &errors)).await;
                                                break;
                                            }
                                            Err(tokio::sync::broadcast::error::RecvError::Lagged(n)) => {
                                                trace!(id = %id_for_loop, lagged = n, "broadcast receiver lagged, skipping missed messages");
                                                continue;
                                            }
                                            Err(tokio::sync::broadcast::error::RecvError::Closed) => {
                                                break;
                                            }
                                        }
                                    }
                                    _ = cancel_rx.recv() => {
                                        cancelled = true;
                                        break;
                                    }
                                }
                            }

                            if cancelled {
                                trace!(id = %id_for_loop, "Subscription cancelled");
                            } else {
                                trace!(id = %id_for_loop, "Subscription completed");
                                let _ = sink.send(ServerMessage::complete(&id_for_loop)).await;
                            }
                        });

                        None
                    }
                }
            }
            .instrument(span_clone)
            .await;

            result
        }
        ClientMessage::Complete { id } => {
            if let Some(msg) = state.borrow().check_acknowledged() {
                return Some(msg);
            }

            if let Some(cancel_tx) = state.borrow_mut().subscriptions.remove(&id) {
                trace!(id = %id, "Client requested subscription cancellation");
                let _ = cancel_tx.try_send(());
            }
            None
        }
        ClientMessage::Ping {} => {
            // respond with pong always, regardless of acknowledged state
            // the client should be able to use subprotocol pings/pongs to check liveness
            Some(ServerMessage::pong())
        }
        ClientMessage::Pong {} => None,
    }
}

/// Parses headers from a sonic_rs::Object into a HeaderMap. Only stringifiable
/// values are included; nulls, arrays, objects and other non-primitive values
/// are ignored.
fn parse_headers_from_object(headers_obj: &sonic_rs::Object) -> HeaderMap {
    let mut header_map = HeaderMap::new();

    for (key, value) in headers_obj.iter() {
        let Ok(name) = HeaderName::try_from(key as &str) else {
            continue;
        };
        let header_value = if let Some(s) = value.as_str() {
            HeaderValue::try_from(s).ok()
        } else if let Some(b) = value.as_bool() {
            Some(HeaderValue::from_static(if b { "true" } else { "false" }))
        } else if let Some(n) = value.as_i64() {
            Some(HeaderValue::from(n))
        } else if let Some(n) = value.as_u64() {
            Some(HeaderValue::from(n))
        } else if let Some(f) = value.as_f64() {
            // f64 has no HeaderValue::From impl, string alloc unavoidable
            HeaderValue::try_from(f.to_string()).ok()
        } else {
            None // ignore nulls, arrays, objects
        };
        if let Some(val) = header_value {
            header_map.insert(name, val);
        }
    }

    header_map
}

fn parse_headers_from_connection_init_payload(
    payload: Option<&ConnectionInitPayload>,
) -> HeaderMap {
    let mut header_map = HeaderMap::new();
    if let Some(payload) = payload {
        // First check if there's a nested "headers" object
        if let Some(headers_prop) = payload.fields.get("headers") {
            if let Some(headers_obj) = headers_prop.as_object() {
                header_map = parse_headers_from_object(headers_obj);
                return header_map;
            }
        }

        // If no nested "headers" object, treat all top-level fields as potential headers
        // Convert the entire fields HashMap to a sonic_rs::Object
        let mut obj = sonic_rs::Object::new();
        for (k, v) in payload.fields.iter() {
            obj.insert(k, v.clone());
        }
        header_map = parse_headers_from_object(&obj);
    }
    header_map
}

fn parse_headers_from_extensions(extensions: Option<&HashMap<String, Value>>) -> HeaderMap {
    let mut header_map = HeaderMap::new();
    if let Some(ext) = extensions {
        if let Some(headers_value) = ext.get("headers") {
            if let Some(headers_obj) = headers_value.as_object() {
                header_map = parse_headers_from_object(headers_obj);
            }
        }
    }
    header_map
}

// NOTE: no `From` trait because it can into ws message and ws closecode but both are ws::Message
impl PipelineError {
    fn into_server_message(self, id: &str) -> ws::Message {
        let code = self.graphql_error_code();
        let message = self.graphql_error_message();

        let graphql_error = GraphQLError::from_message_and_extensions(
            message,
            GraphQLErrorExtensions::new_from_code(code),
        );

        ServerMessage::error(id, &[graphql_error])
    }
}

// NOTE: no `From` trait because it can into ws message and ws closecode but both are ws::Message
impl JwtError {
    fn into_server_message(self, id: &str) -> ws::Message {
        ServerMessage::error(
            id,
            &[GraphQLError::from_message_and_code(
                self.to_string(),
                self.error_code(),
            )],
        )
    }
    fn into_close_message(self) -> ws::Message {
        CloseCode::Forbidden(self.error_code().to_string()).into()
    }
}

#[cfg(test)]
mod tests {
    use sonic_rs::json;

    use super::*;

    #[test]
    fn should_parse_headers_from_object() {
        let headers_json = json!({
            "authorization": "Bearer token123",
            "x-custom-header": "custom-value",
            "x-number": 42,
            "x-bool": true,
            "x-float": 3.14
        });

        let headers_obj = headers_json.as_object().expect("Failed to get object");

        let headers = parse_headers_from_object(&headers_obj);

        assert_eq!(
            headers.get("authorization").unwrap().to_str().unwrap(),
            "Bearer token123"
        );
        assert_eq!(
            headers.get("x-custom-header").unwrap().to_str().unwrap(),
            "custom-value"
        );
        assert_eq!(headers.get("x-number").unwrap().to_str().unwrap(), "42");
        assert_eq!(headers.get("x-bool").unwrap().to_str().unwrap(), "true");
        assert_eq!(headers.get("x-float").unwrap().to_str().unwrap(), "3.14");
    }

    // TODO: hella tests
}