graphql_composition/
lib.rs

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
#![deny(unsafe_code, missing_docs, rust_2018_idioms)]
#![allow(clippy::option_option)]
#![doc = include_str!("../README.md")]

mod compose;
mod composition_ir;
mod diagnostics;
mod emit_federated_graph;
mod grafbase_extensions;
mod ingest_subgraph;
mod result;
mod subgraphs;
mod validate;

pub use self::{
    diagnostics::Diagnostics, grafbase_extensions::LoadedExtension, result::CompositionResult, subgraphs::IngestError,
    subgraphs::Subgraphs,
};
pub use graphql_federated_graph::{
    self as graphql_federated_graph, render_api_sdl, render_federated_sdl, FederatedGraph,
};

use self::{
    compose::{compose_subgraphs, ComposeContext},
    emit_federated_graph::emit_federated_graph,
    ingest_subgraph::ast_value_to_subgraph_value,
};

/// Compose subgraphs into a federated graph.
pub fn compose(subgraphs: &Subgraphs) -> CompositionResult {
    let mut diagnostics = Diagnostics::default();

    if subgraphs.iter_subgraphs().len() == 0 {
        let error = "No graphs found for composition build. You must have at least one active graph.";
        diagnostics.push_fatal(error.to_owned());

        return CompositionResult {
            federated_graph: None,
            diagnostics,
        };
    }

    let mut context = ComposeContext::new(subgraphs, &mut diagnostics);

    validate::validate(&mut context);

    if context.diagnostics.any_fatal() {
        return CompositionResult {
            federated_graph: None,
            diagnostics,
        };
    }

    compose_subgraphs(&mut context);

    if context.diagnostics.any_fatal() {
        CompositionResult {
            federated_graph: None,
            diagnostics,
        }
    } else {
        let federated_graph = emit_federated_graph(context.into_ir(), subgraphs);

        CompositionResult {
            federated_graph: Some(federated_graph),
            diagnostics,
        }
    }
}

trait VecExt<T> {
    fn push_return_idx(&mut self, elem: T) -> usize;
}

impl<T> VecExt<T> for Vec<T> {
    fn push_return_idx(&mut self, elem: T) -> usize {
        let idx = self.len();
        self.push(elem);
        idx
    }
}

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

    #[test]
    fn grafbase_schema_can_be_composed() {
        use std::{fs, path::Path};
        let schema_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../cli/src/backend/api/graphql/api.graphql");
        let schema = fs::read_to_string(schema_path).unwrap();

        let mut subgraphs = Subgraphs::default();
        subgraphs
            .ingest_str(&schema, "grafbase-api", Some("https://api.grafbase.com"))
            .unwrap();
        let result = compose(&subgraphs);
        assert!(!result.diagnostics().any_fatal());
    }
}