apollo_federation/supergraph/
subgraph.rs

1use std::collections::BTreeMap;
2use std::fmt;
3use std::sync::Arc;
4
5use apollo_compiler::Name;
6
7use crate::error::FederationError;
8use crate::error::SingleFederationError;
9use crate::schema::FederationSchema;
10use crate::schema::ValidFederationSchema;
11
12pub(super) struct FederationSubgraph {
13    pub(super) name: String,
14    pub(super) url: String,
15    pub(super) schema: FederationSchema,
16    pub(super) graph_enum_value: Name,
17}
18
19pub(super) struct FederationSubgraphs {
20    pub(super) subgraphs: BTreeMap<String, FederationSubgraph>,
21}
22
23impl FederationSubgraphs {
24    pub(super) fn new() -> Self {
25        FederationSubgraphs {
26            subgraphs: BTreeMap::new(),
27        }
28    }
29
30    pub(super) fn add(&mut self, subgraph: FederationSubgraph) -> Result<(), FederationError> {
31        if self.subgraphs.contains_key(&subgraph.name) {
32            return Err(SingleFederationError::InvalidFederationSupergraph {
33                message: format!("A subgraph named \"{}\" already exists", subgraph.name),
34            }
35            .into());
36        }
37        self.subgraphs.insert(subgraph.name.clone(), subgraph);
38        Ok(())
39    }
40
41    pub(super) fn get_mut(&mut self, name: &str) -> Option<&mut FederationSubgraph> {
42        self.subgraphs.get_mut(name)
43    }
44}
45
46impl IntoIterator for FederationSubgraphs {
47    type Item = <BTreeMap<String, FederationSubgraph> as IntoIterator>::Item;
48    type IntoIter = <BTreeMap<String, FederationSubgraph> as IntoIterator>::IntoIter;
49
50    fn into_iter(self) -> Self::IntoIter {
51        self.subgraphs.into_iter()
52    }
53}
54
55// TODO(@goto-bus-stop): consider an appropriate name for this in the public API
56// TODO(@goto-bus-stop): should this exist separately from the `crate::subgraph::Subgraph` type?
57#[derive(Debug, Clone)]
58pub struct ValidFederationSubgraph {
59    pub name: String,
60    pub url: String,
61    pub schema: ValidFederationSchema,
62}
63
64pub struct ValidFederationSubgraphs {
65    pub subgraphs: BTreeMap<Arc<str>, ValidFederationSubgraph>,
66}
67
68impl fmt::Debug for ValidFederationSubgraphs {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        f.write_str("ValidFederationSubgraphs ")?;
71        f.debug_map().entries(self.subgraphs.iter()).finish()
72    }
73}
74
75impl ValidFederationSubgraphs {
76    pub(crate) fn new() -> Self {
77        ValidFederationSubgraphs {
78            subgraphs: BTreeMap::new(),
79        }
80    }
81
82    pub(crate) fn add(&mut self, subgraph: ValidFederationSubgraph) -> Result<(), FederationError> {
83        if self.subgraphs.contains_key(subgraph.name.as_str()) {
84            return Err(SingleFederationError::InvalidFederationSupergraph {
85                message: format!("A subgraph named \"{}\" already exists", subgraph.name),
86            }
87            .into());
88        }
89        self.subgraphs
90            .insert(subgraph.name.as_str().into(), subgraph);
91        Ok(())
92    }
93
94    pub fn get(&self, name: &str) -> Option<&ValidFederationSubgraph> {
95        self.subgraphs.get(name)
96    }
97}
98
99impl IntoIterator for ValidFederationSubgraphs {
100    type Item = <BTreeMap<Arc<str>, ValidFederationSubgraph> as IntoIterator>::Item;
101    type IntoIter = <BTreeMap<Arc<str>, ValidFederationSubgraph> as IntoIterator>::IntoIter;
102
103    fn into_iter(self) -> Self::IntoIter {
104        self.subgraphs.into_iter()
105    }
106}