2020-03-17 09:26:59 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::quote;
|
2022-04-19 04:25:11 +00:00
|
|
|
use syn::{
|
|
|
|
ext::IdentExt, Block, Error, ImplItem, ItemImpl, ReturnType, Type, TypeImplTrait,
|
|
|
|
TypeParamBound,
|
|
|
|
};
|
2020-03-17 09:26:59 +00:00
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
use crate::{
|
|
|
|
args::{self, ComplexityType, RenameRuleExt, RenameTarget, SubscriptionField},
|
|
|
|
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,
|
|
|
|
},
|
2020-10-16 02:45:27 +00:00
|
|
|
};
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
pub fn generate(
|
|
|
|
subscription_args: &args::Subscription,
|
|
|
|
item_impl: &mut ItemImpl,
|
|
|
|
) -> GeneratorResult<TokenStream> {
|
|
|
|
let crate_name = get_crate_name(subscription_args.internal);
|
2020-11-22 01:53:18 +00:00
|
|
|
let (self_ty, self_name) = get_type_path_and_name(item_impl.self_ty.as_ref())?;
|
2021-03-09 13:21:56 +00:00
|
|
|
let generics = &item_impl.generics;
|
|
|
|
let where_clause = &item_impl.generics.where_clause;
|
2021-03-09 07:12:14 +00:00
|
|
|
let extends = subscription_args.extends;
|
2020-03-17 09:26:59 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
let gql_typename = subscription_args
|
2020-03-17 09:26:59 +00:00
|
|
|
.name
|
|
|
|
.clone()
|
2020-10-14 09:08:57 +00:00
|
|
|
.unwrap_or_else(|| RenameTarget::Type.rename(self_name.clone()));
|
2020-03-19 09:20:12 +00:00
|
|
|
|
2020-10-26 13:34:05 +00:00
|
|
|
let desc = if subscription_args.use_type_description {
|
|
|
|
quote! { ::std::option::Option::Some(<Self as #crate_name::Description>::description()) }
|
|
|
|
} else {
|
|
|
|
get_rustdoc(&item_impl.attrs)?
|
|
|
|
.map(|s| quote!(::std::option::Option::Some(#s)))
|
|
|
|
.unwrap_or_else(|| quote!(::std::option::Option::None))
|
|
|
|
};
|
2020-03-17 09:26:59 +00:00
|
|
|
|
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-09-28 09:44:00 +00:00
|
|
|
let field: SubscriptionField = parse_graphql_attrs(&method.attrs)?.unwrap_or_default();
|
|
|
|
if field.skip {
|
|
|
|
remove_graphql_attrs(&mut method.attrs);
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-17 09:26:59 +00:00
|
|
|
|
2022-02-28 07:26:33 +00:00
|
|
|
let ident = method.sig.ident.clone();
|
2020-10-14 09:08:57 +00:00
|
|
|
let field_name = field.name.clone().unwrap_or_else(|| {
|
|
|
|
subscription_args
|
|
|
|
.rename_fields
|
|
|
|
.rename(method.sig.ident.unraw().to_string(), RenameTarget::Field)
|
|
|
|
});
|
2020-09-28 09:44:00 +00:00
|
|
|
let field_desc = get_rustdoc(&method.attrs)?
|
2020-10-16 10:37:59 +00:00
|
|
|
.map(|s| quote! {::std::option::Option::Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {::std::option::Option::None});
|
2021-02-27 03:59:58 +00:00
|
|
|
let field_deprecation = gen_deprecation(&field.deprecation, &crate_name);
|
2020-09-28 09:44:00 +00:00
|
|
|
let cfg_attrs = get_cfg_attrs(&method.attrs);
|
|
|
|
|
|
|
|
if method.sig.asyncness.is_none() {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
&method,
|
|
|
|
"The subscription stream function must be asynchronous",
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
2020-03-17 09:26:59 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
let mut schema_args = Vec::new();
|
|
|
|
let mut use_params = Vec::new();
|
|
|
|
let mut get_params = Vec::new();
|
2022-05-07 08:16:01 +00:00
|
|
|
let args = extract_input_args::<args::SubscriptionFieldArgument>(&crate_name, method)?;
|
2020-03-17 09:26:59 +00:00
|
|
|
|
2022-05-07 08:16:01 +00:00
|
|
|
for (
|
|
|
|
ident,
|
|
|
|
ty,
|
|
|
|
args::SubscriptionFieldArgument {
|
|
|
|
name,
|
|
|
|
desc,
|
|
|
|
default,
|
|
|
|
default_with,
|
|
|
|
validator,
|
|
|
|
process_with,
|
|
|
|
visible: arg_visible,
|
|
|
|
secret,
|
|
|
|
},
|
|
|
|
) in &args
|
|
|
|
{
|
|
|
|
let name = name.clone().unwrap_or_else(|| {
|
|
|
|
subscription_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)?;
|
2020-03-17 09:26:59 +00:00
|
|
|
|
2022-05-07 08:16:01 +00:00
|
|
|
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});
|
2020-05-01 23:57:34 +00:00
|
|
|
|
2022-05-07 08:16:01 +00:00
|
|
|
let visible = visible_fn(arg_visible);
|
2020-09-28 09:44:00 +00:00
|
|
|
schema_args.push(quote! {
|
2022-05-07 08:16:01 +00:00
|
|
|
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,
|
|
|
|
});
|
2020-03-17 09:26:59 +00:00
|
|
|
});
|
2022-05-07 08:16:01 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
use_params.push(quote! { #ident });
|
|
|
|
|
2022-05-07 08:16:01 +00:00
|
|
|
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;
|
2022-05-07 08:16:01 +00:00
|
|
|
let process_with = match process_with.as_ref() {
|
2022-04-06 07:10:56 +00:00
|
|
|
Some(fn_path) => {
|
|
|
|
let fn_path: syn::ExprPath = syn::parse_str(fn_path)?;
|
|
|
|
quote! {
|
|
|
|
#fn_path(&mut #param_ident);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Default::default(),
|
|
|
|
};
|
|
|
|
|
2022-05-07 08:16:01 +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;
|
2020-09-28 09:44:00 +00:00
|
|
|
get_params.push(quote! {
|
2022-05-07 08:16:01 +00:00
|
|
|
#[allow(non_snake_case, unused_mut)]
|
|
|
|
let (__pos, mut #non_mut_ident) = ctx.param_value::<#ty>(#name, #default)?;
|
2022-04-06 07:10:56 +00:00
|
|
|
#process_with
|
2021-11-14 13:09:14 +00:00
|
|
|
#validators
|
2022-05-07 08:16:01 +00:00
|
|
|
#[allow(non_snake_case)]
|
2022-04-06 07:10:56 +00:00
|
|
|
let #ident = #non_mut_ident;
|
2020-09-28 09:44:00 +00:00
|
|
|
});
|
|
|
|
}
|
2020-04-23 02:26:16 +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())
|
|
|
|
}
|
|
|
|
};
|
2020-09-28 09:44:00 +00:00
|
|
|
let res_ty = ty.value_type();
|
|
|
|
let stream_ty = if let Type::ImplTrait(TypeImplTrait { bounds, .. }) = &res_ty {
|
2020-10-12 07:04:46 +00:00
|
|
|
let mut r = None;
|
|
|
|
for b in bounds {
|
|
|
|
if let TypeParamBound::Trait(b) = b {
|
2021-10-21 16:54:07 +00:00
|
|
|
r = Some(quote! { dyn #b });
|
2020-10-12 07:04:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
quote! { #r }
|
2020-09-28 09:44:00 +00:00
|
|
|
} else {
|
|
|
|
quote! { #res_ty }
|
|
|
|
};
|
|
|
|
|
|
|
|
if let OutputType::Value(inner_ty) = &ty {
|
|
|
|
let block = &method.block;
|
|
|
|
let new_block = quote!({
|
|
|
|
{
|
|
|
|
let value = (move || { async move #block })().await;
|
2020-10-16 10:37:59 +00:00
|
|
|
::std::result::Result::Ok(value)
|
2020-09-28 09:44:00 +00:00
|
|
|
}
|
2020-05-05 05:02:24 +00:00
|
|
|
});
|
2020-09-28 09:44:00 +00:00
|
|
|
method.block = syn::parse2::<Block>(new_block).expect("invalid block");
|
|
|
|
method.sig.output =
|
2020-09-30 18:40:17 +00:00
|
|
|
syn::parse2::<ReturnType>(quote! { -> #crate_name::Result<#inner_ty> })
|
2020-09-28 09:44:00 +00:00
|
|
|
.expect("invalid result type");
|
|
|
|
}
|
2020-04-23 02:26:16 +00:00
|
|
|
|
2020-12-11 08:03:28 +00:00
|
|
|
let visible = visible_fn(&field.visible);
|
2020-12-19 10:39:33 +00:00
|
|
|
let complexity = if let Some(complexity) = &field.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::SubscriptionFieldArgument {
|
|
|
|
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)? {
|
2020-12-19 10:39:33 +00:00
|
|
|
Some(default) => {
|
|
|
|
quote! { ::std::option::Option::Some(|| -> #ty { #default }) }
|
|
|
|
}
|
|
|
|
None => quote! { ::std::option::Option::None },
|
|
|
|
};
|
|
|
|
let name = name.clone().unwrap_or_else(|| {
|
|
|
|
subscription_args.rename_args.rename(
|
|
|
|
ident.ident.unraw().to_string(),
|
|
|
|
RenameTarget::Argument,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
parse_args.push(quote! {
|
2021-11-15 01:12:13 +00:00
|
|
|
let #ident: #ty = __ctx.param_value(__variables_definition, __field, #name, #default)?;
|
|
|
|
});
|
2020-12-19 10:39:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
quote! {
|
|
|
|
Some(#crate_name::registry::ComplexityType::Fn(|__ctx, __variables_definition, __field, child_complexity| {
|
|
|
|
#(#parse_args)*
|
2021-04-04 04:05:54 +00:00
|
|
|
::std::result::Result::Ok(#expr)
|
2020-12-19 10:39:33 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quote! { ::std::option::Option::None }
|
|
|
|
};
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
schema_fields.push(quote! {
|
|
|
|
#(#cfg_attrs)*
|
2020-10-16 19:21:46 +00:00
|
|
|
fields.insert(::std::borrow::ToOwned::to_owned(#field_name), #crate_name::registry::MetaField {
|
|
|
|
name: ::std::borrow::ToOwned::to_owned(#field_name),
|
2020-09-28 09:44:00 +00:00
|
|
|
description: #field_desc,
|
|
|
|
args: {
|
|
|
|
let mut args = #crate_name::indexmap::IndexMap::new();
|
|
|
|
#(#schema_args)*
|
|
|
|
args
|
|
|
|
},
|
2021-11-09 10:52:10 +00:00
|
|
|
ty: <<#stream_ty as #crate_name::futures_util::stream::Stream>::Item as #crate_name::OutputType>::create_type_info(registry),
|
2020-09-28 09:44:00 +00:00
|
|
|
deprecation: #field_deprecation,
|
2020-10-16 10:37:59 +00:00
|
|
|
cache_control: ::std::default::Default::default(),
|
2020-09-28 09:44:00 +00:00
|
|
|
external: false,
|
2020-10-16 10:37:59 +00:00
|
|
|
requires: ::std::option::Option::None,
|
|
|
|
provides: ::std::option::Option::None,
|
2020-12-11 08:03:28 +00:00
|
|
|
visible: #visible,
|
2020-12-19 10:39:33 +00:00
|
|
|
compute_complexity: #complexity,
|
2020-05-05 05:02:24 +00:00
|
|
|
});
|
2020-09-28 09:44:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let create_field_stream = quote! {
|
|
|
|
self.#ident(ctx, #(#use_params),*)
|
|
|
|
.await
|
|
|
|
.map_err(|err| {
|
2021-06-07 06:03:36 +00:00
|
|
|
::std::convert::Into::<#crate_name::Error>::into(err).into_server_error(ctx.item.pos)
|
|
|
|
.with_path(::std::vec![#crate_name::PathSegment::Field(::std::borrow::ToOwned::to_owned(&*field_name))])
|
2021-11-15 01:12:13 +00:00
|
|
|
})
|
2020-09-28 09:44:00 +00:00
|
|
|
};
|
2020-04-23 03:26:36 +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)
|
|
|
|
.with_path(::std::vec![#crate_name::PathSegment::Field(::std::borrow::ToOwned::to_owned(&*field_name))])
|
|
|
|
})
|
|
|
|
};
|
2022-03-14 01:19:27 +00:00
|
|
|
let guard = match field.guard.as_ref().or(subscription_args.guard.as_ref()) {
|
2021-11-16 02:22:39 +00:00
|
|
|
Some(code) => Some(generate_guards(&crate_name, code, guard_map_err)?),
|
2020-09-28 09:44:00 +00:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
let stream_fn = quote! {
|
2021-04-04 04:05:54 +00:00
|
|
|
let field_name = ::std::clone::Clone::clone(&ctx.item.node.response_key().node);
|
2020-10-16 19:21:46 +00:00
|
|
|
let field = ::std::sync::Arc::new(::std::clone::Clone::clone(&ctx.item));
|
2021-11-15 01:12:13 +00:00
|
|
|
|
|
|
|
let f = async {
|
|
|
|
#(#get_params)*
|
|
|
|
#guard
|
|
|
|
#create_field_stream
|
|
|
|
};
|
|
|
|
let stream = f.await.map_err(|err| ctx.set_error_path(err))?;
|
2020-09-28 09:44:00 +00:00
|
|
|
|
|
|
|
let pos = ctx.item.pos;
|
2020-10-16 19:21:46 +00:00
|
|
|
let schema_env = ::std::clone::Clone::clone(&ctx.schema_env);
|
|
|
|
let query_env = ::std::clone::Clone::clone(&ctx.query_env);
|
2021-11-15 01:12:13 +00:00
|
|
|
let stream = #crate_name::futures_util::stream::StreamExt::then(stream, {
|
2020-10-16 19:21:46 +00:00
|
|
|
let field_name = ::std::clone::Clone::clone(&field_name);
|
2020-09-28 09:44:00 +00:00
|
|
|
move |msg| {
|
2020-10-16 19:21:46 +00:00
|
|
|
let schema_env = ::std::clone::Clone::clone(&schema_env);
|
|
|
|
let query_env = ::std::clone::Clone::clone(&query_env);
|
|
|
|
let field = ::std::clone::Clone::clone(&field);
|
|
|
|
let field_name = ::std::clone::Clone::clone(&field_name);
|
2020-09-28 09:44:00 +00:00
|
|
|
async move {
|
|
|
|
let ctx_selection_set = query_env.create_context(
|
|
|
|
&schema_env,
|
2020-10-16 10:37:59 +00:00
|
|
|
::std::option::Option::Some(#crate_name::QueryPathNode {
|
|
|
|
parent: ::std::option::Option::None,
|
2020-09-28 09:44:00 +00:00
|
|
|
segment: #crate_name::QueryPathSegment::Name(&field_name),
|
|
|
|
}),
|
|
|
|
&field.node.selection_set,
|
|
|
|
);
|
2020-09-13 17:52:36 +00:00
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
let mut execute_fut = async {
|
|
|
|
#[allow(bare_trait_objects)]
|
|
|
|
let ri = #crate_name::extensions::ResolveInfo {
|
|
|
|
path_node: ctx_selection_set.path_node.as_ref().unwrap(),
|
|
|
|
parent_type: #gql_typename,
|
2021-11-09 10:52:10 +00:00
|
|
|
return_type: &<<#stream_ty as #crate_name::futures_util::stream::Stream>::Item as #crate_name::OutputType>::qualified_type_name(),
|
2021-06-29 01:30:26 +00:00
|
|
|
name: field.node.name.node.as_str(),
|
|
|
|
alias: field.node.alias.as_ref().map(|alias| alias.node.as_str()),
|
2021-04-04 04:05:54 +00:00
|
|
|
};
|
|
|
|
let resolve_fut = async {
|
2021-06-07 12:51:20 +00:00
|
|
|
#crate_name::OutputType::resolve(&msg, &ctx_selection_set, &*field)
|
|
|
|
.await
|
|
|
|
.map(::std::option::Option::Some)
|
2021-04-04 04:05:54 +00:00
|
|
|
};
|
|
|
|
#crate_name::futures_util::pin_mut!(resolve_fut);
|
2021-06-07 06:03:36 +00:00
|
|
|
let mut resp = query_env.extensions.resolve(ri, &mut resolve_fut).await.map(|value| {
|
2021-10-26 10:57:39 +00:00
|
|
|
let mut map = #crate_name::indexmap::IndexMap::new();
|
2021-06-07 06:03:36 +00:00
|
|
|
map.insert(::std::clone::Clone::clone(&field_name), value.unwrap_or_default());
|
|
|
|
#crate_name::Response::new(#crate_name::Value::Object(map))
|
|
|
|
})
|
2021-06-07 12:51:20 +00:00
|
|
|
.unwrap_or_else(|err| #crate_name::Response::from_errors(::std::vec![err]));
|
|
|
|
|
|
|
|
use ::std::iter::Extend;
|
|
|
|
resp.errors.extend(::std::mem::take(&mut *query_env.errors.lock().unwrap()));
|
2021-06-07 06:03:36 +00:00
|
|
|
resp
|
2020-09-28 09:44:00 +00:00
|
|
|
};
|
2021-04-04 04:05:54 +00:00
|
|
|
#crate_name::futures_util::pin_mut!(execute_fut);
|
2021-06-10 02:17:33 +00:00
|
|
|
::std::result::Result::Ok(query_env.extensions.execute(query_env.operation_name.as_deref(), &mut execute_fut).await)
|
2020-09-13 17:52:36 +00:00
|
|
|
}
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
});
|
2021-12-07 03:00:53 +00:00
|
|
|
#crate_name::ServerResult::Ok(stream)
|
2020-09-28 09:44:00 +00:00
|
|
|
};
|
2020-03-17 09:26:59 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
create_stream.push(quote! {
|
|
|
|
#(#cfg_attrs)*
|
|
|
|
if ctx.item.node.name.node == #field_name {
|
2021-04-04 04:05:54 +00:00
|
|
|
let stream = #crate_name::futures_util::stream::TryStreamExt::try_flatten(
|
|
|
|
#crate_name::futures_util::stream::once((move || async move { #stream_fn })())
|
|
|
|
);
|
|
|
|
let stream = #crate_name::futures_util::StreamExt::map(stream, |res| match res {
|
|
|
|
::std::result::Result::Ok(resp) => resp,
|
|
|
|
::std::result::Result::Err(err) => #crate_name::Response::from_errors(::std::vec![err]),
|
|
|
|
});
|
|
|
|
return ::std::option::Option::Some(::std::boxed::Box::pin(stream));
|
2020-09-28 09:44:00 +00:00
|
|
|
}
|
|
|
|
});
|
2020-03-17 09:26:59 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
remove_graphql_attrs(&mut method.attrs);
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 03:49:31 +00:00
|
|
|
if create_stream.is_empty() {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
&self_ty,
|
2020-10-22 02:11:47 +00:00
|
|
|
"A GraphQL Object type must define one or more fields.",
|
2020-10-20 03:49:31 +00:00
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
2021-11-11 07:22:40 +00:00
|
|
|
let visible = visible_fn(&subscription_args.visible);
|
|
|
|
|
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)]
|
2021-11-09 10:52:10 +00:00
|
|
|
#[allow(unused_braces, unused_variables)]
|
|
|
|
impl #generics #crate_name::SubscriptionType for #self_ty #where_clause {
|
2020-10-16 10:37:59 +00:00
|
|
|
fn type_name() -> ::std::borrow::Cow<'static, ::std::primitive::str> {
|
2020-06-24 06:04:48 +00:00
|
|
|
::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-10-16 10:37:59 +00:00
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String {
|
2021-11-09 10:52:10 +00:00
|
|
|
registry.create_subscription_type::<Self, _>(|registry| #crate_name::registry::MetaType::Object {
|
2020-10-16 19:21:46 +00:00
|
|
|
name: ::std::borrow::ToOwned::to_owned(#gql_typename),
|
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-09-13 17:52:36 +00:00
|
|
|
cache_control: ::std::default::Default::default(),
|
2021-03-09 07:12:14 +00:00
|
|
|
extends: #extends,
|
2020-10-16 10:37:59 +00:00
|
|
|
keys: ::std::option::Option::None,
|
2021-11-11 07:22:40 +00:00
|
|
|
visible: #visible,
|
2021-10-28 08:56:08 +00:00
|
|
|
is_subscription: true,
|
2021-11-08 01:33:20 +00:00
|
|
|
rust_typename: ::std::any::type_name::<Self>(),
|
2020-03-17 09:26:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-14 22:27:31 +00:00
|
|
|
fn create_field_stream<'__life>(
|
|
|
|
&'__life self,
|
2021-02-24 04:06:11 +00:00
|
|
|
ctx: &'__life #crate_name::Context<'_>,
|
2021-04-04 04:05:54 +00:00
|
|
|
) -> ::std::option::Option<::std::pin::Pin<::std::boxed::Box<dyn #crate_name::futures_util::stream::Stream<Item = #crate_name::Response> + ::std::marker::Send + '__life>>> {
|
2020-04-06 05:49:39 +00:00
|
|
|
#(#create_stream)*
|
2020-10-16 10:37:59 +00:00
|
|
|
::std::option::Option::None
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-06-07 06:03:36 +00:00
|
|
|
|
2020-03-17 09:26:59 +00:00
|
|
|
Ok(expanded.into())
|
|
|
|
}
|