Skip to content
New issue

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

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

Already on GitHub? # to your account

GraphQLType is not satisfied with juniper::object #467

Closed
jasonlor opened this issue Nov 24, 2019 · 9 comments
Closed

GraphQLType is not satisfied with juniper::object #467

jasonlor opened this issue Nov 24, 2019 · 9 comments
Labels
bug Something isn't working needs-reproduction

Comments

@jasonlor
Copy link

jasonlor commented Nov 24, 2019

Currently using 0.14.1. I'm attempting to implement fields with the juniper::object macro.

#[juniper::object(
    Context = Context,
    Scalar = juniper::DefaultScalarValue,
)]
impl Event {

On compilation, i see the following error:
error[E0277]: the trait bound models::Event: juniper::GraphQLType<__S> is not satisfied

On this struct, I've implemented another trait that does not have the juniper::object macro.
impl TraitName for Event

I've looked through the docs and issues and have been unable to find a solution.

Thanks!

@jasonlor jasonlor added bug Something isn't working needs-triage labels Nov 24, 2019
@davidpdrsn
Copy link
Contributor

Are you able to share a minimal reproduction script? It’s a bit hard to say what’s wrong from what you posted.

@andy128k
Copy link
Contributor

andy128k commented Dec 3, 2019

I have found similar issue. Here is minimal example which fails to build.

@jasonlor
Copy link
Author

jasonlor commented Jan 6, 2020

Bump, any ideas?

@RyuuGan
Copy link

RyuuGan commented Apr 12, 2020

Hello,

Any updates on this ?

#[derive(GraphQLObject)]
          ^^^^^^^^^^^^^ the trait `juniper::types::base::GraphQLType<__S>` is not implemented for `api::graphql::projects::GQLProject`

I also trying to use it as in @andy128k example

@RyuuGan
Copy link

RyuuGan commented Apr 12, 2020

Hello,

I found a workaround, so it will compile and work as expected. The idea is that you don't use #[juniper::object] inside the #[derive(GraphQLObject)], but what you can do is define all objects as #[juniper::object]. Yes it is a little bit ugly to define all the methods, but at least it works.

So instead of:

pub struct Article {
    pub title: String,
}

#[juniper::object(name = "Article")]
impl Article {
    fn title(&self) -> &str { &self.title }
}

#[derive(juniper::GraphQLObject)]
pub struct Blog {
    pub url: String,
    pub description: Vec<String>,
    pub articles: Vec<Article>,
}

You define:

pub struct Article {
    pub title: String,
}

#[juniper::object(name = "Article")]
impl Article {
    fn title(&self) -> &str { &self.title }
}

pub struct Blog {
    pub url: String,
    pub description: Vec<String>,
    pub articles: Vec<Article>,
}

#[juniper::object(name = "Blog")]
impl Blog {
  // ...
}

@zhenwenc
Copy link

zhenwenc commented May 4, 2020

@andy128k @RyuuGan Have you tried adding the scalar attribute to derived([GraphQLObject])?

#[derive(juniper::GraphQLObject)]
#[graphql(scalar = juniper::DefaultScalarValue)]
pub struct Blog {
    pub url: String,
    pub description: Vec<String>,
    pub articles: Vec<Article>,
}

For some reason it is required, otherwise generic will be used instead of the DefaultScalarValue:

if self.generic_scalar {
// If generic_scalar is true, we always insert a generic scalar.
// See more comments below.
quote!(__S)
} else {
quote!(#juniper_crate_name::DefaultScalarValue)
}

since generic_scalar has been set to true when deriving GraphQLObject:

generic_scalar: true,

Not sure if this is a bug or by design. 🤔

@urkle
Copy link
Contributor

urkle commented May 31, 2020

I'm running into this issue as well when trying to use juniper::graphql_union! against two object where I used #[juniper::object] on an impl.

pub struct MyObject {
  id: i32,
}

#[juniper::object]
impl MyObject {
  fn id(&self) -> i32 {
    &self.id
  }
}

enum MyUnion {
  MyObject(MyObject),
}

juniper::graphql_union!(MyUnion: () where Scalar = <S> |&self| {
    instance_resolvers: |_| {
        &MyObject => match *self { MyUnion::MyObject(ref h) => Some(h), _ => None },
    }
});

This results with

error[E0277]: the trait bound `MyObject: juniper::types::base::GraphQLType<S>` is not satisfied
  --> src/my_object.rs:8:1
   |
8  | / juniper::graphql_union!(MyUnion: () where Scalar = <S> |&self| {
9  | |     instance_resolvers: |_| {
10 | |         &MyObject => match *self { MyUnion::MyObject(ref h) => Some(h), _ => None },
12 | |     }
13 | | });
   | |___^ the trait `juniper::types::base::GraphQLType<S>` is not implemented for `MyObject`

@tyranron
Copy link
Member

tyranron commented May 31, 2020

@urkle I guess that's because GraphQLType implementtion for MyObject is not generic over ScalarValue, while you're trying to do so for MyUnion.

Try this:

juniper::graphql_union!(MyUnion: () where Scalar = juniper::DefaultScalarValue |&self| {

Also, cargo expand will show more why this happens.

@urkle
Copy link
Contributor

urkle commented May 31, 2020

@tyranron Awesome Thanks! That fixed the issue.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working needs-reproduction
Projects
None yet
Development

No branches or pull requests

8 participants