Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions apollo-federation/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ pub enum CompositionError {
MaxValidationSubgraphPathsExceeded { message: String },
#[error("{message}")]
InternalError { message: String },
#[error("{message}")]
LinkImportNameMismatch { message: String },
}

impl CompositionError {
Expand All @@ -176,6 +178,7 @@ impl CompositionError {
ErrorCode::MaxValidationSubgraphPathsExceeded
}
Self::InternalError { .. } => ErrorCode::Internal,
Self::LinkImportNameMismatch { .. } => ErrorCode::LinkImportNameMismatch,
}
}

Expand Down Expand Up @@ -218,6 +221,9 @@ impl CompositionError {
Self::InternalError { message } => Self::InternalError {
message: format!("{message}{appendix}"),
},
Self::LinkImportNameMismatch { message } => Self::LinkImportNameMismatch {
message: format!("{message}{appendix}"),
},
// Remaining errors do not have an obvious way to appending a message, so we just return self.
Self::SubgraphError { .. }
| Self::InvalidGraphQLName(..)
Expand Down Expand Up @@ -462,8 +468,6 @@ pub enum SingleFederationError {
#[error("{message}")]
InvalidLinkIdentifier { message: String },
#[error("{message}")]
LinkImportNameMismatch { message: String },
#[error("{message}")]
ReferencedInaccessible { message: String },
#[error("{message}")]
DefaultValueUsesInaccessible { message: String },
Expand Down Expand Up @@ -676,9 +680,6 @@ impl SingleFederationError {
ErrorCode::InvalidLinkDirectiveUsage
}
SingleFederationError::InvalidLinkIdentifier { .. } => ErrorCode::InvalidLinkIdentifier,
SingleFederationError::LinkImportNameMismatch { .. } => {
ErrorCode::LinkImportNameMismatch
}
SingleFederationError::ReferencedInaccessible { .. } => {
ErrorCode::ReferencedInaccessible
}
Expand Down
60 changes: 60 additions & 0 deletions apollo-federation/src/link/link_spec_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use apollo_compiler::ast::Argument;
use apollo_compiler::ast::Directive;
use apollo_compiler::ast::DirectiveLocation;
use apollo_compiler::ast::Type;
use apollo_compiler::ast::Value;
use apollo_compiler::name;
use apollo_compiler::schema::Component;
use apollo_compiler::ty;
Expand Down Expand Up @@ -273,6 +274,65 @@ impl LinkSpecDefinition {
)
}

pub(crate) fn apply_feature_to_schema(
&self,
schema: &mut FederationSchema,
feature: &dyn SpecDefinition,
alias: Option<Name>,
purpose: Option<Purpose>,
imports: Option<Vec<Import>>,
) -> Result<(), FederationError> {
let mut directive = Directive::new(self.url.identity.name.clone());
directive.arguments.push(Node::new(Argument {
name: self.url_arg_name(),
value: Node::new(feature.to_string().into()),
}));
if let Some(alias) = alias {
directive.arguments.push(Node::new(Argument {
name: LINK_DIRECTIVE_AS_ARGUMENT_NAME,
value: Node::new(alias.to_string().into()),
}));
}
if let Some(purpose) = purpose {
if self.supports_purpose() {
directive.arguments.push(Node::new(Argument {
name: LINK_DIRECTIVE_FOR_ARGUMENT_NAME,
value: Node::new(purpose.to_string().into()),
}));
} else {
return Err(SingleFederationError::InvalidLinkDirectiveUsage {
message: format!(
"Cannot apply feature {} with purpose since the schema's @core/@link version does not support it.", feature.to_string()
),
}.into());
}
}
if let Some(imports) = imports {
if !imports.is_empty() {
if self.supports_import() {
directive.arguments.push(Node::new(Argument {
name: LINK_DIRECTIVE_IMPORT_ARGUMENT_NAME,
value: Node::new(Value::List(
imports.into_iter().map(|i| Node::new(i.into())).collect(),
)),
}))
} else {
return Err(SingleFederationError::InvalidLinkDirectiveUsage {
message: format!(
"Cannot apply feature {} with imports since the schema's @core/@link version does not support it.",
feature.to_string()
),
}.into());
}
}
}

SchemaDefinitionPosition.insert_directive(schema, Component::new(directive))?;
feature.add_elements_to_schema(schema)?;

Ok(())
}

#[allow(unused)]
pub(crate) fn fed1_latest() -> &'static Self {
// Note: The `unwrap()` calls won't panic, since `CORE_VERSIONS` will always have at
Expand Down
40 changes: 40 additions & 0 deletions apollo-federation/src/link/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub(crate) mod tag_spec_definition;
pub const DEFAULT_LINK_NAME: Name = name!("link");
pub const DEFAULT_IMPORT_SCALAR_NAME: Name = name!("Import");
pub const DEFAULT_PURPOSE_ENUM_NAME: Name = name!("Purpose");
pub(crate) const IMPORT_AS_ARGUMENT: Name = name!("as");
pub(crate) const IMPORT_NAME_ARGUMENT: Name = name!("name");

// TODO: we should provide proper "diagnostic" here, linking to ast, accumulating more than one
// error and whatnot.
Expand Down Expand Up @@ -261,6 +263,26 @@ impl fmt::Display for Import {
}
}

#[allow(clippy::from_over_into)]
impl Into<Value> for Import {
fn into(self) -> Value {
if let Some(alias) = self.alias {
Value::Object(vec![
(
IMPORT_NAME_ARGUMENT,
Node::new(Value::String(self.element.to_string())),
),
(
IMPORT_AS_ARGUMENT,
Node::new(Value::String(alias.to_string())),
),
])
} else {
Value::String(self.element.to_string())
}
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Link {
pub url: Url,
Expand Down Expand Up @@ -289,6 +311,24 @@ impl Link {
}
}

pub(crate) fn directive_name_in_schema_for_core_arguments(
spec_url: &Url,
spec_name_in_schema: &Name,
imports: &[Import],
directive_name_in_spec: &Name,
) -> Name {
if let Some(element_import) = imports
.iter()
.find(|i| i.element == *directive_name_in_spec)
{
element_import.imported_name().clone()
} else if spec_url.identity.name == *directive_name_in_spec {
spec_name_in_schema.clone()
} else {
Name::new_unchecked(format!("{spec_name_in_schema}__{directive_name_in_spec}").as_str())
}
}

pub fn type_name_in_schema(&self, name: &Name) -> Name {
// Similar to directives, but the special case of a directive name matching the spec
// name does not apply to types.
Expand Down
11 changes: 10 additions & 1 deletion apollo-federation/src/merger/merge_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ pub(crate) mod tests {

use super::*;
use crate::JOIN_VERSIONS;
use crate::SpecDefinition;
use crate::error::ErrorCode;
use crate::link::federation_spec_definition::FEDERATION_VERSIONS;
use crate::link::link_spec_definition::LINK_VERSIONS;
use crate::link::spec::Version;
use crate::merger::compose_directive_manager::ComposeDirectiveManager;
use crate::merger::error_reporter::ErrorReporter;
use crate::merger::merge::CompositionOptions;
Expand All @@ -283,8 +287,11 @@ pub(crate) mod tests {
// Helper function to create a minimal merger instance for testing
// This only initializes what's needed for merge_enum() testing
pub(crate) fn create_test_merger() -> Result<Merger, FederationError> {
let link_spec_definition = LINK_VERSIONS
.find(&Version { major: 1, minor: 0 })
.expect("LINK_VERSIONS should have version 1.0");
let join_spec_definition = JOIN_VERSIONS
.find(&crate::link::spec::Version { major: 0, minor: 5 })
.find(&Version { major: 0, minor: 5 })
.expect("JOIN_VERSIONS should have version 0.5");

let schema = Schema::builder()
Expand Down Expand Up @@ -350,9 +357,11 @@ pub(crate) mod tests {
fields_with_from_context: Default::default(),
fields_with_override: Default::default(),
inaccessible_directive_name_in_supergraph: None,
link_spec_definition,
join_spec_definition,
join_directive_identities: Default::default(),
schema_to_import_to_feature_url: Default::default(),
latest_federation_version_used: FEDERATION_VERSIONS.latest().version().clone(),
})
}

Expand Down
Loading