Skip to content

Commit

Permalink
Allow QueryRoot derive to drive async gql config
Browse files Browse the repository at this point in the history
  • Loading branch information
karatakis committed Oct 16, 2022
1 parent 126a1e3 commit 8582a1e
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions derive/src/root_query.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};

#[derive(Debug, Eq, PartialEq, bae::FromAttributes)]
#[derive(Debug, Eq, PartialEq, bae::FromAttributes, Clone)]
pub struct Seaography {
entity: syn::Lit,
entity: Option<syn::Lit>,
object_config: Option<syn::Expr>
}

pub fn root_query_fn(
Expand All @@ -12,8 +13,14 @@ pub fn root_query_fn(
) -> Result<TokenStream, crate::error::Error> {
let paths = attrs
.iter()
.filter(|attribute| {
match &attribute.entity {
Some(_) => true,
_ => false
}
})
.map(|attribute| -> Result<TokenStream, crate::error::Error> {
if let syn::Lit::Str(item) = &attribute.entity {
if let syn::Lit::Str(item) = attribute.entity.as_ref().unwrap() {
Ok(item.value().parse::<TokenStream>()?)
} else {
Err(crate::error::Error::Internal(
Expand All @@ -23,6 +30,29 @@ pub fn root_query_fn(
})
.collect::<Result<Vec<TokenStream>, crate::error::Error>>()?;

let object_config = attrs
.iter()
.find(|attribute| {
match attribute.object_config {
Some(_) => true,
_ => false
}
})
.map(|attribute| attribute.object_config.as_ref().unwrap());

let implement_macros = match object_config {
Some(object_config) => {
quote! {
#[async_graphql::Object(#object_config)]
}
},
_ => {
quote! {
#[async_graphql::Object]
}
}
};

let queries: Vec<TokenStream> = paths
.iter()
.map(|path| {
Expand All @@ -41,7 +71,7 @@ pub fn root_query_fn(
Ok(quote! {
#basic_dependencies

#[async_graphql::Object]
#implement_macros
impl #ident {
#(#queries)*
}
Expand Down Expand Up @@ -255,7 +285,6 @@ pub fn basic_dependencies() -> TokenStream {
Cursor(CursorInput),
}


#[derive(async_graphql::SimpleObject)]
pub struct ExtraPaginationFields {
pub pages: Option<usize>,
Expand Down

0 comments on commit 8582a1e

Please sign in to comment.