diff --git a/tests/federation.rs b/tests/federation.rs index 8b36594c..0acfa38d 100644 --- a/tests/federation.rs +++ b/tests/federation.rs @@ -547,6 +547,86 @@ pub async fn test_entity_inaccessible() { } } +#[tokio::test] +pub async fn test_link_directive() { + struct User { + id: ID, + } + + #[Object(extends)] + impl User { + #[graphql(external)] + async fn id(&self) -> &ID { + &self.id + } + + async fn reviews(&self) -> Vec { + todo!() + } + } + + struct Review; + + #[Object] + impl Review { + async fn body(&self) -> String { + todo!() + } + + async fn author(&self) -> User { + todo!() + } + + async fn product(&self) -> Product { + todo!() + } + } + + struct Product { + upc: String, + } + + #[Object(extends)] + impl Product { + #[graphql(external)] + async fn upc(&self) -> &str { + &self.upc + } + + async fn reviews(&self) -> Vec { + todo!() + } + } + + struct Query; + + #[Object] + impl Query { + #[graphql(entity)] + async fn find_user_by_id(&self, id: ID) -> User { + User { id } + } + + #[graphql(entity)] + async fn find_product_by_upc(&self, upc: String) -> Product { + Product { upc } + } + } + + let schema_sdl = Schema::build(Query, EmptyMutation, EmptySubscription) + .enable_apollo_fed2_link() + .finish() + .sdl_with_options(SDLExportOptions::new().federation()); + + let path = std::path::Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) + .join("tests/schemas/test_fed2_link.schema.graphqls"); + let expected_schema = std::fs::read_to_string(&path).unwrap(); + if schema_sdl != expected_schema { + std::fs::write(path, schema_sdl).unwrap(); + panic!("schema was not up-to-date. verify changes and re-run if correct.") + } +} + #[tokio::test] pub async fn test_entity_tag() { struct MyCustomObjTagged; diff --git a/tests/schemas/test_fed2_link.schema.graphqls b/tests/schemas/test_fed2_link.schema.graphqls new file mode 100644 index 00000000..dfb4a959 --- /dev/null +++ b/tests/schemas/test_fed2_link.schema.graphqls @@ -0,0 +1,26 @@ + + + + +extend type Product @key(fields: "upc") { + upc: String! @external + reviews: [Review!]! +} + + +type Review { + body: String! + author: User! + product: Product! +} + + +extend type User @key(fields: "id") { + id: ID! @external + reviews: [Review!]! +} + +extend schema @link( + url: "https://specs.apollo.dev/federation/v2.0", + import: ["@key", "@tag", "@shareable", "@inaccessible", "@override", "@external", "@provides", "@requires"] +)