implement a simple approach to using the link directive

this is probably the simple way to get the link directive in place for
federation 2 support - simply add a flag to the registry, and print out
a hardcoded link directive configured for the current needs of
async-graphql.
This commit is contained in:
aidan coyne 2022-09-04 11:04:12 -05:00
parent 52d3e50af5
commit 3c6076a469
3 changed files with 24 additions and 1 deletions

View File

@ -111,7 +111,14 @@ impl Registry {
writeln!(sdl).ok(); 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, "schema {{").ok();
writeln!(sdl, "\tquery: {}", self.query_type).ok(); writeln!(sdl, "\tquery: {}", self.query_type).ok();
if let Some(mutation_type) = self.mutation_type.as_deref() { if let Some(mutation_type) = self.mutation_type.as_deref() {

View File

@ -413,6 +413,11 @@ pub struct MetaDirective {
pub visible: Option<MetaVisibleFn>, pub visible: Option<MetaVisibleFn>,
} }
#[derive(Debug, Clone)]
pub struct ApolloLinkConfig {
pub extend_schema: bool,
}
#[derive(Default)] #[derive(Default)]
pub struct Registry { pub struct Registry {
pub types: BTreeMap<String, MetaType>, pub types: BTreeMap<String, MetaType>,
@ -423,6 +428,7 @@ pub struct Registry {
pub subscription_type: Option<String>, pub subscription_type: Option<String>,
pub introspection_mode: IntrospectionMode, pub introspection_mode: IntrospectionMode,
pub enable_federation: bool, pub enable_federation: bool,
pub enable_apollo_link: bool,
pub federation_subscription: bool, pub federation_subscription: bool,
pub ignore_name_conflicts: HashSet<String>, pub ignore_name_conflicts: HashSet<String>,
} }

View File

@ -30,6 +30,7 @@ use crate::{
InputType, ObjectType, OutputType, QueryEnv, Request, Response, ServerError, ServerResult, InputType, ObjectType, OutputType, QueryEnv, Request, Response, ServerError, ServerResult,
SubscriptionType, Variables, ID, SubscriptionType, Variables, ID,
}; };
use crate::registry::ApolloLinkConfig;
/// Introspection mode /// Introspection mode
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
@ -173,6 +174,15 @@ impl<Query, Mutation, Subscription> SchemaBuilder<Query, Mutation, Subscription>
self 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. /// Make the Federation SDL include subscriptions.
/// ///
/// Note: Not included by default, in order to be compatible with Apollo /// Note: Not included by default, in order to be compatible with Apollo