Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unions example from book doesn't compile #315

Closed
davidpdrsn opened this issue Jan 14, 2019 · 5 comments
Closed

Unions example from book doesn't compile #315

davidpdrsn opened this issue Jan 14, 2019 · 5 comments
Assignees
Labels
bug Something isn't working k::documentation Related to project documentation regression

Comments

@davidpdrsn
Copy link
Contributor

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

@theduke
Copy link
Member

theduke commented Jan 14, 2019

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.

@LegNeato
Copy link
Member

/cc @weiznich

@LegNeato
Copy link
Member

LegNeato commented Jan 15, 2019

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 #251 (comment) and I believed the answer in #251 (comment) was sufficient.

@LegNeato LegNeato added the k::documentation Related to project documentation label Jan 15, 2019
@theduke
Copy link
Member

theduke commented Jan 15, 2019

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

@theduke
Copy link
Member

theduke commented May 7, 2019

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.

@theduke theduke closed this as completed May 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working k::documentation Related to project documentation regression
Projects
None yet
Development

No branches or pull requests

3 participants