rust trait绑定`ObjectType`不满足,为什么?此代码来自docs

9jyewag0  于 2023-11-19  发布在  其他
关注(0)|答案(1)|浏览(79)

使用async-graphql和下面的代码(from their docs):

use async_graphql::{
    extensions::Tracing, EmptyMutation, EmptySubscription, MergedObject, Object, Schema,
};

pub type GraphqlSchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>;

pub fn create_schema() -> GraphqlSchema {
    Schema::build(QueryRoot, EmptyMutation, EmptySubscription)
        .extension(Tracing)
        .finish()
}

#[derive(MergedObject, Default)]
struct QueryRoot(UserQuery, MovieQuery);

#[derive(Default)]
struct UserQuery;

#[Object]
impl UserQuery {
    async fn users(&self) -> Vec<u64> {
        todo!()
    }
}

#[derive(Default)]
struct MovieQuery;

#[Object]
impl MovieQuery {
    async fn movies(&self) -> Vec<u64> {
        todo!()
    }
}

字符串
我不明白这个错误:

error[E0277]: the trait bound `fn(UserQuery, MovieQuery) -> QueryRoot {QueryRoot}: async_graphql::ObjectType` is not satisfied
   --> src\graphql\src\lib.rs:8:19
    |
8   |     Schema::build(QueryRoot, EmptyMutation, EmptySubscription)
    |     ------------- ^^^^^^^^^ the trait `async_graphql::ObjectType` is not implemented for `fn(UserQuery, MovieQuery) -> QueryRoot {QueryRoot}`
    |     |
    |     required by a bound introduced by this call
    |
    = help: the following other types implement trait `async_graphql::ObjectType`:
              &'impl0 T
              Arc<T>
              Box<T>
              Connection<Cursor, Node, ConnectionFields, EdgeFields, Name, EdgeName>
              Edge<Cursor, Node, EdgeFields, Name>
              EmptyFields
              EmptyMutation
              MergedObjectTail
            and 12 others
note: required by a bound in `async_graphql::Schema::<Query, Mutation, Subscription>::build`
   --> C:\Users\Fred\.cargo\registry\src\github.com-1ecc6299db9ec823\async-graphql-4.0.13\src\schema.rs:315:12
    |
315 |     Query: ObjectType + 'static,
    |            ^^^^^^^^^^ required by this bound in `async_graphql::Schema::<Query, Mutation, Subscription>::build`

error[E0308]: mismatched types
  --> src\internal\graphql\src\lib.rs:8:5
   |
7  |   pub fn create_schema() -> GraphqlSchema {
   |                             ------------- expected `async_graphql::Schema<QueryRoot, EmptyMutation, EmptySubscription>` because of return type
8  | /     Schema::build(QueryRoot, EmptyMutation, EmptySubscription)
9  | |         .extension(Tracing)
10 | |         .finish()
   | |_________________^ expected struct `QueryRoot`, found fn item
   |
   = note: expected struct `async_graphql::Schema<QueryRoot, _, _>`
              found struct `async_graphql::Schema<fn(UserQuery, MovieQuery) -> QueryRoot {QueryRoot}, _, _>`

Some errors have detailed explanations: E0277, E0308.
async-graphql = { version = "4.0.13", default-features = false, features = [
  "tracing",
] }

的数据

yc0p9oo0

yc0p9oo01#

你需要传递一个QueryRoot的示例。如果子结构中没有字段,它可以这样构造:

let instance = QueryRoot(UserQuery, MovieQuery);

字符串
它相当于QueryRoot::default()。最终代码如下所示:

pub fn create_schema() -> GraphqlSchema {
    // ...

    let query = QueryRoot(UserQuery, MovieQuery);
    Schema::build(query, EmptyMutation, EmptySubscription)
        .extension(Tracing)
        .finish()

    // ...
}

相关问题