Skip to content

Commit

Permalink
Second half of updating federation-next to use `apollo-compiler@1.0…
Browse files Browse the repository at this point in the history
….0-beta.6` (apollographql/federation-next#86)

This PR is the second half of apollographql/federation-next#85 , and fully updates `federation-next` to work with `apollo-compiler@1.0.0-beta.6`.

Some notes:
- This is mainly replacing `&str` with `Name`, and converting some functions to use `Result`s.
- The other notable changes are that types (e.g. `ObjectType`) need a `name` field now (making it consistent with the rest of the schema elements in `apollo-rs`), and some minor renames.
- Clippy warns that since `NodeStr` doesn't have `#[derive(PartialEq, Eq)]`, then we can't use `Name` in pattern-matching; I've worked around it with an enum and map for now.
- Consolidated a bit of the logic around converting subgraph names to enum values, with a `TODO` around doing what the JS codebase does.
- Unrelated, but I've been told we had some usages of `.eq()` in the Rust code, so I've converted them to `==`.
  • Loading branch information
sachindshinde authored Nov 13, 2023
1 parent 79de712 commit c3565ae
Show file tree
Hide file tree
Showing 7 changed files with 488 additions and 322 deletions.
2 changes: 2 additions & 0 deletions apollo-federation/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ impl SingleFederationError {
}
}

// TODO: Once InvalidNameError includes the invalid name in the error, we can replace this with an
// implementation for From<InvalidNameError>.
pub(crate) fn graphql_name(name: &str) -> Result<Name, FederationError> {
Name::new(name).map_err(|_| {
SingleFederationError::InvalidGraphQL {
Expand Down
8 changes: 4 additions & 4 deletions apollo-federation/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(dead_code)] // TODO: This is fine while we're iterating, but should be removed later.
use apollo_compiler::ast::Directives;
use apollo_compiler::ast::DirectiveList;
use apollo_compiler::schema::ExtendedType;
use apollo_compiler::Schema;

Expand Down Expand Up @@ -60,7 +60,7 @@ impl Supergraph {
&& !graphql_type
.directives()
.iter()
.any(|d| d.name.eq("inaccessible"))
.any(|d| d.name == "inaccessible")
});
// remove directive applications
for (_, graphql_type) in api_schema.types.iter_mut() {
Expand Down Expand Up @@ -146,8 +146,8 @@ fn is_join_type(type_name: &str) -> bool {
JOIN_TYPES.contains(&type_name)
}

fn is_inaccessible_applied(directives: &Directives) -> bool {
directives.iter().any(|d| d.name.eq("inaccessible"))
fn is_inaccessible_applied(directives: &DirectiveList) -> bool {
directives.iter().any(|d| d.name == "inaccessible")
}

#[cfg(test)]
Expand Down
3 changes: 2 additions & 1 deletion apollo-federation/src/link/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ fn parse_link_if_bootstrap_directive(schema: &Schema, directive: &Directive) ->
.and_then(|value| value.as_str())
{
let url = url.parse::<Url>();
let default_link_name = DEFAULT_LINK_NAME;
let expected_name = directive
.argument_by_name("as")
.and_then(|value| value.as_str())
.unwrap_or(DEFAULT_LINK_NAME);
.unwrap_or(default_link_name.as_str());
return url.map_or(false, |url| {
url.identity == Identity::link_identity() && directive.name == expected_name
});
Expand Down
17 changes: 14 additions & 3 deletions apollo-federation/src/link/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::link::spec::Identity;
use crate::link::spec::Url;
use crate::link::spec_definition::spec_definitions;
use apollo_compiler::ast::{Directive, Value};
use apollo_compiler::name;
use apollo_compiler::schema::Name;
use std::fmt;
use std::ops::Deref;
use std::str;
Expand All @@ -18,9 +20,9 @@ pub(crate) mod link_spec_definition;
pub mod spec;
pub(crate) mod spec_definition;

pub const DEFAULT_LINK_NAME: &str = "link";
pub const DEFAULT_IMPORT_SCALAR_NAME: &str = "Import";
pub const DEFAULT_PURPOSE_ENUM_NAME: &str = "Purpose";
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");

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

impl From<&Purpose> for Name {
fn from(value: &Purpose) -> Self {
match value {
Purpose::SECURITY => name!("SECURITY"),
Purpose::EXECUTION => name!("EXECUTION"),
}
}
}

#[derive(Eq, PartialEq, Debug)]
pub struct Import {
/// The name of the element that is being imported.
Expand Down
Loading

0 comments on commit c3565ae

Please sign in to comment.