Skip to content

Commit

Permalink
DeriveRelatedEntity use async-graphql re-exported by seaography
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Jan 9, 2025
1 parent bead32a commit e553115
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions sea-orm-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ syn = { version = "2", default-features = false, features = ["parsing", "proc-ma
quote = { version = "1", default-features = false }
heck = { version = "0.4", default-features = false }
proc-macro2 = { version = "1", default-features = false }
proc-macro-crate = { version = "3.2.0" }
unicode-ident = { version = "1" }

[dev-dependencies]
Expand Down
17 changes: 15 additions & 2 deletions sea-orm-macros/src/derives/related_entity.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use heck::ToLowerCamelCase;
use proc_macro2::TokenStream;
use proc_macro2::{Ident, Span, TokenStream};
use proc_macro_crate::{crate_name, FoundCrate};
use quote::{quote, quote_spanned};

use crate::derives::attributes::related_attr;
Expand Down Expand Up @@ -82,9 +83,21 @@ impl DeriveRelatedEntity {
})
.collect::<Result<Vec<_>, _>>()?;

// Get the path of the `async-graphql` on the application's Cargo.toml
let async_graphql_crate = match crate_name("async-graphql") {
// if found, use application's `async-graphql`
Ok(FoundCrate::Name(name)) => {
let ident = Ident::new(&name, Span::call_site());
quote! { #ident }
},
Ok(FoundCrate::Itself) => quote! { async_graphql },
// if not, then use the `async-graphql` re-exported by `seaography`
Err(_) => quote! { seaography::async_graphql },
};

Ok(quote! {
impl seaography::RelationBuilder for #ident {
fn get_relation(&self, context: & 'static seaography::BuilderContext) -> async_graphql::dynamic::Field {
fn get_relation(&self, context: & 'static seaography::BuilderContext) -> #async_graphql_crate::dynamic::Field {
let builder = seaography::EntityObjectRelationBuilder { context };
let via_builder = seaography::EntityObjectViaRelationBuilder { context };
match self {
Expand Down

0 comments on commit e553115

Please sign in to comment.