2020-03-17 09:26:59 +00:00
|
|
|
use crate::args;
|
2020-04-23 03:26:36 +00:00
|
|
|
use crate::output_type::OutputType;
|
2020-06-08 02:30:40 +00:00
|
|
|
use crate::utils::{feature_block, get_crate_name, get_param_getter_ident, get_rustdoc};
|
2020-03-17 09:26:59 +00:00
|
|
|
use inflector::Inflector;
|
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::quote;
|
2020-07-02 02:24:54 +00:00
|
|
|
use syn::ext::IdentExt;
|
2020-04-23 02:26:16 +00:00
|
|
|
use syn::{
|
2020-05-01 23:57:34 +00:00
|
|
|
Block, Error, FnArg, ImplItem, ItemImpl, Pat, Result, ReturnType, Type, TypeImplTrait,
|
|
|
|
TypeReference,
|
2020-04-23 02:26:16 +00:00
|
|
|
};
|
2020-03-17 09:26:59 +00:00
|
|
|
|
|
|
|
pub fn generate(object_args: &args::Object, item_impl: &mut ItemImpl) -> Result<TokenStream> {
|
|
|
|
let crate_name = get_crate_name(object_args.internal);
|
|
|
|
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")),
|
|
|
|
};
|
|
|
|
let generics = &item_impl.generics;
|
2020-04-28 10:05:06 +00:00
|
|
|
let where_clause = &generics.where_clause;
|
2020-03-17 09:26:59 +00:00
|
|
|
|
|
|
|
let gql_typename = object_args
|
|
|
|
.name
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| self_name.clone());
|
2020-03-19 09:20:12 +00:00
|
|
|
|
2020-03-17 09:26:59 +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-17 09:26:59 +00:00
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
|
2020-04-06 05:49:39 +00:00
|
|
|
let mut create_stream = Vec::new();
|
2020-03-17 09:26:59 +00:00
|
|
|
let mut schema_fields = Vec::new();
|
|
|
|
|
|
|
|
for item in &mut item_impl.items {
|
|
|
|
if let ImplItem::Method(method) = item {
|
2020-05-01 23:57:34 +00:00
|
|
|
if let Some(field) = args::Field::parse(&crate_name, &method.attrs)? {
|
2020-03-17 09:26:59 +00:00
|
|
|
let ident = &method.sig.ident;
|
|
|
|
let field_name = field
|
|
|
|
.name
|
|
|
|
.clone()
|
2020-07-02 02:24:54 +00:00
|
|
|
.unwrap_or_else(|| method.sig.ident.unraw().to_string().to_camel_case());
|
2020-03-17 09:26:59 +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-06-05 13:06:53 +00:00
|
|
|
let features = field.features;
|
2020-03-17 09:26:59 +00:00
|
|
|
|
2020-04-07 06:30:46 +00:00
|
|
|
if method.sig.asyncness.is_none() {
|
2020-03-17 09:26:59 +00:00
|
|
|
return Err(Error::new_spanned(
|
2020-05-01 23:57:34 +00:00
|
|
|
&method,
|
2020-04-07 06:30:46 +00:00
|
|
|
"The subscription stream function must be asynchronous",
|
2020-03-17 09:26:59 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-04-23 03:26:36 +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-03-17 09:26:59 +00:00
|
|
|
let mut args = Vec::new();
|
|
|
|
|
2020-04-23 02:26:16 +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-17 09:26:59 +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-17 09:26:59 +00:00
|
|
|
pat.attrs.clear();
|
|
|
|
}
|
2020-05-01 23:57:34 +00:00
|
|
|
(arg, Type::Reference(TypeReference { elem, .. })) => {
|
2020-04-23 02:26:16 +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-23 02:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 09:26:59 +00:00
|
|
|
_ => {
|
|
|
|
return Err(Error::new_spanned(arg, "Incorrect argument type"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(Error::new_spanned(arg, "Incorrect 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-03-17 09:26:59 +00:00
|
|
|
let mut schema_args = Vec::new();
|
|
|
|
let mut use_params = Vec::new();
|
|
|
|
let mut get_params = Vec::new();
|
|
|
|
|
|
|
|
for (
|
|
|
|
ident,
|
|
|
|
ty,
|
|
|
|
args::Argument {
|
|
|
|
name,
|
|
|
|
desc,
|
|
|
|
default,
|
2020-03-22 01:34:32 +00:00
|
|
|
validator,
|
2020-06-19 04:37:52 +00:00
|
|
|
..
|
2020-03-17 09:26:59 +00:00
|
|
|
},
|
|
|
|
) in args
|
|
|
|
{
|
|
|
|
let name = name
|
|
|
|
.clone()
|
2020-07-02 02:24:54 +00:00
|
|
|
.unwrap_or_else(|| ident.ident.unraw().to_string().to_camel_case());
|
2020-03-17 09:26:59 +00:00
|
|
|
let desc = desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
let schema_default = default
|
|
|
|
.as_ref()
|
2020-05-26 10:34:43 +00:00
|
|
|
.map(|value| {
|
|
|
|
quote! {Some( <#ty as #crate_name::InputValueType>::to_value(&#value).to_string() )}
|
2020-03-17 09:26:59 +00:00
|
|
|
})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
|
|
|
|
schema_args.push(quote! {
|
2020-05-15 02:08:37 +00:00
|
|
|
args.insert(#name, #crate_name::registry::MetaInputValue {
|
2020-03-17 09:26:59 +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-17 09:26:59 +00:00
|
|
|
default_value: #schema_default,
|
2020-03-22 01:34:32 +00:00
|
|
|
validator: #validator,
|
2020-03-17 09:26:59 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
use_params.push(quote! { #ident });
|
|
|
|
|
2020-05-26 10:34:43 +00:00
|
|
|
let default = match default {
|
|
|
|
Some(default) => quote! { Some(|| -> #ty { #default }) },
|
|
|
|
None => quote! { None },
|
2020-03-17 09:26:59 +00:00
|
|
|
};
|
2020-06-03 06:50:06 +00:00
|
|
|
let param_getter_name = get_param_getter_ident(&ident.ident.to_string());
|
2020-03-17 09:26:59 +00:00
|
|
|
get_params.push(quote! {
|
2020-06-03 06:50:06 +00:00
|
|
|
let #param_getter_name = || -> #crate_name::Result<#ty> { ctx.param_value(#name, #default) };
|
2020-05-09 09:55:04 +00:00
|
|
|
let #ident: #ty = ctx.param_value(#name, #default)?;
|
2020-03-17 09:26:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-23 03:26:36 +00:00
|
|
|
let res_ty = ty.value_type();
|
|
|
|
let stream_ty = if let Type::ImplTrait(TypeImplTrait { bounds, .. }) = &res_ty {
|
|
|
|
quote! { #bounds }
|
|
|
|
} else {
|
|
|
|
quote! { #res_ty }
|
2020-04-06 05:49:39 +00:00
|
|
|
};
|
|
|
|
|
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 = (move || { 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-06-06 03:49:29 +00:00
|
|
|
method.block =
|
|
|
|
syn::parse2::<Block>(feature_block(&crate_name, &features, &field_name, {
|
|
|
|
let block = &method.block;
|
|
|
|
quote! { #block }
|
|
|
|
}))
|
|
|
|
.expect("invalid block");
|
2020-06-05 13:06:53 +00:00
|
|
|
|
2020-03-17 09:26:59 +00:00
|
|
|
schema_fields.push(quote! {
|
2020-05-15 02:08:37 +00:00
|
|
|
fields.insert(#field_name.to_string(), #crate_name::registry::MetaField {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: #field_name.to_string(),
|
2020-03-17 09:26:59 +00:00
|
|
|
description: #field_desc,
|
|
|
|
args: {
|
2020-05-16 02:05:48 +00:00
|
|
|
let mut args = #crate_name::indexmap::IndexMap::new();
|
2020-03-17 09:26:59 +00:00
|
|
|
#(#schema_args)*
|
|
|
|
args
|
|
|
|
},
|
2020-05-12 08:27:06 +00:00
|
|
|
ty: <<#stream_ty as #crate_name::futures::stream::Stream>::Item as #crate_name::Type>::create_type_info(registry),
|
2020-03-17 09:26:59 +00:00
|
|
|
deprecation: #field_deprecation,
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: Default::default(),
|
2020-04-09 14:03:09 +00:00
|
|
|
external: false,
|
|
|
|
requires: None,
|
|
|
|
provides: None,
|
2020-03-17 09:26:59 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-01 23:57:34 +00:00
|
|
|
let create_field_stream = quote! {
|
|
|
|
#crate_name::futures::stream::StreamExt::fuse(self.#ident(ctx, #(#use_params),*).await.
|
2020-07-15 10:05:24 +00:00
|
|
|
map_err(|err| err.into_error_with_path(ctx.position(), ctx.path_node.as_ref()))?)
|
2020-04-23 02:26:16 +00:00
|
|
|
};
|
|
|
|
|
2020-05-05 05:02:24 +00:00
|
|
|
let guard = field.guard.map(|guard| quote! {
|
2020-07-15 10:05:24 +00:00
|
|
|
#guard.check(ctx).await.map_err(|err| err.into_error_with_path(ctx.position(), ctx.path_node.as_ref()))?;
|
2020-05-05 05:02:24 +00:00
|
|
|
});
|
2020-06-03 06:50:06 +00:00
|
|
|
if field.post_guard.is_some() {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
method,
|
|
|
|
"The subscription field does not support post guard",
|
|
|
|
));
|
|
|
|
}
|
2020-04-23 03:26:36 +00:00
|
|
|
|
2020-04-06 05:49:39 +00:00
|
|
|
create_stream.push(quote! {
|
2020-09-06 05:38:31 +00:00
|
|
|
if ctx.node.name.node == #field_name {
|
2020-05-09 10:34:57 +00:00
|
|
|
#(#get_params)*
|
2020-05-05 05:02:24 +00:00
|
|
|
#guard
|
2020-09-06 05:38:31 +00:00
|
|
|
let field_name = ::std::sync::Arc::new(ctx.item.node.response_key().node.clone());
|
2020-06-24 06:04:48 +00:00
|
|
|
let field = ::std::sync::Arc::new(ctx.item.clone());
|
2020-05-12 08:27:06 +00:00
|
|
|
|
2020-05-09 09:55:04 +00:00
|
|
|
let pos = ctx.position();
|
2020-05-19 08:24:15 +00:00
|
|
|
let schema_env = schema_env.clone();
|
|
|
|
let query_env = query_env.clone();
|
2020-06-30 08:14:09 +00:00
|
|
|
let stream = #crate_name::futures::StreamExt::then(#create_field_stream, {
|
2020-05-03 08:02:46 +00:00
|
|
|
let field_name = field_name.clone();
|
|
|
|
move |msg| {
|
2020-05-19 08:24:15 +00:00
|
|
|
let schema_env = schema_env.clone();
|
|
|
|
let query_env = query_env.clone();
|
2020-05-20 00:18:28 +00:00
|
|
|
let field = field.clone();
|
2020-05-03 08:02:46 +00:00
|
|
|
let field_name = field_name.clone();
|
|
|
|
async move {
|
2020-06-24 06:04:48 +00:00
|
|
|
let resolve_id = ::std::sync::atomic::AtomicUsize::default();
|
2020-05-19 08:24:15 +00:00
|
|
|
let ctx_selection_set = query_env.create_context(
|
|
|
|
&schema_env,
|
2020-05-03 08:02:46 +00:00
|
|
|
Some(#crate_name::QueryPathNode {
|
|
|
|
parent: None,
|
|
|
|
segment: #crate_name::QueryPathSegment::Name(&field_name),
|
|
|
|
}),
|
2020-09-06 05:38:31 +00:00
|
|
|
&field.node.selection_set,
|
2020-05-03 08:02:46 +00:00
|
|
|
&resolve_id,
|
|
|
|
);
|
2020-05-20 00:18:28 +00:00
|
|
|
#crate_name::OutputValueType::resolve(&msg, &ctx_selection_set, &*field).await
|
2020-05-03 08:02:46 +00:00
|
|
|
}
|
2020-04-06 05:49:39 +00:00
|
|
|
}
|
2020-06-30 08:14:09 +00:00
|
|
|
});
|
|
|
|
let stream = #crate_name::futures::TryStreamExt::map_ok(stream, move |value| #crate_name::serde_json::json!({ field_name.as_str(): value }));
|
|
|
|
let stream = #crate_name::futures::StreamExt::scan(stream, true, |state, item| {
|
2020-05-03 13:21:54 +00:00
|
|
|
if !*state {
|
|
|
|
return #crate_name::futures::future::ready(None);
|
|
|
|
}
|
2020-09-10 08:39:43 +00:00
|
|
|
let resp = match item {
|
|
|
|
Ok(value) => #crate_name::Response {
|
|
|
|
data: value,
|
|
|
|
extensions: None,
|
|
|
|
cache_control: Default::default(),
|
|
|
|
error: None,
|
|
|
|
}
|
|
|
|
Err(err) => err.into(),
|
|
|
|
};
|
|
|
|
if resp.is_err() {
|
2020-05-03 13:21:54 +00:00
|
|
|
*state = false;
|
|
|
|
}
|
2020-09-10 08:39:43 +00:00
|
|
|
return #crate_name::futures::future::ready(Some(resp));
|
2020-05-03 13:21:54 +00:00
|
|
|
});
|
2020-04-06 05:49:39 +00:00
|
|
|
return Ok(Box::pin(stream));
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
});
|
2020-04-27 06:14:29 +00:00
|
|
|
}
|
2020-03-17 09:26:59 +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-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
#item_impl
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-04-28 10:05:06 +00:00
|
|
|
impl #generics #crate_name::Type for #self_ty #where_clause {
|
2020-06-24 06:04:48 +00:00
|
|
|
fn type_name() -> ::std::borrow::Cow<'static, str> {
|
|
|
|
::std::borrow::Cow::Borrowed(#gql_typename)
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 05:49:39 +00:00
|
|
|
#[allow(bare_trait_objects)]
|
2020-03-17 09:26:59 +00:00
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> String {
|
2020-05-15 02:08:37 +00:00
|
|
|
registry.create_type::<Self, _>(|registry| #crate_name::registry::MetaType::Object {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: #gql_typename.to_string(),
|
2020-03-17 09:26:59 +00:00
|
|
|
description: #desc,
|
|
|
|
fields: {
|
2020-05-16 02:05:48 +00:00
|
|
|
let mut fields = #crate_name::indexmap::IndexMap::new();
|
2020-03-17 09:26:59 +00:00
|
|
|
#(#schema_fields)*
|
|
|
|
fields
|
|
|
|
},
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: Default::default(),
|
2020-04-09 14:03:09 +00:00
|
|
|
extends: false,
|
|
|
|
keys: None,
|
2020-03-17 09:26:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 06:28:53 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2020-03-17 09:26:59 +00:00
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-07-03 01:41:26 +00:00
|
|
|
#[allow(unused_braces, unused_variables)]
|
2020-04-28 10:05:06 +00:00
|
|
|
impl #crate_name::SubscriptionType for #self_ty #where_clause {
|
2020-05-19 08:24:15 +00:00
|
|
|
async fn create_field_stream(
|
2020-04-06 05:49:39 +00:00
|
|
|
&self,
|
2020-05-12 08:27:06 +00:00
|
|
|
idx: usize,
|
2020-04-06 05:49:39 +00:00
|
|
|
ctx: &#crate_name::Context<'_>,
|
2020-05-19 08:24:15 +00:00
|
|
|
schema_env: #crate_name::SchemaEnv,
|
|
|
|
query_env: #crate_name::QueryEnv,
|
2020-09-10 08:39:43 +00:00
|
|
|
) -> #crate_name::Result<::std::pin::Pin<Box<dyn #crate_name::futures::Stream<Item = #crate_name::Response> + Send>>> {
|
2020-04-06 05:49:39 +00:00
|
|
|
#(#create_stream)*
|
2020-04-02 02:21:04 +00:00
|
|
|
Err(#crate_name::QueryError::FieldNotFound {
|
2020-09-06 05:38:31 +00:00
|
|
|
field_name: ctx.node.name.to_string(),
|
2020-03-18 03:13:45 +00:00
|
|
|
object: #gql_typename.to_string(),
|
2020-05-09 09:55:04 +00:00
|
|
|
}.into_error(ctx.position()))
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(expanded.into())
|
|
|
|
}
|