Skip to content

Commit c48e3a0

Browse files
committed
DeriveRelatedEntity use async-graphql re-exported by seaography
1 parent bead32a commit c48e3a0

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

sea-orm-macros/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ syn = { version = "2", default-features = false, features = ["parsing", "proc-ma
2323
quote = { version = "1", default-features = false }
2424
heck = { version = "0.4", default-features = false }
2525
proc-macro2 = { version = "1", default-features = false }
26+
proc-macro-crate = { version = "3.2.0" }
2627
unicode-ident = { version = "1" }
2728

2829
[dev-dependencies]

sea-orm-macros/src/derives/related_entity.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use heck::ToLowerCamelCase;
2-
use proc_macro2::TokenStream;
2+
use proc_macro2::{Ident, Span, TokenStream};
3+
use proc_macro_crate::{crate_name, FoundCrate};
34
use quote::{quote, quote_spanned};
45

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

86+
// Get the path of the `async-graphql` on the application's Cargo.toml
87+
let async_graphql_crate = match crate_name("async-graphql") {
88+
// if found, use application's `async-graphql`
89+
Ok(FoundCrate::Name(name)) => {
90+
let ident = Ident::new(&name, Span::call_site());
91+
quote! { #ident }
92+
}
93+
Ok(FoundCrate::Itself) => quote! { async_graphql },
94+
// if not, then use the `async-graphql` re-exported by `seaography`
95+
Err(_) => quote! { seaography::async_graphql },
96+
};
97+
8598
Ok(quote! {
8699
impl seaography::RelationBuilder for #ident {
87-
fn get_relation(&self, context: & 'static seaography::BuilderContext) -> async_graphql::dynamic::Field {
100+
fn get_relation(&self, context: & 'static seaography::BuilderContext) -> #async_graphql_crate::dynamic::Field {
88101
let builder = seaography::EntityObjectRelationBuilder { context };
89102
let via_builder = seaography::EntityObjectViaRelationBuilder { context };
90103
match self {

0 commit comments

Comments
 (0)