Как добавить поле в структуру GraphQL типа GraphQL Union? (Ржавчина перечисляет и можжевельник)

Я скопировал простой пример GraphQL Union из Juniper GitBook.

use juniper::{graphql_union, GraphQLObject};

// I copied the below from the Juniper Gitbook

#[derive(GraphQLObject)]
struct Human {
    id: String,
    home_planet: String,
}

#[derive(GraphQLObject)]
struct Droid {
    id: String,
    primary_function: String,
}

enum Character {
    Human(Human),
    Droid(Droid),
}

graphql_union!(Character: () where Scalar = <S> |&self| {
    instance_resolvers: |_| {
        &Human => match *self { Character::Human(ref h) => Some(h), _ => None },
        &Droid => match *self { Character::Droid(ref d) => Some(d), _ => None },
    }
});

И это нормально работало. Но затем я попытался использовать свое новое поле внутри структуры GraphQLObject.



// I added this type myself

#[derive(GraphQLObject)]
struct Movie {
    protagonist: Character,
    title: String,
}

Это дало мне странную ошибку:

error[E0277]: the trait bound `Character: juniper::types::base::GraphQLType<__S>` is not satisfied
  --> src/main.rs:24:10
   |
24 | #[derive(GraphQLObject)]
   |          ^^^^^^^^^^^^^ the trait `juniper::types::base::GraphQLType<__S>` is not implemented for `Character`
   |
   = help: consider adding a `where Character: juniper::types::base::GraphQLType<__S>` bound
   = note: required because of the requirements on the impl of `juniper::types::base::GraphQLType<__S>` for `&Character`
   = note: required because of the requirements on the impl of `juniper::executor::IntoResolvable<'_, __S, &Character, _>` for `&Character`
   = note: required by `juniper::executor::IntoResolvable::into`

Понятия не имею, что происходит. Я знаю, что синтаксис GraphQLObject для моей структуры Movie верен.


person Adam    schedule 18.08.2019    source источник


Ответы (1)


Вам нужно добавить where Scalar = <S> к вашему graphql_union! определению:

graphql_union!(Character: () where Scalar = <S> |&self| {
    instance_resolvers: |_| {
        &Human => match *self { Character::Human(ref h) => Some(h), _ => None },
        &Droid => match *self { Character::Droid(ref d) => Some(d), _ => None },
    }
});

Это исправляет ошибку. Я нашел это в последней версии мастер исходного кода Gitbook. Возможно, выпущенные в настоящее время документы устарели.

person Adam    schedule 18.08.2019