SeaQL / seaography

🧭 GraphQL framework for SeaORM
Apache License 2.0
363 stars 31 forks source link

Docs: adoption guide for existing SeaORM users #43

Open billy1624 opened 1 year ago

billy1624 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

It's probably just for me

// 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
```crate
pub use common::*;

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