Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Docs: adoption guide for existing SeaORM users #43

Open
billy1624 opened this issue Sep 1, 2022 · 3 comments
Open

Docs: adoption guide for existing SeaORM users #43

billy1624 opened this issue Sep 1, 2022 · 3 comments

Comments

@billy1624
Copy link
Member

I think we need an "adoption guide" (feel free to rename it) for existing SeaORM users.

For example,

  1. they have to add extra derive on Model and Relation
  2. query_root.rs has to be added as well
  3. all necessary dependency are also required
use sea_orm::entity::prelude::*;

#[derive(
    Clone,
    Debug,
    PartialEq,
    DeriveEntityModel,
+   async_graphql::SimpleObject,
+   seaography::macros::Filter,
)]
#[sea_orm(table_name = "albums")]
#[graphql(complex)]
#[graphql(name = "Albums")]
pub struct Model {
    #[sea_orm(column_name = "AlbumId", primary_key)]
    pub album_id: i32,
    #[sea_orm(column_name = "Title")]
    pub title: String,
    #[sea_orm(column_name = "ArtistId")]
    pub artist_id: i32,
}

#[derive(
    Copy,
    Clone,
    Debug,
    EnumIter,
+   DeriveRelation,
+   seaography::macros::RelationsCompact,
)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::artists::Entity",
        from = "Column::ArtistId",
        to = "super::artists::Column::ArtistId",
        on_update = "NoAction",
        on_delete = "NoAction"
    )]
    Artists,
    #[sea_orm(has_many = "super::tracks::Entity")]
    Tracks,
}

impl Related<super::artists::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Artists.def()
    }
}

impl Related<super::tracks::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Tracks.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}
@billy1624
Copy link
Member Author

However, under the current setup. One has to modify existing SeaORM entities. I don't think it's ideal. Because it pollute the entity files and seaography dependency become part of entity crate. So, it's not an add-on.

Ideally, we want to have a seperate files to implement the async-graphql types.

Thoughts? @tyt2y3 @karatakis

@karatakis
Copy link
Collaborator

We can approach the problem in 3 different ways:

  1. Make a guide (which derive macros the developer has to add) how turn an Sea ORM entity into GraphQL node
  2. Develop a tool that parses Sea ORM entities folder and adds the required macros
  3. The solution you are recommending (though we might have some code duplication)

@huang12zheng
Copy link

It's probably just for me

  • model
use crate::*; //just one line<<<
// something about Model Schema  //<<<<
  • common
// use for marco
// pub mod data_dict;
pub use async_graphql;
pub use sea_orm;
pub use seaography;

// use for common source files
pub use async_graphql::SimpleObject;
// pub use data_dict::*;
pub use sea_orm::entity::prelude::*;
pub use seaography::macros::{Filter, RelationsCompact};
  • crate or lib
pub use common::*;

pub mod account;
pub use account::*;
// ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Triage
Development

No branches or pull requests

3 participants