so, for example i have user entity which has related adverts (one user -> many adverts)
use async_graphql::{self, SimpleObject};
use chrono::NaiveDateTime;
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, SimpleObject)]
#[sea_orm(table_name = "user")]
#[graphql(name = "User")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
pub name: String,
pub surname: String,
#[sea_orm(unique)]
pub email: String,
pub phone: String,
pub balance: f32,
#[graphql(visible = false)]
pub password_hash: String,
#[graphql(visible = false)]
pub refresh_token: Option<String>,
// pub adverts
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::advert::Entity")]
Advert,
}
// `Related` trait has to be implemented by hand
impl Related<super::advert::Entity> for Entity {
fn to() -> RelationDef {
Relation::Advert.def()
}
}
impl ActiveModelBehavior for ActiveModel {}
impl Entity {
pub fn find_by_email(email: String) -> Select<Entity> {
Self::find().filter(Column::Email.eq(email))
}
}
advert:
use async_graphql::{self, SimpleObject};
use chrono::NaiveDateTime;
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, SimpleObject)]
#[sea_orm(table_name = "advert")]
#[graphql(name = "Advert")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
pub available: bool,
pub price: f32,
pub location: String,
pub user_id: i32,
// pub adverts
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::user::Entity",
from = "Column::UserId",
to = "super::user::Column::Id"
)]
User,
}
impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}
impl ActiveModelBehavior for ActiveModel {}
and i want to query User but with all his adverts, is there a way to query it without defining new struct ?
#[derive(SimpleObject)]
#[graphql(name = "UserWithAdverts")]
pub struct UserWithAdverts {
pub id: i32,
pub name: String,
pub surname: String,
pub email: String,
pub phone: String,
adverts: Vec<advert::Model>,
}
maybe it’s a good method, but if i will have a million of different tables and relations, i will need to make 2 millions concatenated struct for my query methods.
this is a query that i wrote with first method
async fn get_user(
&self,
ctx: &async_graphql::Context<'_>,
id: i32,
) -> Result<UserWithAdverts, async_graphql::Error> {
let my_ctx = ctx.data::<Context>().unwrap();
let user: Option<user::Model> = User::find_by_id(id).one(&my_ctx.db).await?;
let user = match user {
Some(user) => user,
None => {
return Err(async_graphql::Error::new(
"Wrong email or password".to_string(),
))
}
};
let adverts: Vec<advert::Model> = user.find_related(Advert).all(&my_ctx.db).await?;
println!("{:?}", adverts);
// You can now access the database connection via `my_ctx.db`
return Ok(UserWithAdverts {
id: user.id,
name: user.name,
surname: user.surname,
email: user.email,
phone: user.phone,
adverts: adverts,
});
}
i`m expecting something simillar like in typeorm where i can store related entities like this:
@ManyToOne(() => User, (author) => author.messages)
author: User;
@OneToMany(() => Message, (message) => message.author)
messages: Message[];
can i implement this with sea orm and async_graphq?