Async-graphql: Interface "downcasting" or "specialization"

Created on 30 Oct 2020  路  4Comments  路  Source: async-graphql/async-graphql

In GraphQL i can have schema like this

interface Code {
    number: String!
}

type Barcode implements Code {
    number: String!
}

type Qrcode implements Code {
    number: String!
}

interface Article {
    number: Code!    
}

type Book implements Article {
    number: Barcode!
}

That means, Book can choose which implementor of interface Code will be returned when its number field is queried.
Is it possible to tell async-graphql somehow which one to return? With the following code:

#[derive(Interface)]
#[graphql(
    field(
        desc = "The code represented as a number.",
        name = "number",
        type = "String"
    )
)]
pub enum Code {
    Barode(Barcode)
    Qrcode(Qrcode)
}

pub struct Barcode{}

#[Object]
impl Barcode{
    pub fn number(&self) -> String {
        todo!()
    }
}

pub struct Qrcode{}

#[Object]
impl Qrcode{
    pub fn number(&self) -> String {
        todo!()
    }
}

#[derive(Interface)]
#[graphql(
    field(
        desc = "The article number.",
        name = "number",
        type = "Code"  // type = "Barcode" does not work. Compiler complains that "Code" is expected
    )
)]
pub enum Article {
    Book(Book)
}

pub struct Book {}

#[Object]
impl Book {
    pub fn number(&self) -> Article {
        todo!()
    }
}

the generated SDL for Book looks like this

type Book implements Article {
    number: Code!
}

Is it possible to tell async-graphql to return a Barcode instead of Code?

question

Most helpful comment

I reconsidered it and made changes to the master branch.
Please help to see if the problem has been resolved.

Here is the test code:

https://github.com/async-graphql/async-graphql/blob/d4e26192cea2f074021242c1468ca327bf87cadd/tests/interface.rs#L402

All 4 comments

Since async-graphql implements the interface through call forwarding, this cannot be supported. If we adopt the #312 mentioned, this problem will be solved.

Ok, thanks for clarifying.

I reconsidered it and made changes to the master branch.
Please help to see if the problem has been resolved.

Here is the test code:

https://github.com/async-graphql/async-graphql/blob/d4e26192cea2f074021242c1468ca327bf87cadd/tests/interface.rs#L402

Great! Works like a charm!

Was this page helpful?
0 / 5 - 0 ratings