|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +use apollo_federation_types::{ |
| 4 | + build::SubgraphDefinition, |
| 5 | + config::{ConfigError, SupergraphConfig}, |
| 6 | +}; |
| 7 | +use derive_getters::Getters; |
| 8 | +use futures::StreamExt; |
| 9 | +use tap::TapFallible; |
| 10 | + |
| 11 | +use super::file::FileWatcher; |
| 12 | +use crate::composition::watchers::subtask::SubtaskHandleUnit; |
| 13 | + |
| 14 | +pub struct SupergraphConfigWatcher { |
| 15 | + file_watcher: FileWatcher, |
| 16 | + supergraph_config: SupergraphConfig, |
| 17 | +} |
| 18 | + |
| 19 | +impl SupergraphConfigWatcher { |
| 20 | + pub fn new( |
| 21 | + file_watcher: FileWatcher, |
| 22 | + supergraph_config: SupergraphConfig, |
| 23 | + ) -> SupergraphConfigWatcher { |
| 24 | + SupergraphConfigWatcher { |
| 25 | + file_watcher, |
| 26 | + supergraph_config, |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl SubtaskHandleUnit for SupergraphConfigWatcher { |
| 32 | + type Output = SupergraphConfigDiff; |
| 33 | + fn handle( |
| 34 | + self, |
| 35 | + sender: tokio::sync::mpsc::UnboundedSender<Self::Output>, |
| 36 | + ) -> tokio::task::AbortHandle { |
| 37 | + tokio::spawn(async move { |
| 38 | + let mut latest_supergraph_config = self.supergraph_config.clone(); |
| 39 | + while let Some(contents) = self.file_watcher.clone().watch().next().await { |
| 40 | + match SupergraphConfig::new_from_yaml(&contents) { |
| 41 | + Ok(supergraph_config) => { |
| 42 | + if let Ok(supergraph_config_diff) = |
| 43 | + SupergraphConfigDiff::new(&latest_supergraph_config, &supergraph_config) |
| 44 | + { |
| 45 | + let _ = sender |
| 46 | + .send(supergraph_config_diff) |
| 47 | + .tap_err(|err| tracing::error!("{:?}", err)); |
| 48 | + } |
| 49 | + latest_supergraph_config = supergraph_config; |
| 50 | + } |
| 51 | + Err(err) => { |
| 52 | + tracing::error!("Could not parse supergraph config file. {:?}", err); |
| 53 | + eprintln!("Could not parse supergraph config file"); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + }) |
| 58 | + .abort_handle() |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +#[derive(Getters)] |
| 63 | +pub struct SupergraphConfigDiff { |
| 64 | + added: Vec<SubgraphDefinition>, |
| 65 | + removed: Vec<String>, |
| 66 | +} |
| 67 | + |
| 68 | +impl SupergraphConfigDiff { |
| 69 | + pub fn new( |
| 70 | + old: &SupergraphConfig, |
| 71 | + new: &SupergraphConfig, |
| 72 | + ) -> Result<SupergraphConfigDiff, ConfigError> { |
| 73 | + let old_subgraph_defs = old.get_subgraph_definitions().tap_err(|err| { |
| 74 | + eprintln!( |
| 75 | + "Error getting subgraph definitions from the current supergraph config: {:?}", |
| 76 | + err |
| 77 | + ) |
| 78 | + })?; |
| 79 | + let old_subgraph_names: HashSet<String> = |
| 80 | + HashSet::from_iter(old_subgraph_defs.iter().map(|def| def.name.to_string())); |
| 81 | + let new_subgraph_defs = new.get_subgraph_definitions().tap_err(|err| { |
| 82 | + eprintln!( |
| 83 | + "Error getting subgraph definitions from the modified supergraph config: {:?}", |
| 84 | + err |
| 85 | + ) |
| 86 | + })?; |
| 87 | + let new_subgraph_names = |
| 88 | + HashSet::from_iter(new_subgraph_defs.iter().map(|def| def.name.to_string())); |
| 89 | + let added_names: HashSet<String> = |
| 90 | + HashSet::from_iter(new_subgraph_names.difference(&old_subgraph_names).cloned()); |
| 91 | + let removed_names = old_subgraph_names.difference(&new_subgraph_names); |
| 92 | + let added = new_subgraph_defs |
| 93 | + .into_iter() |
| 94 | + .filter(|def| added_names.contains(&def.name)) |
| 95 | + .collect::<Vec<_>>(); |
| 96 | + let removed = removed_names.into_iter().cloned().collect::<Vec<_>>(); |
| 97 | + Ok(SupergraphConfigDiff { added, removed }) |
| 98 | + } |
| 99 | +} |
0 commit comments