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-03-19 09:20:12 +00:00
|
|
|
use crate::utils::{build_value_repr, check_reserved_name, get_crate_name};
|
2020-03-17 09:26:59 +00:00
|
|
|
use inflector::Inflector;
|
|
|
|
use proc_macro::TokenStream;
|
2020-05-01 23:57:34 +00:00
|
|
|
use proc_macro2::{Ident, Span};
|
2020-03-17 09:26:59 +00:00
|
|
|
use quote::quote;
|
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
|
|
|
check_reserved_name(&gql_typename, object_args.internal)?;
|
|
|
|
|
2020-03-17 09:26:59 +00:00
|
|
|
let desc = object_args
|
|
|
|
.desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.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()
|
|
|
|
.unwrap_or_else(|| method.sig.ident.to_string().to_camel_case());
|
|
|
|
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-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;
|
|
|
|
let mut arg_ctx = Ident::new("ctx", Span::call_site());
|
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;
|
|
|
|
match arg {
|
|
|
|
Pat::Wild(_) => {
|
|
|
|
pat.pat = Box::new(
|
|
|
|
syn::parse2::<Pat>(quote! { #arg_ctx })
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Pat::Ident(arg_ident) => {
|
|
|
|
arg_ctx = arg_ident.ident.clone();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
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 {
|
|
|
|
let arg = syn::parse2::<FnArg>(quote! { #arg_ctx: &#crate_name::Context<'_> })
|
|
|
|
.unwrap();
|
|
|
|
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-03-17 09:26:59 +00:00
|
|
|
},
|
|
|
|
) in args
|
|
|
|
{
|
|
|
|
let name = name
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| ident.ident.to_string().to_camel_case());
|
|
|
|
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! {
|
|
|
|
args.insert(#name, #crate_name::registry::InputValue {
|
|
|
|
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 });
|
|
|
|
|
|
|
|
let default = match &default {
|
|
|
|
Some(default) => {
|
|
|
|
let repr = build_value_repr(&crate_name, &default);
|
|
|
|
quote! {|| #repr }
|
|
|
|
}
|
|
|
|
None => quote! { || #crate_name::Value::Null },
|
|
|
|
};
|
|
|
|
|
|
|
|
get_params.push(quote! {
|
2020-04-06 05:49:39 +00:00
|
|
|
let #ident: #ty = ctx.param_value(#name, ctx.position, #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;
|
|
|
|
method.block =
|
|
|
|
syn::parse2::<Block>(quote!({ Ok(#block) })).expect("invalid block");
|
|
|
|
method.sig.output = syn::parse2::<ReturnType>(
|
|
|
|
quote! { -> #crate_name::FieldResult<#inner_ty> },
|
|
|
|
)
|
|
|
|
.expect("invalid result type");
|
|
|
|
}
|
|
|
|
|
2020-03-17 09:26:59 +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-17 09:26:59 +00:00
|
|
|
description: #field_desc,
|
|
|
|
args: {
|
|
|
|
let mut args = std::collections::HashMap::new();
|
|
|
|
#(#schema_args)*
|
|
|
|
args
|
|
|
|
},
|
2020-04-06 05:49:39 +00:00
|
|
|
ty: <#stream_ty as #crate_name::futures::stream::Stream>::Item::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.
|
|
|
|
map_err(|err| err.into_error_with_path(ctx.position, ctx.path_node.as_ref().unwrap().to_json()))?)
|
2020-04-23 02:26:16 +00:00
|
|
|
};
|
|
|
|
|
2020-05-01 23:57:34 +00:00
|
|
|
if let Some(guard) = &field.guard {
|
|
|
|
method.block.stmts.insert(
|
|
|
|
0,
|
|
|
|
syn::parse2(quote! { #guard.check(#arg_ctx).await?; })
|
|
|
|
.expect("invalid guard"),
|
|
|
|
);
|
|
|
|
}
|
2020-04-23 03:26:36 +00:00
|
|
|
|
2020-04-06 05:49:39 +00:00
|
|
|
create_stream.push(quote! {
|
|
|
|
if ctx.name.as_str() == #field_name {
|
|
|
|
let field_name = ctx.result_name().to_string();
|
2020-03-17 09:26:59 +00:00
|
|
|
#(#get_params)*
|
2020-04-06 05:49:39 +00:00
|
|
|
let field_selection_set = std::sync::Arc::new(ctx.selection_set.clone());
|
|
|
|
let schema = schema.clone();
|
|
|
|
let pos = ctx.position;
|
|
|
|
let environment = environment.clone();
|
2020-04-23 03:46:48 +00:00
|
|
|
let stream = #crate_name::futures::stream::StreamExt::then(#create_field_stream, move |msg| {
|
2020-04-06 05:49:39 +00:00
|
|
|
let environment = environment.clone();
|
|
|
|
let field_selection_set = field_selection_set.clone();
|
|
|
|
let schema = schema.clone();
|
|
|
|
async move {
|
|
|
|
let resolve_id = std::sync::atomic::AtomicUsize::default();
|
|
|
|
let ctx_selection_set = environment.create_context(
|
2020-04-23 02:26:16 +00:00
|
|
|
&schema,
|
2020-04-06 05:49:39 +00:00
|
|
|
Some(#crate_name::QueryPathNode {
|
|
|
|
parent: None,
|
|
|
|
segment: #crate_name::QueryPathSegment::Name("time"),
|
|
|
|
}),
|
2020-04-23 02:26:16 +00:00
|
|
|
&*field_selection_set,
|
2020-04-06 05:49:39 +00:00
|
|
|
&resolve_id,
|
|
|
|
);
|
|
|
|
#crate_name::OutputValueType::resolve(&msg, &ctx_selection_set, pos).await
|
|
|
|
}
|
|
|
|
}).
|
|
|
|
filter_map(move |res| {
|
|
|
|
let res = res.ok().map(|value| {
|
|
|
|
#crate_name::serde_json::json!({ &field_name: value })
|
|
|
|
});
|
|
|
|
async move { res }
|
|
|
|
});
|
|
|
|
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-04-28 10:05:06 +00:00
|
|
|
impl #generics #crate_name::Type for #self_ty #where_clause {
|
2020-03-17 09:26:59 +00:00
|
|
|
fn type_name() -> std::borrow::Cow<'static, str> {
|
|
|
|
std::borrow::Cow::Borrowed(#gql_typename)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
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-17 09:26:59 +00:00
|
|
|
description: #desc,
|
|
|
|
fields: {
|
|
|
|
let mut fields = std::collections::HashMap::new();
|
|
|
|
#(#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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-04-28 10:05:06 +00:00
|
|
|
impl #crate_name::SubscriptionType for #self_ty #where_clause {
|
2020-04-06 05:49:39 +00:00
|
|
|
#[allow(unused_variables)]
|
|
|
|
#[allow(bare_trait_objects)]
|
2020-04-07 06:30:46 +00:00
|
|
|
async fn create_field_stream<Query, Mutation>(
|
2020-04-06 05:49:39 +00:00
|
|
|
&self,
|
|
|
|
ctx: &#crate_name::Context<'_>,
|
|
|
|
schema: &#crate_name::Schema<Query, Mutation, Self>,
|
|
|
|
environment: std::sync::Arc<#crate_name::Environment>,
|
2020-04-23 08:08:32 +00:00
|
|
|
) -> #crate_name::Result<std::pin::Pin<Box<dyn #crate_name::futures::Stream<Item = #crate_name::serde_json::Value> + Send>>>
|
2020-04-06 05:49:39 +00:00
|
|
|
where
|
|
|
|
Query: #crate_name::ObjectType + Send + Sync + 'static,
|
|
|
|
Mutation: #crate_name::ObjectType + Send + Sync + 'static,
|
|
|
|
Self: Send + Sync + 'static + Sized,
|
|
|
|
{
|
2020-04-23 08:08:32 +00:00
|
|
|
use #crate_name::futures::StreamExt;
|
|
|
|
|
2020-04-06 05:49:39 +00:00
|
|
|
#(#create_stream)*
|
2020-04-02 02:21:04 +00:00
|
|
|
Err(#crate_name::QueryError::FieldNotFound {
|
2020-04-06 05:49:39 +00:00
|
|
|
field_name: ctx.name.clone(),
|
2020-03-18 03:13:45 +00:00
|
|
|
object: #gql_typename.to_string(),
|
2020-04-06 05:49:39 +00:00
|
|
|
}.into_error(ctx.position))
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(expanded.into())
|
|
|
|
}
|