graphql_composition/
result.rs

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