1

I copied the straightforward GraphQL Union example from the 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 },
    }
});

And this worked fine. But then I tried to use my new field inside a GraphQLObject struct.



// I added this type myself

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

This gave me a weird error:

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`

I have no idea what's going on. I know the GraphQLObject syntax for my Movie struct is right.

Adam
  • 482
  • 1
  • 4
  • 12

1 Answers1

0

You need to add where Scalar = <S> to your graphql_union! definition:

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 },
    }
});

This fixes the error. I found this on the latest master of the Gitbook's source. Maybe the currently-released docs are out-of-date.

Adam
  • 482
  • 1
  • 4
  • 12