2020-03-01 10:54:34 +00:00
|
|
|
use crate::args;
|
2020-03-06 15:58:43 +00:00
|
|
|
use crate::output_type::OutputType;
|
2020-03-19 09:20:12 +00:00
|
|
|
use crate::utils::{build_value_repr, check_reserved_name, get_crate_name};
|
2020-03-09 12:00:57 +00:00
|
|
|
use inflector::Inflector;
|
2020-03-01 10:54:34 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::quote;
|
2020-03-06 15:58:43 +00:00
|
|
|
use syn::{Error, FnArg, ImplItem, ItemImpl, Pat, Result, ReturnType, Type, TypeReference};
|
2020-03-05 06:23:55 +00:00
|
|
|
|
|
|
|
pub fn generate(object_args: &args::Object, item_impl: &mut ItemImpl) -> Result<TokenStream> {
|
2020-03-02 00:24:49 +00:00
|
|
|
let crate_name = get_crate_name(object_args.internal);
|
2020-03-05 06:23:55 +00:00
|
|
|
let (self_ty, self_name) = match item_impl.self_ty.as_ref() {
|
|
|
|
Type::Path(path) => (
|
|
|
|
path,
|
|
|
|
path.path
|
|
|
|
.segments
|
|
|
|
.last()
|
|
|
|
.map(|s| s.ident.to_string())
|
|
|
|
.unwrap(),
|
|
|
|
),
|
|
|
|
_ => return Err(Error::new_spanned(&item_impl.self_ty, "Invalid type")),
|
2020-03-01 10:54:34 +00:00
|
|
|
};
|
2020-03-05 06:23:55 +00:00
|
|
|
let generics = &item_impl.generics;
|
2020-03-01 10:54:34 +00:00
|
|
|
|
|
|
|
let gql_typename = object_args
|
|
|
|
.name
|
|
|
|
.clone()
|
2020-03-05 06:23:55 +00:00
|
|
|
.unwrap_or_else(|| self_name.clone());
|
2020-03-19 09:20:12 +00:00
|
|
|
check_reserved_name(&gql_typename, object_args.internal)?;
|
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
let desc = object_args
|
|
|
|
.desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
2020-03-05 06:23:55 +00:00
|
|
|
|
2020-03-01 10:54:34 +00:00
|
|
|
let mut resolvers = Vec::new();
|
2020-03-03 03:48:00 +00:00
|
|
|
let mut schema_fields = Vec::new();
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-05 06:23:55 +00:00
|
|
|
for item in &mut item_impl.items {
|
|
|
|
if let ImplItem::Method(method) = item {
|
|
|
|
if let Some(field) = args::Field::parse(&method.attrs)? {
|
|
|
|
let field_name = field
|
|
|
|
.name
|
|
|
|
.clone()
|
2020-03-09 12:00:57 +00:00
|
|
|
.unwrap_or_else(|| method.sig.ident.to_string().to_camel_case());
|
2020-03-05 06:23:55 +00:00
|
|
|
let field_desc = field
|
|
|
|
.desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
let field_deprecation = field
|
|
|
|
.deprecation
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
let ty = match &method.sig.output {
|
2020-03-06 15:58:43 +00:00
|
|
|
ReturnType::Type(_, ty) => OutputType::parse(ty)?,
|
2020-03-05 06:23:55 +00:00
|
|
|
ReturnType::Default => {
|
2020-03-06 15:58:43 +00:00
|
|
|
return Err(Error::new_spanned(&method.sig.output, "Missing type"))
|
2020-03-05 06:23:55 +00:00
|
|
|
}
|
|
|
|
};
|
2020-03-22 08:45:59 +00:00
|
|
|
let cache_control = {
|
|
|
|
let public = field.cache_control.public;
|
|
|
|
let max_age = field.cache_control.max_age;
|
|
|
|
quote! {
|
|
|
|
#crate_name::CacheControl {
|
|
|
|
public: #public,
|
|
|
|
max_age: #max_age,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-03-05 06:23:55 +00:00
|
|
|
|
|
|
|
let mut arg_ctx = false;
|
|
|
|
let mut args = Vec::new();
|
2020-03-02 11:25:21 +00:00
|
|
|
|
2020-03-05 06:23:55 +00:00
|
|
|
for (idx, arg) in method.sig.inputs.iter_mut().enumerate() {
|
|
|
|
if let FnArg::Receiver(receiver) = arg {
|
|
|
|
if idx != 0 {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
receiver,
|
|
|
|
"The self receiver must be the first parameter.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} else if let FnArg::Typed(pat) = arg {
|
|
|
|
if idx == 0 {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
pat,
|
|
|
|
"The self receiver must be the first parameter.",
|
|
|
|
));
|
|
|
|
}
|
2020-03-04 02:38:07 +00:00
|
|
|
|
2020-03-05 06:23:55 +00:00
|
|
|
match (&*pat.pat, &*pat.ty) {
|
|
|
|
(Pat::Ident(arg_ident), Type::Path(arg_ty)) => {
|
2020-03-21 07:07:11 +00:00
|
|
|
args.push((
|
|
|
|
arg_ident,
|
|
|
|
arg_ty,
|
|
|
|
args::Argument::parse(&crate_name, &pat.attrs)?,
|
|
|
|
));
|
2020-03-05 06:23:55 +00:00
|
|
|
pat.attrs.clear();
|
|
|
|
}
|
|
|
|
(_, Type::Reference(TypeReference { elem, .. })) => {
|
|
|
|
if let Type::Path(path) = elem.as_ref() {
|
|
|
|
if idx != 1
|
2020-03-21 01:32:13 +00:00
|
|
|
|| path.path.segments.last().unwrap().ident != "Context"
|
2020-03-05 06:23:55 +00:00
|
|
|
{
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
arg,
|
|
|
|
"The Context must be the second argument.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
arg_ctx = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return Err(Error::new_spanned(arg, "Invalid argument type.")),
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 02:38:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 06:23:55 +00:00
|
|
|
let mut schema_args = Vec::new();
|
|
|
|
let mut use_params = Vec::new();
|
|
|
|
let mut get_params = Vec::new();
|
2020-03-04 02:38:07 +00:00
|
|
|
|
2020-03-05 06:23:55 +00:00
|
|
|
for (
|
|
|
|
ident,
|
|
|
|
ty,
|
|
|
|
args::Argument {
|
|
|
|
name,
|
|
|
|
desc,
|
|
|
|
default,
|
2020-03-22 01:34:32 +00:00
|
|
|
validator,
|
2020-03-05 06:23:55 +00:00
|
|
|
},
|
|
|
|
) in args
|
|
|
|
{
|
2020-03-09 12:00:57 +00:00
|
|
|
let name = name
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| ident.ident.to_string().to_camel_case());
|
2020-03-05 06:23:55 +00:00
|
|
|
let desc = desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
let schema_default = default
|
|
|
|
.as_ref()
|
|
|
|
.map(|v| {
|
|
|
|
let s = v.to_string();
|
|
|
|
quote! {Some(#s)}
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
|
|
|
|
schema_args.push(quote! {
|
2020-03-08 12:35:36 +00:00
|
|
|
args.insert(#name, #crate_name::registry::InputValue {
|
2020-03-05 06:23:55 +00:00
|
|
|
name: #name,
|
|
|
|
description: #desc,
|
2020-03-19 09:20:12 +00:00
|
|
|
ty: <#ty as #crate_name::Type>::create_type_info(registry),
|
2020-03-05 06:23:55 +00:00
|
|
|
default_value: #schema_default,
|
2020-03-22 01:34:32 +00:00
|
|
|
validator: #validator,
|
2020-03-08 12:35:36 +00:00
|
|
|
});
|
2020-03-05 06:23:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
use_params.push(quote! { #ident });
|
|
|
|
|
|
|
|
let default = match &default {
|
|
|
|
Some(default) => {
|
|
|
|
let repr = build_value_repr(&crate_name, &default);
|
2020-03-05 13:34:31 +00:00
|
|
|
quote! {|| #repr }
|
2020-03-05 06:23:55 +00:00
|
|
|
}
|
2020-03-05 13:34:31 +00:00
|
|
|
None => quote! { || #crate_name::Value::Null },
|
2020-03-05 06:23:55 +00:00
|
|
|
};
|
2020-03-17 09:26:59 +00:00
|
|
|
|
2020-03-05 06:23:55 +00:00
|
|
|
get_params.push(quote! {
|
2020-03-06 15:58:43 +00:00
|
|
|
let #ident: #ty = ctx.param_value(#name, #default)?;
|
2020-03-05 06:23:55 +00:00
|
|
|
});
|
2020-03-03 03:48:00 +00:00
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-07 10:03:19 +00:00
|
|
|
let schema_ty = ty.value_type();
|
2020-03-22 08:45:59 +00:00
|
|
|
|
2020-03-05 06:23:55 +00:00
|
|
|
schema_fields.push(quote! {
|
2020-03-19 09:20:12 +00:00
|
|
|
fields.insert(#field_name.to_string(), #crate_name::registry::Field {
|
|
|
|
name: #field_name.to_string(),
|
2020-03-05 06:23:55 +00:00
|
|
|
description: #field_desc,
|
2020-03-08 12:35:36 +00:00
|
|
|
args: {
|
|
|
|
let mut args = std::collections::HashMap::new();
|
|
|
|
#(#schema_args)*
|
|
|
|
args
|
|
|
|
},
|
2020-03-19 09:20:12 +00:00
|
|
|
ty: <#schema_ty as #crate_name::Type>::create_type_info(registry),
|
2020-03-05 06:23:55 +00:00
|
|
|
deprecation: #field_deprecation,
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: #cache_control,
|
2020-03-08 12:35:36 +00:00
|
|
|
});
|
2020-03-02 11:25:21 +00:00
|
|
|
});
|
2020-03-05 06:23:55 +00:00
|
|
|
|
2020-03-21 01:32:13 +00:00
|
|
|
let ctx_field = if arg_ctx {
|
|
|
|
quote! { &ctx, }
|
|
|
|
} else {
|
|
|
|
quote! {}
|
2020-03-05 06:23:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let field_ident = &method.sig.ident;
|
|
|
|
let resolve_obj = match &ty {
|
2020-03-05 13:34:31 +00:00
|
|
|
OutputType::Value(_) => quote! {
|
2020-03-05 06:23:55 +00:00
|
|
|
self.#field_ident(#ctx_field #(#use_params),*).await
|
|
|
|
},
|
|
|
|
OutputType::Result(_, _) => {
|
|
|
|
quote! {
|
|
|
|
self.#field_ident(#ctx_field #(#use_params),*).await.
|
|
|
|
map_err(|err| err.with_position(field.position))?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
resolvers.push(quote! {
|
|
|
|
if field.name.as_str() == #field_name {
|
|
|
|
#(#get_params)*
|
2020-03-06 15:58:43 +00:00
|
|
|
let ctx_obj = ctx.with_item(&field.selection_set);
|
2020-03-19 09:20:12 +00:00
|
|
|
return #crate_name::OutputValueType::resolve(&#resolve_obj, &ctx_obj).await.
|
2020-03-06 15:58:43 +00:00
|
|
|
map_err(|err| err.with_position(field.position).into());
|
2020-03-05 06:23:55 +00:00
|
|
|
}
|
2020-03-02 11:25:21 +00:00
|
|
|
});
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-05 06:23:55 +00:00
|
|
|
method.attrs.clear();
|
2020-03-03 03:48:00 +00:00
|
|
|
}
|
2020-03-05 06:23:55 +00:00
|
|
|
}
|
2020-03-02 00:24:49 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 08:45:59 +00:00
|
|
|
let cache_control = {
|
|
|
|
let public = object_args.cache_control.public;
|
|
|
|
let max_age = object_args.cache_control.max_age;
|
|
|
|
quote! {
|
|
|
|
#crate_name::CacheControl {
|
|
|
|
public: #public,
|
|
|
|
max_age: #max_age,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-01 10:54:34 +00:00
|
|
|
let expanded = quote! {
|
2020-03-05 06:23:55 +00:00
|
|
|
#item_impl
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
impl #generics #crate_name::Type for #self_ty {
|
2020-03-02 00:24:49 +00:00
|
|
|
fn type_name() -> std::borrow::Cow<'static, str> {
|
|
|
|
std::borrow::Cow::Borrowed(#gql_typename)
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
2020-03-03 03:48:00 +00:00
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> String {
|
2020-03-04 06:24:44 +00:00
|
|
|
registry.create_type::<Self, _>(|registry| #crate_name::registry::Type::Object {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: #gql_typename.to_string(),
|
2020-03-03 11:15:18 +00:00
|
|
|
description: #desc,
|
2020-03-08 12:35:36 +00:00
|
|
|
fields: {
|
|
|
|
let mut fields = std::collections::HashMap::new();
|
|
|
|
#(#schema_fields)*
|
|
|
|
fields
|
|
|
|
},
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: #cache_control,
|
2020-03-03 03:48:00 +00:00
|
|
|
})
|
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:24:49 +00:00
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-03-19 09:20:12 +00:00
|
|
|
impl#generics #crate_name::ObjectType for #self_ty {
|
2020-03-06 15:58:43 +00:00
|
|
|
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>, field: &#crate_name::graphql_parser::query::Field) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
2020-03-02 00:24:49 +00:00
|
|
|
use #crate_name::ErrorWithPosition;
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-06 15:58:43 +00:00
|
|
|
#(#resolvers)*
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-08 12:35:36 +00:00
|
|
|
#crate_name::anyhow::bail!(#crate_name::QueryError::FieldNotFound {
|
2020-03-06 15:58:43 +00:00
|
|
|
field_name: field.name.clone(),
|
|
|
|
object: #gql_typename.to_string(),
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
2020-03-06 15:58:43 +00:00
|
|
|
.with_position(field.position));
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
2020-03-07 02:39:55 +00:00
|
|
|
|
2020-03-21 08:13:19 +00:00
|
|
|
async fn resolve_inline_fragment(&self, name: &str, ctx: &#crate_name::ContextSelectionSet<'_>, result: &mut #crate_name::serde_json::Map<String, #crate_name::serde_json::Value>) -> #crate_name::Result<()> {
|
2020-03-08 12:35:36 +00:00
|
|
|
#crate_name::anyhow::bail!(#crate_name::QueryError::UnrecognizedInlineFragment {
|
2020-03-07 02:39:55 +00:00
|
|
|
object: #gql_typename.to_string(),
|
|
|
|
name: name.to_string(),
|
|
|
|
});
|
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
2020-03-19 09:20:12 +00:00
|
|
|
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
|
|
|
impl #generics #crate_name::OutputValueType for #self_ty {
|
|
|
|
async fn resolve(value: &Self, ctx: &#crate_name::ContextSelectionSet<'_>) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
|
|
|
#crate_name::do_resolve(ctx, value).await
|
|
|
|
}
|
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
};
|
|
|
|
Ok(expanded.into())
|
|
|
|
}
|