Juniper: Unions example from book doesn't compile

Created on 14 Jan 2019  路  5Comments  路  Source: graphql-rust/juniper

I noticed the code from the section in the book about unions don't compile. Not sure what I'm doing wrong 馃

Here is an example:

#[macro_use]
extern crate juniper;

use juniper::{EmptyMutation, FieldResult, Variables};

struct Context;
impl juniper::Context for Context {}

struct Query;

#[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: () |&self| {
    instance_resolvers: |_| {
        &Human => match *self { Character::Human(ref h) => Some(h), _ => None },
        &Droid => match *self { Character::Droid(ref d) => Some(d), _ => None },
    }
});

fn main() {}

Error I'm getting

$ cargo build
   Compiling juniper v0.11.1
warning: unused imports: `EmptyMutation`, `FieldResult`, `Variables`
 --> src/main.rs:4:15
  |
4 | use juniper::{EmptyMutation, FieldResult, Variables};
  |               ^^^^^^^^^^^^^  ^^^^^^^^^^^  ^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

error[E0282]: type annotations needed
  --> src/main.rs:28:1
   |
28 | / graphql_union!(Character: () |&self| {
29 | |     instance_resolvers: |_| {
30 | |         &Human => match *self { Character::Human(ref h) => Some(h), _ => None },
31 | |         &Droid => match *self { Character::Droid(ref d) => Some(d), _ => None },
32 | |     }
33 | | });
   | |___^ cannot infer type
   |
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.
error: Could not compile `juniper-from-schema-test`.

To learn more, run the command again with --verbose.

I would also expect Character: () should be Character: Context in graphql_union!. But that gives an error saying it expected ().

Juniper version is 0.11.1

bug docs regression

All 5 comments

I would also expect Character: () should be Character: Context in graphql_union!. But that gives an error saying it expected ()

Context is per-type, so you don't need to specify the context for unions.

I think this problem is related to the new custom scalar support.
I will try to find a solution.

/cc @weiznich

This works:

#[macro_use]
extern crate juniper;

use juniper::{EmptyMutation, FieldResult, Variables};

struct Context;
impl juniper::Context for Context {}

struct Query;

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

fn main() {}

Note the where Scalar = <S>.

Keeping this open as we should probably synthesize that in the macro if it isn't entered? FWIW I raised this in https://github.com/graphql-rust/juniper/pull/251#discussion_r218178702 and I believed the answer in https://github.com/graphql-rust/juniper/pull/251#discussion_r220489994 was sufficient.

I remember having issues around generic defaults in the past.
I'll dig into the macro code to see what's happening.

As mentioned in the other issue, the reason why where Scalar = <S> became mandatory was a change to the custom derives to be mostly generic over the scalar by default.

I think this is reasonable and we can leave it this way.

We can somewhat improve the situation with the new proc macros though.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hrbigelow picture hrbigelow  路  5Comments

tomhoule picture tomhoule  路  4Comments

thedodd picture thedodd  路  5Comments

spacemeowx2 picture spacemeowx2  路  5Comments

jadedevin13 picture jadedevin13  路  5Comments