Add interface to implement another interface. #322

This commit is contained in:
Sunli 2020-10-24 09:08:18 +08:00
parent 5f68861926
commit 33697ea19f
2 changed files with 68 additions and 2 deletions

View File

@ -85,7 +85,7 @@ pub fn generate(interface_args: &args::Interface) -> GeneratorResult<TokenStream
RemoveLifetime.visit_type_path_mut(&mut assert_ty);
type_into_impls.push(quote! {
#crate_name::static_assertions::assert_impl_one!(#assert_ty: #crate_name::ObjectType);
#crate_name::static_assertions::assert_impl_any!(#assert_ty: #crate_name::ObjectType, #crate_name::InterfaceType);
#[allow(clippy::all, clippy::pedantic)]
impl #generics ::std::convert::From<#p> for #ident #generics {
@ -250,7 +250,7 @@ pub fn generate(interface_args: &args::Interface) -> GeneratorResult<TokenStream
methods.push(quote! {
#[inline]
async fn #method_name<'ctx>(&self, #(#decl_params),*) -> #crate_name::Result<#ty> {
pub async fn #method_name<'ctx>(&self, #(#decl_params),*) -> #crate_name::Result<#ty> {
match self {
#(#calls,)*
}

View File

@ -331,3 +331,69 @@ pub async fn test_interface_field_method() {
})
);
}
#[async_std::test]
pub async fn test_interface_implement_other_interface() {
#[derive(Interface)]
#[graphql(field(name = "id", type = "ID"))]
pub enum Entity {
Company(Company),
Organization(Organization),
}
#[derive(Interface)]
#[graphql(field(name = "id", type = "ID"))]
pub enum Node {
Entity(Entity),
}
pub struct Company {}
#[Object]
impl Company {
pub async fn id(&self) -> ID {
"88".into()
}
}
pub struct Organization {}
#[Object]
impl Organization {
pub async fn id(&self) -> ID {
"99".into()
}
}
struct Query;
#[Object]
impl Query {
async fn company(&self) -> Node {
Entity::Company(Company {}).into()
}
async fn organization(&self) -> Node {
Entity::Organization(Organization {}).into()
}
}
let query = r#"
{
company { id }
organization { id }
}
"#;
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
assert_eq!(
schema.execute(query).await.into_result().unwrap().data,
value!({
"company": {
"id": "88",
},
"organization": {
"id": "99",
}
})
);
}