juniper icon indicating copy to clipboard operation
juniper copied to clipboard

Got conflicting implementation while using "juniper::GraphQLObject" and "#[juniper::object(Context = Context,)]"

Open kentkrantz opened this issue 7 years ago • 2 comments

If I use #[derive(juniper::GraphQLObject)] upon a struct while using #[juniper::object(Context = Context,)] upon it's impl, I will get the conflicting implementation, but I have to derive GraphQLObject on the struct cause another GraphQLObject needs that. Anyone has idea?

#[derive(juniper::GraphQLObject)]
pub struct Repository {
    pub id: i32,
    pub name: String,
    pub user_id: i32,
}

// RepositoriesAndTotal needs Repository has GraphQLObject implementation, 
// but GraphQLObject upon struct Repository will conflict with juniper::object with Context
#[derive(juniper::GraphQLObject)]
pub struct RepositoriesAndTotal {
    pub repositories: Vec<Repository>,
    pub total: i32,
}

#[juniper::object(Context = Context,)]
impl Repository {
    fn owner(&self, context: &Context) -> User {
        self.get_owner(&context.pool.get().unwrap())
    }
}

#[juniper::object(
    Context = Context,
)]
impl QueryRoot {
    fn repositories(context: &Context, page: i32, limit: i32) -> FieldResult<RepositoriesAndTotal> {
        Ok(Repository::get_repositories(&context.pool.get().unwrap(), page, limit))
    }
}

kentkrantz avatar Jul 29 '19 11:07 kentkrantz

Does this work?

pub struct Repository {
    pub id: i32,
    pub name: String,
    pub user_id: i32,
}

// RepositoriesAndTotal needs Repository has GraphQLObject implementation,
// but GraphQLObject upon struct Repository will conflict with juniper::object with Context
#[derive(juniper::GraphQLObject)]
pub struct RepositoriesAndTotal {
    pub repositories: Vec<Repository>,
    pub total: i32,
}

#[juniper::object(Context = Context, Scalar = S)]
impl<S: juniper::ScalarValue> Repository {
    fn owner(&self, context: &Context) -> bool {
        //self.get_owner(&context.pool.get().unwrap())
        true
    }
}

struct QueryRoot;

#[juniper::object(Context = Context, Scalar = S)]
impl<S: juniper::ScalarValue> QueryRoot {
    fn repositories(context: &Context, page: i32, limit: i32) -> RepositoriesAndTotal {
        RepositoriesAndTotal {
            repositories: vec![],
            total: 0,
        }
    }
}

jairinhohw avatar Aug 02 '19 19:08 jairinhohw

Hey, please have a look at #182. (and discuss there if you have further questions).

In short: currently, you have to use either the derive or a custom implementation with #[juniper::object]. Doing both does not work at the moment.

theduke avatar Aug 02 '19 20:08 theduke