graphql_composition/
result.rs

1use crate::{Diagnostics, diagnostics::CompositeSchemasErrorCode, federated_graph::FederatedGraph};
2
3/// The result of a [`compose()`](crate::compose()) invocation.
4pub struct CompositionResult {
5    pub(crate) federated_graph: Option<FederatedGraph>,
6    pub(crate) diagnostics: Diagnostics,
7}
8
9impl CompositionResult {
10    /// Treat all warnings as fatal.
11    #[doc(hidden)]
12    pub fn warnings_are_fatal(mut self) -> Self {
13        if self.diagnostics.iter().any(|diagnostic| {
14            diagnostic.composite_shemas_error_code() != Some(CompositeSchemasErrorCode::LookupReturnsNonNullableType)
15        }) {
16            self.federated_graph = None;
17        }
18        self
19    }
20    /// Simplify the result data to a yes-no answer: did composition succeed?
21    ///
22    /// `Ok()` contains the [FederatedGraph].
23    /// `Err()` contains all [Diagnostics].
24    pub fn into_result(self) -> Result<FederatedGraph, Diagnostics> {
25        if let Some(federated_graph) = self.federated_graph {
26            Ok(federated_graph)
27        } else {
28            // means a fatal error occured
29            Err(self.diagnostics)
30        }
31    }
32
33    /// Composition warnings and errors.
34    pub fn diagnostics(&self) -> &Diagnostics {
35        &self.diagnostics
36    }
37}