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-05-10 04:41:05 +00:00
|
|
|
use crate::utils::{build_value_repr, check_reserved_name, get_crate_name, get_rustdoc};
|
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-05-01 23:57:34 +00:00
|
|
|
use syn::{Block, 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-04-28 10:05:06 +00:00
|
|
|
let where_clause = &item_impl.generics.where_clause;
|
2020-04-09 14:03:09 +00:00
|
|
|
let extends = object_args.extends;
|
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
|
2020-05-10 04:41:05 +00:00
|
|
|
.clone()
|
|
|
|
.or_else(|| get_rustdoc(&item_impl.attrs).ok().flatten())
|
|
|
|
.map(|s| quote! { Some(#s) })
|
2020-03-03 11:15:18 +00:00
|
|
|
.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-04-09 14:03:09 +00:00
|
|
|
let mut find_entities = Vec::new();
|
|
|
|
let mut add_keys = Vec::new();
|
|
|
|
let mut create_entity_types = 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 {
|
2020-05-01 23:57:34 +00:00
|
|
|
if let Some(entity) = args::Entity::parse(&crate_name, &method.attrs)? {
|
2020-04-27 04:57:52 +00:00
|
|
|
let ty = match &method.sig.output {
|
|
|
|
ReturnType::Type(_, ty) => OutputType::parse(ty)?,
|
|
|
|
ReturnType::Default => {
|
|
|
|
return Err(Error::new_spanned(&method.sig.output, "Missing type"))
|
|
|
|
}
|
|
|
|
};
|
2020-05-01 23:57:34 +00:00
|
|
|
let mut create_ctx = true;
|
2020-04-27 04:57:52 +00:00
|
|
|
let mut args = Vec::new();
|
|
|
|
|
|
|
|
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.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
match (&*pat.pat, &*pat.ty) {
|
|
|
|
(Pat::Ident(arg_ident), Type::Path(arg_ty)) => {
|
|
|
|
args.push((
|
2020-05-01 23:57:34 +00:00
|
|
|
arg_ident.clone(),
|
|
|
|
arg_ty.clone(),
|
2020-04-27 04:57:52 +00:00
|
|
|
args::Argument::parse(&crate_name, &pat.attrs)?,
|
|
|
|
));
|
|
|
|
pat.attrs.clear();
|
|
|
|
}
|
2020-05-01 23:57:34 +00:00
|
|
|
(arg, Type::Reference(TypeReference { elem, .. })) => {
|
2020-04-27 04:57:52 +00:00
|
|
|
if let Type::Path(path) = elem.as_ref() {
|
|
|
|
if idx != 1
|
|
|
|
|| path.path.segments.last().unwrap().ident != "Context"
|
|
|
|
{
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
arg,
|
|
|
|
"The Context must be the second argument.",
|
|
|
|
));
|
2020-05-01 23:57:34 +00:00
|
|
|
} else {
|
|
|
|
create_ctx = false;
|
2020-04-27 04:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return Err(Error::new_spanned(arg, "Invalid argument type.")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 23:57:34 +00:00
|
|
|
if create_ctx {
|
2020-05-05 05:02:24 +00:00
|
|
|
let arg =
|
|
|
|
syn::parse2::<FnArg>(quote! { _: &#crate_name::Context<'_> }).unwrap();
|
2020-05-01 23:57:34 +00:00
|
|
|
method.sig.inputs.insert(1, arg);
|
|
|
|
}
|
|
|
|
|
2020-04-27 04:57:52 +00:00
|
|
|
let entity_type = ty.value_type();
|
|
|
|
let mut key_pat = Vec::new();
|
|
|
|
let mut key_getter = Vec::new();
|
|
|
|
let mut use_keys = Vec::new();
|
|
|
|
let mut keys = Vec::new();
|
|
|
|
let mut keys_str = String::new();
|
|
|
|
|
|
|
|
for (ident, ty, args::Argument { name, .. }) in &args {
|
|
|
|
let name = name
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| ident.ident.to_string().to_camel_case());
|
|
|
|
|
|
|
|
if !keys_str.is_empty() {
|
|
|
|
keys_str.push(' ');
|
|
|
|
}
|
|
|
|
keys_str.push_str(&name);
|
|
|
|
|
|
|
|
key_pat.push(quote! {
|
|
|
|
Some(#ident)
|
|
|
|
});
|
|
|
|
key_getter.push(quote! {
|
|
|
|
params.get(#name).and_then(|value| {
|
2020-05-11 13:47:24 +00:00
|
|
|
let value: Option<#ty> = #crate_name::InputValueType::parse(value.clone()).ok();
|
2020-04-27 04:57:52 +00:00
|
|
|
value
|
|
|
|
})
|
|
|
|
});
|
|
|
|
keys.push(name);
|
|
|
|
use_keys.push(ident);
|
|
|
|
}
|
|
|
|
add_keys.push(quote! { registry.add_keys(&<#entity_type as #crate_name::Type>::type_name(), #keys_str); });
|
|
|
|
create_entity_types.push(
|
|
|
|
quote! { <#entity_type as #crate_name::Type>::create_type_info(registry); },
|
|
|
|
);
|
|
|
|
|
|
|
|
let field_ident = &method.sig.ident;
|
2020-05-01 23:57:34 +00:00
|
|
|
if let OutputType::Value(inner_ty) = &ty {
|
|
|
|
let block = &method.block;
|
2020-05-03 23:59:30 +00:00
|
|
|
let new_block = quote!({
|
|
|
|
{
|
|
|
|
let value:#inner_ty = async move #block.await;
|
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
method.block = syn::parse2::<Block>(new_block).expect("invalid block");
|
2020-05-01 23:57:34 +00:00
|
|
|
method.sig.output = syn::parse2::<ReturnType>(
|
|
|
|
quote! { -> #crate_name::FieldResult<#inner_ty> },
|
|
|
|
)
|
|
|
|
.expect("invalid result type");
|
|
|
|
}
|
2020-05-09 09:55:04 +00:00
|
|
|
let do_find = quote! { self.#field_ident(ctx, #(#use_keys),*).await.map_err(|err| err.into_error(ctx.position()))? };
|
2020-05-01 23:57:34 +00:00
|
|
|
|
2020-05-05 05:02:24 +00:00
|
|
|
let guard = entity.guard.map(
|
2020-05-09 09:55:04 +00:00
|
|
|
|guard| quote! { #guard.check(ctx).await.map_err(|err| err.into_error(ctx.position()))?; },
|
2020-05-05 05:02:24 +00:00
|
|
|
);
|
2020-04-27 04:57:52 +00:00
|
|
|
|
|
|
|
find_entities.push((
|
|
|
|
args.len(),
|
|
|
|
quote! {
|
|
|
|
if typename == &<#entity_type as #crate_name::Type>::type_name() {
|
|
|
|
if let (#(#key_pat),*) = (#(#key_getter),*) {
|
2020-05-05 05:02:24 +00:00
|
|
|
#guard
|
2020-04-27 04:57:52 +00:00
|
|
|
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
2020-05-09 09:55:04 +00:00
|
|
|
return #crate_name::OutputValueType::resolve(&#do_find, &ctx_obj, ctx.position()).await;
|
2020-04-27 04:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
));
|
|
|
|
|
|
|
|
method.attrs.remove(
|
|
|
|
method
|
|
|
|
.attrs
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.find(|(_, a)| a.path.is_ident("entity"))
|
|
|
|
.map(|(idx, _)| idx)
|
|
|
|
.unwrap(),
|
|
|
|
);
|
2020-05-01 23:57:34 +00:00
|
|
|
} else if let Some(field) = args::Field::parse(&crate_name, &method.attrs)? {
|
2020-03-28 12:11:26 +00:00
|
|
|
if method.sig.asyncness.is_none() {
|
2020-05-01 23:57:34 +00:00
|
|
|
return Err(Error::new_spanned(&method, "Must be asynchronous"));
|
2020-03-28 12:11:26 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 06:23:55 +00:00
|
|
|
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});
|
2020-04-09 14:03:09 +00:00
|
|
|
let external = field.external;
|
|
|
|
let requires = match &field.requires {
|
|
|
|
Some(requires) => quote! { Some(#requires) },
|
|
|
|
None => quote! { None },
|
|
|
|
};
|
|
|
|
let provides = match &field.provides {
|
|
|
|
Some(provides) => quote! { Some(#provides) },
|
|
|
|
None => quote! { None },
|
|
|
|
};
|
2020-03-05 06:23:55 +00:00
|
|
|
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
|
|
|
|
2020-05-01 23:57:34 +00:00
|
|
|
let mut create_ctx = true;
|
2020-03-05 06:23:55 +00:00
|
|
|
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((
|
2020-05-01 23:57:34 +00:00
|
|
|
arg_ident.clone(),
|
|
|
|
arg_ty.clone(),
|
2020-03-21 07:07:11 +00:00
|
|
|
args::Argument::parse(&crate_name, &pat.attrs)?,
|
|
|
|
));
|
2020-03-05 06:23:55 +00:00
|
|
|
pat.attrs.clear();
|
|
|
|
}
|
2020-05-01 23:57:34 +00:00
|
|
|
(arg, Type::Reference(TypeReference { elem, .. })) => {
|
2020-03-05 06:23:55 +00:00
|
|
|
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.",
|
|
|
|
));
|
|
|
|
}
|
2020-05-01 23:57:34 +00:00
|
|
|
|
|
|
|
create_ctx = false;
|
2020-03-05 06:23:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return Err(Error::new_spanned(arg, "Invalid argument type.")),
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 02:38:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 23:57:34 +00:00
|
|
|
if create_ctx {
|
2020-05-05 05:02:24 +00:00
|
|
|
let arg =
|
|
|
|
syn::parse2::<FnArg>(quote! { _: &#crate_name::Context<'_> }).unwrap();
|
2020-05-01 23:57:34 +00:00
|
|
|
method.sig.inputs.insert(1, arg);
|
|
|
|
}
|
|
|
|
|
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-05-09 09:55:04 +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-05-09 09:55:04 +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-04-09 14:03:09 +00:00
|
|
|
external: #external,
|
|
|
|
provides: #provides,
|
|
|
|
requires: #requires,
|
2020-03-08 12:35:36 +00:00
|
|
|
});
|
2020-03-02 11:25:21 +00:00
|
|
|
});
|
2020-03-05 06:23:55 +00:00
|
|
|
|
|
|
|
let field_ident = &method.sig.ident;
|
2020-05-01 23:57:34 +00:00
|
|
|
if let OutputType::Value(inner_ty) = &ty {
|
|
|
|
let block = &method.block;
|
2020-05-03 23:59:30 +00:00
|
|
|
let new_block = quote!({
|
|
|
|
{
|
|
|
|
let value:#inner_ty = async move #block.await;
|
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
method.block = syn::parse2::<Block>(new_block).expect("invalid block");
|
2020-05-01 23:57:34 +00:00
|
|
|
method.sig.output = syn::parse2::<ReturnType>(
|
|
|
|
quote! { -> #crate_name::FieldResult<#inner_ty> },
|
|
|
|
)
|
|
|
|
.expect("invalid result type");
|
|
|
|
}
|
|
|
|
let resolve_obj = quote! {
|
|
|
|
{
|
2020-05-05 05:02:24 +00:00
|
|
|
let res = self.#field_ident(ctx, #(#use_params),*).await;
|
2020-05-09 09:55:04 +00:00
|
|
|
res.map_err(|err| err.into_error_with_path(ctx.position(), ctx.path_node.as_ref().unwrap().to_json()))?
|
2020-03-05 06:23:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-05 05:02:24 +00:00
|
|
|
let guard = field
|
|
|
|
.guard
|
|
|
|
.map(|guard| quote! {
|
|
|
|
#guard.check(ctx).await
|
2020-05-09 09:55:04 +00:00
|
|
|
.map_err(|err| err.into_error_with_path(ctx.position(), ctx.path_node.as_ref().unwrap().to_json()))?;
|
2020-05-05 05:02:24 +00:00
|
|
|
});
|
|
|
|
|
2020-03-05 06:23:55 +00:00
|
|
|
resolvers.push(quote! {
|
2020-05-12 08:27:06 +00:00
|
|
|
if ctx.name.node == #field_name {
|
2020-05-05 13:06:48 +00:00
|
|
|
use #crate_name::OutputValueType;
|
2020-03-05 06:23:55 +00:00
|
|
|
#(#get_params)*
|
2020-05-09 10:34:57 +00:00
|
|
|
#guard
|
2020-04-26 07:12:18 +00:00
|
|
|
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
2020-05-09 09:55:04 +00:00
|
|
|
return OutputValueType::resolve(&#resolve_obj, &ctx_obj, ctx.position()).await;
|
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-04-27 06:14:29 +00:00
|
|
|
if let Some((idx, _)) = method
|
|
|
|
.attrs
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.find(|(_, a)| a.path.is_ident("field"))
|
|
|
|
{
|
|
|
|
method.attrs.remove(idx);
|
|
|
|
}
|
2020-04-28 07:01:19 +00:00
|
|
|
} else if let Some((idx, _)) = method
|
|
|
|
.attrs
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.find(|(_, a)| a.path.is_ident("field"))
|
|
|
|
{
|
|
|
|
method.attrs.remove(idx);
|
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-04-09 14:03:09 +00:00
|
|
|
find_entities.sort_by(|(a, _), (b, _)| b.cmp(a));
|
|
|
|
let find_entities_iter = find_entities.iter().map(|(_, code)| code);
|
|
|
|
|
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-04-28 10:05:06 +00:00
|
|
|
impl #generics #crate_name::Type for #self_ty #where_clause {
|
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-04-09 14:03:09 +00:00
|
|
|
let ty = 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-04-09 14:03:09 +00:00
|
|
|
extends: #extends,
|
|
|
|
keys: None,
|
|
|
|
});
|
|
|
|
#(#create_entity_types)*
|
|
|
|
#(#add_keys)*
|
|
|
|
ty
|
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-04-28 10:05:06 +00:00
|
|
|
impl#generics #crate_name::ObjectType for #self_ty #where_clause {
|
2020-04-26 07:12:18 +00:00
|
|
|
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
2020-03-06 15:58:43 +00:00
|
|
|
#(#resolvers)*
|
2020-04-02 02:21:04 +00:00
|
|
|
Err(#crate_name::QueryError::FieldNotFound {
|
2020-05-12 08:27:06 +00:00
|
|
|
field_name: ctx.name.to_string(),
|
2020-03-06 15:58:43 +00:00
|
|
|
object: #gql_typename.to_string(),
|
2020-05-09 09:55:04 +00:00
|
|
|
}.into_error(ctx.position()))
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
2020-04-09 14:03:09 +00:00
|
|
|
|
2020-05-09 09:55:04 +00:00
|
|
|
async fn find_entity(&self, ctx: &#crate_name::Context<'_>, params: &#crate_name::Value) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
2020-04-09 14:03:09 +00:00
|
|
|
let params = match params {
|
|
|
|
#crate_name::Value::Object(params) => params,
|
2020-05-09 09:55:04 +00:00
|
|
|
_ => return Err(#crate_name::QueryError::EntityNotFound.into_error(ctx.position())),
|
2020-04-09 14:03:09 +00:00
|
|
|
};
|
|
|
|
let typename = if let Some(#crate_name::Value::String(typename)) = params.get("__typename") {
|
|
|
|
typename
|
|
|
|
} else {
|
2020-05-09 09:55:04 +00:00
|
|
|
return Err(#crate_name::QueryError::TypeNameNotExists.into_error(ctx.position()));
|
2020-04-09 14:03:09 +00:00
|
|
|
};
|
|
|
|
#(#find_entities_iter)*
|
2020-05-09 09:55:04 +00:00
|
|
|
Err(#crate_name::QueryError::EntityNotFound.into_error(ctx.position()))
|
2020-04-09 14:03:09 +00:00
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
2020-03-19 09:20:12 +00:00
|
|
|
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-04-28 10:05:06 +00:00
|
|
|
impl #generics #crate_name::OutputValueType for #self_ty #where_clause {
|
2020-05-05 13:06:48 +00:00
|
|
|
async fn resolve(&self, ctx: &#crate_name::ContextSelectionSet<'_>, pos: #crate_name::Pos) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
|
|
|
#crate_name::do_resolve(ctx, self).await
|
2020-03-19 09:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
};
|
|
|
|
Ok(expanded.into())
|
|
|
|
}
|