Coder Social home page Coder Social logo

Comments (18)

phated avatar phated commented on April 28, 2024 1

Excited to see this! Please let me know if I can help in any way

from async-graphql.

sunli829 avatar sunli829 commented on April 28, 2024 1

I tried it and it worked.

Here is the new interface definition.

#[Interface]
trait Node: Send + Sync {
    async fn id(&self) -> i32;
}

struct MyObj;

#[Implement]
impl Node for MyObj {
    async fn id(&self) -> i32 {
       10
    }
}

#[Object]
impl MyObj {
    async fn value(&self) -> i32 {
        20
    }
}

struct Query;

#[Object]
impl Query {
    async fn my_obj(&self) -> NodeInterface<MyObj> {
         // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
         // NodeInterface is generated automatically, which may cause the auto-completion of the ide to fail.
         MyObj.into()
    }
}

The following is a query that can be executed correctly.

{
    id
    ... on Node {
         id1: id
    }
    ... on MyObj {
         value 
    }
}

from async-graphql.

sunli829 avatar sunli829 commented on April 28, 2024 1

Trait object are the only way to solve this problem, but I don't have a computer right now, so I'll think about it later.

from async-graphql.

phated avatar phated commented on April 28, 2024

Wow! This looks great!!!!!

from async-graphql.

sunli829 avatar sunli829 commented on April 28, 2024

I couldn't implement such a feature because lose the type information if converted to a trait object.😭

#[Object]
impl Query {
    async fn my_obj(&self) -> Vec<NodeInterface<???>> {
         vec![MyObjA.into(), MyObjB.into()]
    }
}

from async-graphql.

nicolaiunrein avatar nicolaiunrein commented on April 28, 2024

I suppose NodeInterface is a struct?
I don’t think this compiles but wouldn’t that be the correct signature?

#[Object]
impl Query {
    async fn my_obj(&self) -> Vec<NodeInterface<dyn Node>> {
         vec![MyObjA.into(), MyObjB.into()]
    }
}

from async-graphql.

sunli829 avatar sunli829 commented on April 28, 2024

I need to think again about whether this generic parameter is really necessary.

from async-graphql.

phated avatar phated commented on April 28, 2024

And we are unable able to do this?

struct Query;

#[Object]
impl Query {
    async fn my_obj(&self) -> impl Node  {
         MyObj{}
    }
}

https://doc.rust-lang.org/edition-guide/rust-2018/trait-system/impl-trait-for-returning-complex-types-with-ease.html

from async-graphql.

sunli829 avatar sunli829 commented on April 28, 2024

No, this is just type MyObj, which is missing the interface information, so need a wrapper.

from async-graphql.

nicolaiunrein avatar nicolaiunrein commented on April 28, 2024

Correct me if I’m wrong but I think under the hood impl Query gets converted to a trait? and returning impl Trait is not supported on trait methods.

But what about utilising the flatten attribute inside the generated Interface somehow to inline existing fields?

from async-graphql.

phated avatar phated commented on April 28, 2024

No, this is just type MyObj, which is missing the interface information, so need a wrapper.

If you impl Node for MyObj, why would it not fulfill the trait implementation type signature?

from async-graphql.

sunli829 avatar sunli829 commented on April 28, 2024

Cannot use a trait object, which is a run-time type, and generate Schema requires static type.
So let's go back to #38, is it worth sacrificing a little type safety for more flexible interface definitions?

from async-graphql.

sunli829 avatar sunli829 commented on April 28, 2024

The generic list should solve all the problems. I'm still thinking about it.

from async-graphql.

sunli829 avatar sunli829 commented on April 28, 2024

This is impossible, so I'm going to close it right now.

from async-graphql.

sunli829 avatar sunli829 commented on April 28, 2024

I think #38 is the best solution, removing many of the limitations of function forwarding and making it easy to use. Panic at startup is acceptable.

from async-graphql.

phated avatar phated commented on April 28, 2024

I discussed this at my presentation and everyone wanted static type checking. It takes a long time to start up the server to see that you made a mistake.

from async-graphql.

sunli829 avatar sunli829 commented on April 28, 2024

Let's keep the original design the same.😂

from async-graphql.

sunli829 avatar sunli829 commented on April 28, 2024

I'll try again.

from async-graphql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.