the trait `async_graphql::OutputType` is not implemented for `entity::user::Model`

241 Views Asked by At

i'm trying to return user model in my graphql mutation after it, when i'm making ->ResultUser::Model rust giving an error that the trait 'async_graphql::OutputType' is not implemented for 'entity::user::Model', i was checking few examples (seaorm and async_graphql) one's and i don't really know what's going on, because all code is the same i think? so how can i fix this error?

-mutation/user.rs-

use crate::db::Database;
use async_graphql::*;
use chrono::Local;
use entity::user as User;
use sea_orm::{ActiveModelTrait, ActiveValue, Set};

#[derive(InputObject)]
pub struct CreateUserInput {
    pub username: String,
    // pub email: String,
}

#[derive(Default)]
pub struct UserMutation;

#[Object] // this line gets an error
impl UserMutation {
    pub async fn create_user(
        &self,
        ctx: &Context<'_>,
        input: CreateUserInput,
    ) -> Result<User::Model> {
        let db = ctx.data::<Database>().unwrap();

        let user = User::ActiveModel {
            username: Set(input.username),
            created: ActiveValue::set(Local::now().naive_local()),
            updated: ActiveValue::set(Local::now().naive_local()),
            ..Default::default()
        };

        Ok(user.insert(db.get_connection()).await?)
    }
}

-mutation/mod.rs-

//use entity::async_graphql;

pub mod user;
pub use user::UserMutation;

#[derive(async_graphql::MergedObject, Default)]

pub struct Mutation(UserMutation);

-entity/src/user.rs-

use async_graphql::*;
use sea_orm::{entity::prelude::*, DeleteMany};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize, SimpleObject)]
#[sea_orm(table_name = "users")]
#[graphql(concrete(name = "User", params()))]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = true, unique)]
    #[serde(skip_deserializing)]
    pub id: i32,
    pub username: String,
    pub created: DateTime,
    pub updated: DateTime,
}
//

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

impl Entity {
    pub fn find_by_id(id: i32) -> Select<Entity> {
        Self::find().filter(Column::Id.eq(id))
    }

    pub fn find_by_username(title: &str) -> Select<Entity> {
        Self::find().filter(Column::Username.eq(title))
    }

    pub fn delete_by_id(id: i32) -> DeleteMany<Entity> {
        Self::delete_many().filter(Column::Id.eq(id))
    }
}

crate structure:

enter image description here

1

There are 1 best solutions below

0
On

You have to use DateTime from chrono crate, and add chrono-feature to graphql:

Cargo.toml:

async-graphql = { version = "6.0.11", features = ["chrono"] }
chrono = "0.4.31"

and use that type:

use chrono::{DateTime, Local}
async_graphql::SimpleObject

#[derive(SimpleObject)]
pub struct Model {
  pub created: DateTime<Local>,
  pub updated: DateTime<Local>,
}