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

impl create/update/delete mutations #125

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod error;
mod filter;
mod relation;
mod root_query;
mod mutation;

#[proc_macro_derive(Filter, attributes(sea_orm))]
pub fn derive_filter_fn(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
Expand Down Expand Up @@ -135,6 +136,43 @@ pub fn derive_root_query_fn(input: proc_macro::TokenStream) -> proc_macro::Token
res.into()
}

#[proc_macro_derive(Mutation, attributes(sea_orm, seaography_mutation))]
pub fn derive_mutation_fn(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let DeriveInput {
ident, data, attrs, ..
} = syn::parse_macro_input!(input as syn::DeriveInput);

let item = match data {
syn::Data::Struct(item) => item,
_ => {
return quote::quote! {
compile_error!("Input not structure")
}
.into()
}
};

if ident.ne("Model") {
return quote::quote! {
compile_error!("Struct must be SeaOrm Model structure")
}
.into();
}

let attrs = mutation::SeaOrm::from_attributes(&attrs).unwrap();

mutation::mutation_fn(item, attrs)
.unwrap_or_else(|err| {
let error = format!("{:?}", err);

quote::quote! {
compile_error!(#error)
}
})
.into()
}


#[proc_macro_derive(EnumFilter, attributes())]
pub fn derive_enum_filter_fn(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let DeriveInput { ident, data, .. } = syn::parse_macro_input!(input as syn::DeriveInput);
Expand Down
218 changes: 218 additions & 0 deletions derive/src/mutation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
use proc_macro2::{Ident, TokenStream, Span};
use quote::quote;

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

#[derive(Debug, Eq, PartialEq, bae::FromAttributes)]
pub struct SeaographyMutation {
table_name: Option<syn::Lit>,
mutation_root: Option<syn::Lit>,
skip: Option<syn::Lit>
}

// copy of filter::SeaOrm to make table_name public
#[derive(Debug, Eq, PartialEq, bae::FromAttributes)]
pub struct SeaOrm {
table_name: Option<syn::Lit>,
}

// bool is meant as "skip", so this entry cant be created/updated/deleted
pub type IdentTypeTuple = (syn::Ident, syn::Type, bool);

pub fn mutation_fn(item: syn::DataStruct, attrs_seaorm: SeaOrm) -> Result<TokenStream, crate::error::Error> {

let fields: Vec<IdentTypeTuple> = item
.fields
.into_iter()
.map(|field| {
(
field.ident.unwrap(),
field.ty,
field.attrs
.into_iter()
.any(|attr| {
if let Ok(c) = SeaographyMutation::from_attributes(&[attr]) {
c.skip.is_some()
} else {
false
}
})
)
})
.collect();

let name = if let syn::Lit::Str(item) = attrs_seaorm.table_name.as_ref().unwrap() {
Ok(item.value().parse::<TokenStream>()?)
} else {
Err(crate::error::Error::Internal(
"Unreachable parse of query entities".into(),
))
}?;

let create_mutation_query = create_mutation(&name, &fields);
let delete_mutation_query = delete_mutation(&name);
let update_mutation_query = update_mutation(&name, &fields);

let struct_name = Ident::new(&format!("{}Mutation", &name), Span::call_site());

Ok(quote! {

#[derive(Default)]
pub struct #struct_name;

#[async_graphql::Object]
impl #struct_name {
#create_mutation_query
#delete_mutation_query
#update_mutation_query
}
})

}

pub fn create_mutation(name: &TokenStream, fields: &[IdentTypeTuple]) -> TokenStream {

let variables: Vec<TokenStream> = fields
.iter()
.filter(|(_, _, skip)| { !*skip })
.map(|(i, tp, _)| {
quote! {
#i: #tp,
}
})
.collect();


let variables_set: Vec<TokenStream> = fields
.iter()
.filter(|(_, _, i)| { !*i })
.map(|(i, _, _)| {
quote! {
#i: Set(#i),
}
})
.collect();

let fn_name = Ident::new(&format!("create_{}", *name), Span::call_site());

quote! {

pub async fn #fn_name<'a>(
&self,
ctx: &async_graphql::Context<'a>,
#(#variables)*
) -> async_graphql::Result<bool> {

use async_graphql::*;
use sea_orm::prelude::*;
use sea_orm::{NotSet, Set, IntoActiveModel, ActiveModelTrait, ModelTrait, EntityTrait, QueryFilter, ColumnTrait};

let db: &crate::DatabaseConnection = ctx.data::<crate::DatabaseConnection>().unwrap();

let c = super::#name::ActiveModel {
#(#variables_set)*
..Default::default()
};

let res = c.insert(db).await?;

Ok(true)
}
}
}

pub fn delete_mutation(name: &TokenStream) -> TokenStream {

let fn_name = Ident::new(&format!("delete_{}", *name), Span::call_site());

quote! {

pub async fn #fn_name<'a>(
&self,
ctx: &async_graphql::Context<'a>,
filters: super::#name::Filter,
) -> async_graphql::Result<bool> {
use async_graphql::*;
use sea_orm::prelude::*;
use sea_orm::{EntityTrait};
use seaography::{EntityOrderBy, EntityFilter};

let db: &crate::DatabaseConnection = ctx.data::<crate::DatabaseConnection>().unwrap();
let res = super::#name::Entity::delete_many()
.filter(filters.filter_condition())
.exec(db)
.await?;

Ok(true)
}
}
}




pub fn update_mutation(name: &TokenStream, fields: &[IdentTypeTuple]) -> TokenStream {

let variables: Vec<TokenStream> = fields
.iter()
.filter(|(_, _, skip)| { !*skip })
.map(|(i, tp, _)| {
quote! {
#i: Option<#tp>,
}
})
.collect();


let variables_set: Vec<TokenStream> = fields
.iter()
.filter(|(_, _, i)| { !*i })
.map(|(i, _, _)| {
quote! {

//c.#i = Set(v)

if let Some(v) = #i {
c.#i = Set(v);
}
}
})
.collect();

let fn_name = Ident::new(&format!("update_{}", *name), Span::call_site());

quote! {

pub async fn #fn_name<'a>(
&self,
ctx: &async_graphql::Context<'a>,
filters: super::#name::Filter,
#(#variables)*
) -> async_graphql::Result<bool> {

use async_graphql::*;
use sea_orm::prelude::*;
use sea_orm::{NotSet, Set, IntoActiveModel, ActiveModelTrait, ModelTrait, EntityTrait, QueryFilter, ColumnTrait};
use seaography::EntityFilter;

let db: &crate::DatabaseConnection = ctx.data::<crate::DatabaseConnection>().unwrap();

let mut c: super::#name::ActiveModel =Default::default();

#(#variables_set)*

let res = super::#name::Entity::update_many()
.set(c)
.filter(filters.filter_condition())
.exec(db)
.await?;

Ok(true)
}
}
}