2022-04-19 04:25:11 +00:00
|
|
|
use std::{iter::FromIterator, str::FromStr};
|
|
|
|
|
2021-03-17 12:51:30 +00:00
|
|
|
use proc_macro::TokenStream;
|
2021-10-25 12:23:12 +00:00
|
|
|
use proc_macro2::Ident;
|
2021-03-17 12:51:30 +00:00
|
|
|
use quote::quote;
|
2021-10-25 12:23:12 +00:00
|
|
|
use syn::{
|
2022-04-19 04:25:11 +00:00
|
|
|
ext::IdentExt, punctuated::Punctuated, Block, Error, FnArg, ImplItem, ItemImpl, Pat,
|
|
|
|
ReturnType, Token, Type, TypeReference,
|
2021-10-25 12:23:12 +00:00
|
|
|
};
|
2021-03-17 12:51:30 +00:00
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
use crate::{
|
|
|
|
args::{self, ComplexityType, RenameRuleExt, RenameTarget},
|
|
|
|
output_type::OutputType,
|
|
|
|
utils::{
|
|
|
|
extract_input_args, gen_deprecation, generate_default, generate_guards, get_cfg_attrs,
|
|
|
|
get_crate_name, get_rustdoc, get_type_path_and_name, parse_complexity_expr,
|
|
|
|
parse_graphql_attrs, remove_graphql_attrs, visible_fn, GeneratorResult,
|
|
|
|
},
|
2021-03-17 12:51:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn generate(
|
|
|
|
object_args: &args::ComplexObject,
|
|
|
|
item_impl: &mut ItemImpl,
|
|
|
|
) -> GeneratorResult<TokenStream> {
|
|
|
|
let crate_name = get_crate_name(object_args.internal);
|
|
|
|
let (self_ty, _) = get_type_path_and_name(item_impl.self_ty.as_ref())?;
|
|
|
|
let generics = &item_impl.generics;
|
|
|
|
let where_clause = &item_impl.generics.where_clause;
|
|
|
|
|
|
|
|
let mut resolvers = Vec::new();
|
|
|
|
let mut schema_fields = Vec::new();
|
|
|
|
|
2021-10-25 12:23:12 +00:00
|
|
|
// Computation of the derivated fields
|
|
|
|
let mut derived_impls = vec![];
|
|
|
|
for item in &mut item_impl.items {
|
|
|
|
if let ImplItem::Method(method) = item {
|
2022-01-18 02:33:07 +00:00
|
|
|
let method_args: args::ComplexObjectField =
|
2021-10-25 12:23:12 +00:00
|
|
|
parse_graphql_attrs(&method.attrs)?.unwrap_or_default();
|
|
|
|
|
|
|
|
for derived in method_args.derived {
|
|
|
|
if derived.name.is_some() && derived.into.is_some() {
|
|
|
|
let base_function_name = &method.sig.ident;
|
|
|
|
let name = derived.name.unwrap();
|
2021-10-28 12:42:13 +00:00
|
|
|
let with = derived.with;
|
2021-10-25 12:23:12 +00:00
|
|
|
let into = Type::Verbatim(
|
|
|
|
proc_macro2::TokenStream::from_str(&derived.into.unwrap()).unwrap(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut new_impl = method.clone();
|
|
|
|
new_impl.sig.ident = name;
|
|
|
|
new_impl.sig.output =
|
|
|
|
syn::parse2::<ReturnType>(quote! { -> #crate_name::Result<#into> })
|
|
|
|
.expect("invalid result type");
|
|
|
|
|
|
|
|
let should_create_context = new_impl
|
|
|
|
.sig
|
|
|
|
.inputs
|
|
|
|
.iter()
|
|
|
|
.nth(1)
|
|
|
|
.map(|x| {
|
|
|
|
if let FnArg::Typed(pat) = x {
|
|
|
|
if let Type::Reference(TypeReference { elem, .. }) = &*pat.ty {
|
|
|
|
if let Type::Path(path) = elem.as_ref() {
|
|
|
|
return path.path.segments.last().unwrap().ident
|
|
|
|
!= "Context";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
true
|
|
|
|
})
|
|
|
|
.unwrap_or(true);
|
|
|
|
|
|
|
|
if should_create_context {
|
|
|
|
let arg_ctx = syn::parse2::<FnArg>(quote! { ctx: &Context<'_> })
|
|
|
|
.expect("invalid arg type");
|
|
|
|
new_impl.sig.inputs.insert(1, arg_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
let other_atts: Punctuated<Ident, Token![,]> = Punctuated::from_iter(
|
|
|
|
new_impl
|
|
|
|
.sig
|
|
|
|
.inputs
|
|
|
|
.iter()
|
|
|
|
.filter_map(|x| match x {
|
|
|
|
FnArg::Typed(pat) => match &*pat.pat {
|
|
|
|
Pat::Ident(ident) => Some(Ok(ident.ident.clone())),
|
|
|
|
_ => Some(Err(Error::new_spanned(
|
|
|
|
&pat,
|
|
|
|
"Must be a simple argument",
|
2021-10-26 10:57:39 +00:00
|
|
|
))),
|
2021-10-25 12:23:12 +00:00
|
|
|
},
|
|
|
|
FnArg::Receiver(_) => None,
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<Ident>, Error>>()?
|
|
|
|
.into_iter(),
|
|
|
|
);
|
|
|
|
|
2021-10-28 12:42:13 +00:00
|
|
|
let new_block = match with {
|
|
|
|
Some(with) => quote!({
|
|
|
|
::std::result::Result::Ok(#with(#self_ty::#base_function_name(&self, #other_atts).await?))
|
|
|
|
}),
|
|
|
|
None => quote!({
|
|
|
|
{
|
|
|
|
::std::result::Result::Ok(#self_ty::#base_function_name(&self, #other_atts).await?.into())
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
};
|
2021-10-25 12:23:12 +00:00
|
|
|
|
|
|
|
new_impl.block = syn::parse2::<Block>(new_block).expect("invalid block");
|
|
|
|
|
|
|
|
derived_impls.push(ImplItem::Method(new_impl));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
item_impl.items.append(&mut derived_impls);
|
|
|
|
|
2021-03-17 12:51:30 +00:00
|
|
|
for item in &mut item_impl.items {
|
|
|
|
if let ImplItem::Method(method) = item {
|
2022-01-18 02:33:07 +00:00
|
|
|
let method_args: args::ComplexObjectField =
|
2021-03-17 12:51:30 +00:00
|
|
|
parse_graphql_attrs(&method.attrs)?.unwrap_or_default();
|
|
|
|
if method_args.skip {
|
2021-07-03 16:04:18 +00:00
|
|
|
remove_graphql_attrs(&mut method.attrs);
|
2021-03-17 12:51:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-01-18 02:33:07 +00:00
|
|
|
let cfg_attrs = get_cfg_attrs(&method.attrs);
|
|
|
|
|
|
|
|
if method_args.flatten {
|
2022-02-04 16:30:58 +00:00
|
|
|
// Only used to inject the context placeholder if required.
|
2022-02-28 07:26:33 +00:00
|
|
|
extract_input_args::<args::Argument>(&crate_name, method)?;
|
2022-02-04 16:30:58 +00:00
|
|
|
|
2022-01-18 02:33:07 +00:00
|
|
|
let ty = match &method.sig.output {
|
|
|
|
ReturnType::Type(_, ty) => OutputType::parse(ty)?,
|
|
|
|
ReturnType::Default => {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
&method.sig.output,
|
|
|
|
"Flatten resolver must have a return type",
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let ty = ty.value_type();
|
|
|
|
let ident = &method.sig.ident;
|
|
|
|
|
|
|
|
schema_fields.push(quote! {
|
|
|
|
#crate_name::static_assertions::assert_impl_one!(#ty: #crate_name::ObjectType);
|
|
|
|
<#ty>::create_type_info(registry);
|
|
|
|
if let #crate_name::registry::MetaType::Object { fields: obj_fields, .. } =
|
|
|
|
registry.create_fake_output_type::<#ty>() {
|
|
|
|
fields.extend(obj_fields);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
resolvers.push(quote! {
|
|
|
|
#(#cfg_attrs)*
|
2022-02-04 16:30:58 +00:00
|
|
|
if let ::std::option::Option::Some(value) = #crate_name::ContainerType::resolve_field(&self.#ident(ctx).await, ctx).await? {
|
2022-01-18 02:33:07 +00:00
|
|
|
return ::std::result::Result::Ok(std::option::Option::Some(value));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
remove_graphql_attrs(&mut method.attrs);
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-17 12:51:30 +00:00
|
|
|
|
|
|
|
let field_name = method_args.name.clone().unwrap_or_else(|| {
|
|
|
|
object_args
|
|
|
|
.rename_fields
|
|
|
|
.rename(method.sig.ident.unraw().to_string(), RenameTarget::Field)
|
|
|
|
});
|
|
|
|
let field_desc = get_rustdoc(&method.attrs)?
|
|
|
|
.map(|s| quote! { ::std::option::Option::Some(#s) })
|
|
|
|
.unwrap_or_else(|| quote! {::std::option::Option::None});
|
|
|
|
let field_deprecation = gen_deprecation(&method_args.deprecation, &crate_name);
|
|
|
|
let external = method_args.external;
|
|
|
|
let requires = match &method_args.requires {
|
|
|
|
Some(requires) => quote! { ::std::option::Option::Some(#requires) },
|
|
|
|
None => quote! { ::std::option::Option::None },
|
|
|
|
};
|
|
|
|
let provides = match &method_args.provides {
|
|
|
|
Some(provides) => quote! { ::std::option::Option::Some(#provides) },
|
|
|
|
None => quote! { ::std::option::Option::None },
|
|
|
|
};
|
|
|
|
let cache_control = {
|
|
|
|
let public = method_args.cache_control.is_public();
|
|
|
|
let max_age = method_args.cache_control.max_age;
|
|
|
|
quote! {
|
|
|
|
#crate_name::CacheControl {
|
|
|
|
public: #public,
|
|
|
|
max_age: #max_age,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-28 07:26:33 +00:00
|
|
|
let mut args = extract_input_args::<args::Argument>(&crate_name, method)?;
|
2021-03-17 12:51:30 +00:00
|
|
|
let mut schema_args = Vec::new();
|
|
|
|
let mut use_params = Vec::new();
|
|
|
|
let mut get_params = Vec::new();
|
2022-02-28 07:26:33 +00:00
|
|
|
let mut is_oneof_field = false;
|
2021-03-17 12:51:30 +00:00
|
|
|
|
2022-02-28 07:26:33 +00:00
|
|
|
if method_args.oneof {
|
|
|
|
is_oneof_field = true;
|
2021-03-17 12:51:30 +00:00
|
|
|
|
2022-02-28 07:26:33 +00:00
|
|
|
if args.len() != 1 {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
&method,
|
|
|
|
"The `oneof` field requires exactly one argument.",
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
let (ident, ty, argument) = args.pop().unwrap();
|
2021-03-17 12:51:30 +00:00
|
|
|
schema_args.push(quote! {
|
2022-02-28 07:26:33 +00:00
|
|
|
#crate_name::static_assertions::assert_impl_one!(#ty: #crate_name::OneofObjectType);
|
|
|
|
if let #crate_name::registry::MetaType::InputObject { input_fields, .. } = registry.create_fake_input_type::<#ty>() {
|
|
|
|
args.extend(input_fields);
|
2021-03-17 12:51:30 +00:00
|
|
|
}
|
2022-02-28 07:26:33 +00:00
|
|
|
});
|
|
|
|
use_params.push(quote! { #ident });
|
|
|
|
|
2022-04-06 07:10:56 +00:00
|
|
|
let param_ident = &ident.ident;
|
|
|
|
let process_with = match argument.process_with.as_ref() {
|
|
|
|
Some(fn_path) => {
|
|
|
|
let fn_path: syn::ExprPath = syn::parse_str(fn_path)?;
|
|
|
|
quote! {
|
|
|
|
#fn_path(&mut #param_ident);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Default::default(),
|
|
|
|
};
|
|
|
|
|
2022-02-28 07:26:33 +00:00
|
|
|
let validators = argument
|
|
|
|
.validator
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_default()
|
|
|
|
.create_validators(
|
|
|
|
&crate_name,
|
|
|
|
quote!(&#ident),
|
|
|
|
quote!(#ty),
|
|
|
|
Some(quote!(.map_err(|err| err.into_server_error(__pos)))),
|
|
|
|
)?;
|
2022-04-06 07:10:56 +00:00
|
|
|
let mut non_mut_ident = ident.clone();
|
|
|
|
non_mut_ident.mutability = None;
|
2021-03-17 12:51:30 +00:00
|
|
|
get_params.push(quote! {
|
2022-04-06 07:10:56 +00:00
|
|
|
#[allow(non_snake_case, unused_variables, unused_mut)]
|
|
|
|
let (__pos, mut #non_mut_ident) = ctx.oneof_param_value::<#ty>()?;
|
|
|
|
#process_with
|
2021-11-14 13:09:14 +00:00
|
|
|
#validators
|
2022-04-06 07:10:56 +00:00
|
|
|
#[allow(non_snake_case, unused_variables)]
|
|
|
|
let #ident = #non_mut_ident;
|
2021-11-14 13:09:14 +00:00
|
|
|
});
|
2022-02-28 07:26:33 +00:00
|
|
|
} else {
|
|
|
|
for (
|
|
|
|
ident,
|
|
|
|
ty,
|
|
|
|
args::Argument {
|
|
|
|
name,
|
|
|
|
desc,
|
|
|
|
default,
|
|
|
|
default_with,
|
|
|
|
validator,
|
2022-04-06 07:10:56 +00:00
|
|
|
process_with,
|
2022-02-28 07:26:33 +00:00
|
|
|
visible,
|
|
|
|
secret,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
) in &args
|
|
|
|
{
|
|
|
|
let name = name.clone().unwrap_or_else(|| {
|
|
|
|
object_args
|
|
|
|
.rename_args
|
|
|
|
.rename(ident.ident.unraw().to_string(), RenameTarget::Argument)
|
|
|
|
});
|
|
|
|
let desc = desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {::std::option::Option::Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {::std::option::Option::None});
|
|
|
|
let default = generate_default(default, default_with)?;
|
|
|
|
let schema_default = default
|
|
|
|
.as_ref()
|
|
|
|
.map(|value| {
|
|
|
|
quote! {
|
|
|
|
::std::option::Option::Some(::std::string::ToString::to_string(
|
|
|
|
&<#ty as #crate_name::InputType>::to_value(&#value)
|
|
|
|
))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| quote! {::std::option::Option::None});
|
|
|
|
|
|
|
|
let visible = visible_fn(visible);
|
|
|
|
schema_args.push(quote! {
|
|
|
|
args.insert(::std::borrow::ToOwned::to_owned(#name), #crate_name::registry::MetaInputValue {
|
|
|
|
name: #name,
|
|
|
|
description: #desc,
|
|
|
|
ty: <#ty as #crate_name::InputType>::create_type_info(registry),
|
|
|
|
default_value: #schema_default,
|
|
|
|
visible: #visible,
|
|
|
|
is_secret: #secret,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let param_ident = &ident.ident;
|
|
|
|
use_params.push(quote! { #param_ident });
|
|
|
|
|
|
|
|
let default = match default {
|
|
|
|
Some(default) => {
|
|
|
|
quote! { ::std::option::Option::Some(|| -> #ty { #default }) }
|
|
|
|
}
|
|
|
|
None => quote! { ::std::option::Option::None },
|
|
|
|
};
|
|
|
|
|
2022-04-06 07:10:56 +00:00
|
|
|
let param_ident = &ident.ident;
|
|
|
|
let process_with = match process_with.as_ref() {
|
|
|
|
Some(fn_path) => {
|
|
|
|
let fn_path: syn::ExprPath = syn::parse_str(fn_path)?;
|
|
|
|
quote! {
|
|
|
|
#fn_path(&mut #param_ident);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Default::default(),
|
|
|
|
};
|
|
|
|
|
2022-02-28 07:26:33 +00:00
|
|
|
let validators = validator.clone().unwrap_or_default().create_validators(
|
|
|
|
&crate_name,
|
|
|
|
quote!(&#ident),
|
|
|
|
quote!(ty),
|
|
|
|
Some(quote!(.map_err(|err| err.into_server_error(__pos)))),
|
|
|
|
)?;
|
|
|
|
|
2022-04-06 07:10:56 +00:00
|
|
|
let mut non_mut_ident = ident.clone();
|
|
|
|
non_mut_ident.mutability = None;
|
2022-02-28 07:26:33 +00:00
|
|
|
get_params.push(quote! {
|
2022-04-06 07:10:56 +00:00
|
|
|
#[allow(non_snake_case, unused_mut)]
|
|
|
|
let (__pos, mut #non_mut_ident) = ctx.param_value::<#ty>(#name, #default)?;
|
|
|
|
#process_with
|
2022-02-28 07:26:33 +00:00
|
|
|
#validators
|
2022-04-06 07:10:56 +00:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
let #ident = #non_mut_ident;
|
2022-02-28 07:26:33 +00:00
|
|
|
});
|
|
|
|
}
|
2021-03-17 12:51:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 07:26:33 +00:00
|
|
|
let ty = match &method.sig.output {
|
|
|
|
ReturnType::Type(_, ty) => OutputType::parse(ty)?,
|
|
|
|
ReturnType::Default => {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
&method.sig.output,
|
|
|
|
"Resolver must have a return type",
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
};
|
2021-03-17 12:51:30 +00:00
|
|
|
let schema_ty = ty.value_type();
|
|
|
|
let visible = visible_fn(&method_args.visible);
|
|
|
|
|
|
|
|
let complexity = if let Some(complexity) = &method_args.complexity {
|
|
|
|
match complexity {
|
|
|
|
ComplexityType::Const(n) => {
|
|
|
|
quote! { ::std::option::Option::Some(#crate_name::registry::ComplexityType::Const(#n)) }
|
|
|
|
}
|
|
|
|
ComplexityType::Fn(s) => {
|
|
|
|
let (variables, expr) = parse_complexity_expr(s)?;
|
|
|
|
let mut parse_args = Vec::new();
|
|
|
|
for variable in variables {
|
|
|
|
if let Some((
|
|
|
|
ident,
|
|
|
|
ty,
|
|
|
|
args::Argument {
|
|
|
|
name,
|
|
|
|
default,
|
|
|
|
default_with,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
)) = args
|
|
|
|
.iter()
|
|
|
|
.find(|(pat_ident, _, _)| pat_ident.ident == variable)
|
|
|
|
{
|
2021-07-31 15:54:16 +00:00
|
|
|
let default = match generate_default(default, default_with)? {
|
2021-03-17 12:51:30 +00:00
|
|
|
Some(default) => {
|
|
|
|
quote! { ::std::option::Option::Some(|| -> #ty { #default }) }
|
|
|
|
}
|
|
|
|
None => quote! { ::std::option::Option::None },
|
|
|
|
};
|
|
|
|
let name = name.clone().unwrap_or_else(|| {
|
|
|
|
object_args.rename_args.rename(
|
|
|
|
ident.ident.unraw().to_string(),
|
|
|
|
RenameTarget::Argument,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
parse_args.push(quote! {
|
|
|
|
let #ident: #ty = __ctx.param_value(__variables_definition, __field, #name, #default)?;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
quote! {
|
|
|
|
Some(#crate_name::registry::ComplexityType::Fn(|__ctx, __variables_definition, __field, child_complexity| {
|
|
|
|
#(#parse_args)*
|
|
|
|
Ok(#expr)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quote! { ::std::option::Option::None }
|
|
|
|
};
|
|
|
|
|
|
|
|
schema_fields.push(quote! {
|
|
|
|
#(#cfg_attrs)*
|
|
|
|
fields.push((#field_name.to_string(), #crate_name::registry::MetaField {
|
|
|
|
name: ::std::borrow::ToOwned::to_owned(#field_name),
|
|
|
|
description: #field_desc,
|
|
|
|
args: {
|
|
|
|
let mut args = #crate_name::indexmap::IndexMap::new();
|
|
|
|
#(#schema_args)*
|
|
|
|
args
|
|
|
|
},
|
2021-11-09 10:52:10 +00:00
|
|
|
ty: <#schema_ty as #crate_name::OutputType>::create_type_info(registry),
|
2021-03-17 12:51:30 +00:00
|
|
|
deprecation: #field_deprecation,
|
|
|
|
cache_control: #cache_control,
|
|
|
|
external: #external,
|
|
|
|
provides: #provides,
|
|
|
|
requires: #requires,
|
|
|
|
visible: #visible,
|
|
|
|
compute_complexity: #complexity,
|
2022-02-28 07:26:33 +00:00
|
|
|
oneof: #is_oneof_field,
|
2021-03-17 12:51:30 +00:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
let field_ident = &method.sig.ident;
|
|
|
|
if let OutputType::Value(inner_ty) = &ty {
|
|
|
|
let block = &method.block;
|
|
|
|
let new_block = quote!({
|
|
|
|
{
|
|
|
|
::std::result::Result::Ok(async move {
|
|
|
|
let value:#inner_ty = #block;
|
|
|
|
value
|
|
|
|
}.await)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
method.block = syn::parse2::<Block>(new_block).expect("invalid block");
|
|
|
|
method.sig.output =
|
|
|
|
syn::parse2::<ReturnType>(quote! { -> #crate_name::Result<#inner_ty> })
|
|
|
|
.expect("invalid result type");
|
|
|
|
}
|
|
|
|
|
|
|
|
let resolve_obj = quote! {
|
|
|
|
{
|
|
|
|
let res = self.#field_ident(ctx, #(#use_params),*).await;
|
2021-11-15 01:12:13 +00:00
|
|
|
res.map_err(|err| ::std::convert::Into::<#crate_name::Error>::into(err).into_server_error(ctx.item.pos))
|
2021-03-17 12:51:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-16 02:22:39 +00:00
|
|
|
let guard_map_err = quote! {
|
|
|
|
.map_err(|err| err.into_server_error(ctx.item.pos))
|
|
|
|
};
|
2022-03-14 01:19:27 +00:00
|
|
|
let guard = match method_args.guard.as_ref().or(object_args.guard.as_ref()) {
|
2021-11-16 02:22:39 +00:00
|
|
|
Some(code) => Some(generate_guards(&crate_name, code, guard_map_err)?),
|
2021-03-17 12:51:30 +00:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
resolvers.push(quote! {
|
|
|
|
#(#cfg_attrs)*
|
|
|
|
if ctx.item.node.name.node == #field_name {
|
2021-11-15 01:12:13 +00:00
|
|
|
let f = async move {
|
|
|
|
#(#get_params)*
|
|
|
|
#guard
|
|
|
|
#resolve_obj
|
|
|
|
};
|
|
|
|
let obj = f.await.map_err(|err| ctx.set_error_path(err))?;
|
2021-03-17 12:51:30 +00:00
|
|
|
let ctx_obj = ctx.with_selection_set(&ctx.item.node.selection_set);
|
2021-11-15 01:12:13 +00:00
|
|
|
return #crate_name::OutputType::resolve(&obj, &ctx_obj, ctx.item).await.map(::std::option::Option::Some);
|
2021-03-17 12:51:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
remove_graphql_attrs(&mut method.attrs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
#item_impl
|
|
|
|
|
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
2021-07-03 16:02:28 +00:00
|
|
|
impl #generics #crate_name::ComplexObject for #self_ty #where_clause {
|
2021-03-17 12:51:30 +00:00
|
|
|
fn fields(registry: &mut #crate_name::registry::Registry) -> ::std::vec::Vec<(::std::string::String, #crate_name::registry::MetaField)> {
|
|
|
|
let mut fields = ::std::vec::Vec::new();
|
|
|
|
#(#schema_fields)*
|
|
|
|
fields
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::ServerResult<::std::option::Option<#crate_name::Value>> {
|
|
|
|
#(#resolvers)*
|
|
|
|
::std::result::Result::Ok(::std::option::Option::None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-12-26 02:42:36 +00:00
|
|
|
|
2021-03-17 12:51:30 +00:00
|
|
|
Ok(expanded.into())
|
|
|
|
}
|