weiznich / wundergraph

Apache License 2.0
214 stars 13 forks source link

LoadingHandler not implemented #38

Closed andrejohansson closed 4 years ago

andrejohansson commented 4 years ago

Hi,

I'm trying to get your examples running, but for some reason it seems like the macros doesnt get applied and extends my types. Perhaps you see directly what I'm doing wrong/missing?

The only thing I have done different from your example is to break apart the content in different files/modules. But the have correct (what I think) imports etc between them.

Tried putting everything in the same file but still got the same error...

The error

error[E0277]: the trait bound `person::model::Person: wundergraph::query_builder::selection::LoadingHandler<_, _>` is not satisfied
  --> src\main.rs:45:18
   |
45 |     let schema = Schema::new(query, mutation);
   |                  ^^^^^^^^^^^ the trait `wundergraph::query_builder::selection::LoadingHandler<_, _>` is not implemented for `person::model::Person`
   |
   = note: required because of the requirements on the impl of `juniper::types::base::GraphQLType<wundergraph::scalar::WundergraphScalarValue>` for `graphql::query::Query<_>`
   = note: required by `juniper::schema::model::RootNode````

My type implementation

use diesel_derive_enum::DbEnum;
use serde::Serialize;
use wundergraph::prelude::*;
use crate::address::model::Address;
use crate::db::schema::*;

#[derive(Clone, Debug, Identifiable, Queryable, WundergraphEntity)]
pub struct Person {
    pub id: i32,
    pub firstname: String,
    pub lastname: Option<String>,
    pub year_of_birth: Option<i32>,
    pub address: HasOne<i32, Address>,
}

My diesel type

table! {
    persons (id) {
        id -> Int4,
        firstname -> Varchar,
        lastname -> Nullable<Varchar>,
        year_of_birth -> Nullable<Int4>,
        address_id -> Nullable<Int4>,
    }
}

QueryModifier

impl<T, C, DB> QueryModifier<T, DB> for MyContext<C>
where
    C: Connection<Backend = DB>,
    DB: Backend + ApplyOffset + 'static,
    T: LoadingHandler<DB, Self>,
    Self: WundergraphContext,
    Self::Connection: Connection<Backend = DB>,
{
    fn modify_query<'a>(
        &self,
        _select: &LookAheadSelection<'_, WundergraphScalarValue>,
        query: BoxedQuery<'a, T, DB, Self>,
    ) -> Result<BoxedQuery<'a, T, DB, Self>> {
        match T::TYPE_NAME {
            _ => Ok(query),
        }
    }
}

Query object

use crate::person::model::Person;
use crate::event::model::Event;
use crate::address::model::Address;

wundergraph::query_object!{
    Query {
       Person,
       Event,
       Address
   }
}
weiznich commented 4 years ago

Can you provide a full example that fails to compile, not only parts of the project? I cannot reproduce this issue with the information given here.

andrejohansson commented 4 years ago

Here the full project in its current state, please do not publish (link expires after 1 download).

Thank you so much for your time.

weiznich commented 4 years ago

I hand some time to investigate why this happens. Turns out the reason is quite simple đŸ™ˆ :. WundergraphEntity generates backend specific code (because of type system reasons…). That means depending on what backend is enabled for wundergraph the correct impl is generated. In your example there was no backend enabled, so no impl was generated. Enabling at least the postgres backend does fix the problem described in this issue. You need to change wundergraph = "0.1.2" to wundergraph = { version = "0.1.2", features = ["postgres", "chrono"]} (I've also added the chrono feature, because you use chrono in your entities :wink:.

There were also other issues with your code, here is a full patch to make it compile.

Closed as this is expected behaviour, though I should maybe add better documentation somewhere.

andrejohansson commented 4 years ago

Thank you so much, I suspected it was something trivial I was missing! Have a nice weekend!