diff --git a/src/registry/export_sdl.rs b/src/registry/export_sdl.rs index 2adf2596..d8ad4f26 100644 --- a/src/registry/export_sdl.rs +++ b/src/registry/export_sdl.rs @@ -111,7 +111,14 @@ impl Registry { writeln!(sdl).ok(); } - if !options.federation { + if options.federation { + if self.enable_apollo_link { + writeln!("extend schema @link(").ok(); + writelin!("\turl: \"https://specs.apollo.dev/federation/v2.0\",").ok(); + writeln!("\timport: [\"@key\", \"@tag\", \"@shareable\", \"@inaccessible\", \"@override\", \"@external\", \"@provides\", \"@requires\"]").ok(); + writeln!(")").ok(); + } + } else { writeln!(sdl, "schema {{").ok(); writeln!(sdl, "\tquery: {}", self.query_type).ok(); if let Some(mutation_type) = self.mutation_type.as_deref() { diff --git a/src/registry/mod.rs b/src/registry/mod.rs index cf67b893..88abf48a 100644 --- a/src/registry/mod.rs +++ b/src/registry/mod.rs @@ -413,6 +413,11 @@ pub struct MetaDirective { pub visible: Option, } +#[derive(Debug, Clone)] +pub struct ApolloLinkConfig { + pub extend_schema: bool, +} + #[derive(Default)] pub struct Registry { pub types: BTreeMap, @@ -423,6 +428,7 @@ pub struct Registry { pub subscription_type: Option, pub introspection_mode: IntrospectionMode, pub enable_federation: bool, + pub enable_apollo_link: bool, pub federation_subscription: bool, pub ignore_name_conflicts: HashSet, } diff --git a/src/schema.rs b/src/schema.rs index 39769ae3..f17b6de0 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -30,6 +30,7 @@ use crate::{ InputType, ObjectType, OutputType, QueryEnv, Request, Response, ServerError, ServerResult, SubscriptionType, Variables, ID, }; +use crate::registry::ApolloLinkConfig; /// Introspection mode #[derive(Debug, Copy, Clone, PartialEq, Eq)] @@ -173,6 +174,15 @@ impl SchemaBuilder self } + /// Enables printing the apollo federation 2 `@link` directive during federation schema export; + /// the directive is attached to an "extend schema" element, and will have values set to ensure that + /// the federation schema directives and types are named properly. + #[must_use] + pub fn enable_apollo_fed2_link(mut self) -> Self { + self.registry.enable_apollo_link = true; + self + } + /// Make the Federation SDL include subscriptions. /// /// Note: Not included by default, in order to be compatible with Apollo