Skip to content

Commit cefa68d

Browse files
committed
fix: Export directives to federation SDL so they can be composed. Add example of this feature.
1 parent 0d64047 commit cefa68d

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/registry/export_sdl.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,12 @@ impl Registry {
113113

114114
if options.federation {
115115
writeln!(sdl, "extend schema @link(").ok();
116-
writeln!(sdl, "\turl: \"https://specs.apollo.dev/federation/v2.0\",").ok();
116+
writeln!(sdl, "\turl: \"https://specs.apollo.dev/federation/v2.1\",").ok();
117117
writeln!(sdl, "\timport: [\"@key\", \"@tag\", \"@shareable\", \"@inaccessible\", \"@override\", \"@external\", \"@provides\", \"@requires\"]").ok();
118118
writeln!(sdl, ")").ok();
119+
self.directives.values().for_each(|directive| {
120+
writeln!(sdl, "{}", directive.sdl()).ok();
121+
});
119122
} else {
120123
writeln!(sdl, "schema {{").ok();
121124
writeln!(sdl, "\tquery: {}", self.query_type).ok();

src/registry/mod.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod stringify_exec_doc;
44

55
use std::{
66
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
7-
fmt::{self, Display, Formatter},
7+
fmt::{self, Display, Formatter, Write},
88
sync::Arc,
99
};
1010

@@ -596,6 +596,29 @@ pub struct MetaDirective {
596596
pub visible: Option<MetaVisibleFn>,
597597
}
598598

599+
impl MetaDirective {
600+
pub(crate) fn sdl(&self) -> String {
601+
let mut sdl = format!("directive @{}", self.name);
602+
if !self.args.is_empty() {
603+
let args = self
604+
.args
605+
.values()
606+
.map(|value| format!("{}: {}", value.name, value.ty))
607+
.collect::<Vec<_>>()
608+
.join(", ");
609+
write!(sdl, "({})", args).ok();
610+
}
611+
let locations = self
612+
.locations
613+
.iter()
614+
.map(|location| location.to_value().to_string())
615+
.collect::<Vec<_>>()
616+
.join(" | ");
617+
write!(sdl, " on {}", locations).ok();
618+
sdl
619+
}
620+
}
621+
599622
/// A type registry for build schemas
600623
#[derive(Default)]
601624
pub struct Registry {

0 commit comments

Comments
 (0)