2020-09-28 09:44:00 +00:00
|
|
|
use darling::ast::Data;
|
2020-03-27 02:20:20 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use quote::quote;
|
2021-10-25 10:56:33 +00:00
|
|
|
use std::str::FromStr;
|
2020-07-02 02:24:54 +00:00
|
|
|
use syn::ext::IdentExt;
|
2022-01-05 03:52:02 +00:00
|
|
|
use syn::visit::Visit;
|
|
|
|
use syn::{Error, Ident, LifetimeDef, Path, Type};
|
2020-03-27 02:20:20 +00:00
|
|
|
|
2021-10-25 10:56:33 +00:00
|
|
|
use crate::args::{self, RenameRuleExt, RenameTarget, SimpleObjectField};
|
2021-02-27 03:59:58 +00:00
|
|
|
use crate::utils::{
|
2021-10-28 10:22:39 +00:00
|
|
|
gen_deprecation, generate_guards, get_crate_name, get_rustdoc, visible_fn, GeneratorResult,
|
2021-02-27 03:59:58 +00:00
|
|
|
};
|
2020-10-16 02:45:27 +00:00
|
|
|
|
2021-10-27 13:37:13 +00:00
|
|
|
#[derive(Debug)]
|
2021-10-25 10:56:33 +00:00
|
|
|
struct DerivedFieldMetadata {
|
|
|
|
ident: Ident,
|
|
|
|
into: Type,
|
2021-10-27 13:37:13 +00:00
|
|
|
owned: Option<bool>,
|
2021-10-28 10:22:39 +00:00
|
|
|
with: Option<Path>,
|
2021-10-25 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SimpleObjectFieldGenerator<'a> {
|
|
|
|
field: &'a SimpleObjectField,
|
|
|
|
derived: Option<DerivedFieldMetadata>,
|
|
|
|
}
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
pub fn generate(object_args: &args::SimpleObject) -> GeneratorResult<TokenStream> {
|
2020-03-27 02:20:20 +00:00
|
|
|
let crate_name = get_crate_name(object_args.internal);
|
2020-09-28 09:44:00 +00:00
|
|
|
let ident = &object_args.ident;
|
2021-01-14 03:27:15 +00:00
|
|
|
let (impl_generics, ty_generics, where_clause) = object_args.generics.split_for_impl();
|
2020-04-09 14:03:09 +00:00
|
|
|
let extends = object_args.extends;
|
2020-03-27 02:20:20 +00:00
|
|
|
let gql_typename = object_args
|
|
|
|
.name
|
|
|
|
.clone()
|
2020-10-14 09:08:57 +00:00
|
|
|
.unwrap_or_else(|| RenameTarget::Type.rename(ident.to_string()));
|
2020-03-27 02:20:20 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
let desc = get_rustdoc(&object_args.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});
|
2020-03-27 02:20:20 +00:00
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
let s = match &object_args.data {
|
2020-05-10 04:41:05 +00:00
|
|
|
Data::Struct(e) => e,
|
2020-09-28 09:44:00 +00:00
|
|
|
_ => {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
&ident,
|
|
|
|
"SimpleObject can only be applied to an struct.",
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-05-10 04:41:05 +00:00
|
|
|
};
|
2020-04-21 07:27:43 +00:00
|
|
|
let mut getters = Vec::new();
|
2020-03-27 02:20:20 +00:00
|
|
|
let mut resolvers = Vec::new();
|
|
|
|
let mut schema_fields = Vec::new();
|
|
|
|
|
2021-10-25 10:56:33 +00:00
|
|
|
let mut processed_fields: Vec<SimpleObjectFieldGenerator> = vec![];
|
|
|
|
|
|
|
|
// Before processing the fields, we generate the derivated fields
|
2020-09-28 09:44:00 +00:00
|
|
|
for field in &s.fields {
|
2021-10-25 10:56:33 +00:00
|
|
|
processed_fields.push(SimpleObjectFieldGenerator {
|
2021-10-26 10:57:39 +00:00
|
|
|
field,
|
2021-10-25 10:56:33 +00:00
|
|
|
derived: None,
|
|
|
|
});
|
|
|
|
|
|
|
|
for derived in &field.derived {
|
|
|
|
if derived.name.is_some() && derived.into.is_some() {
|
|
|
|
let name = derived.name.clone().unwrap();
|
2021-10-27 13:37:13 +00:00
|
|
|
let into = match syn::parse2::<Type>(
|
2021-10-25 10:56:33 +00:00
|
|
|
proc_macro2::TokenStream::from_str(&derived.into.clone().unwrap()).unwrap(),
|
2021-10-27 13:37:13 +00:00
|
|
|
) {
|
|
|
|
Ok(e) => e,
|
|
|
|
_ => {
|
|
|
|
return Err(Error::new_spanned(
|
|
|
|
&name,
|
|
|
|
"derived into must be a valid type.",
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let derived = DerivedFieldMetadata {
|
|
|
|
ident: name,
|
|
|
|
into,
|
|
|
|
owned: derived.owned,
|
2021-10-28 10:22:39 +00:00
|
|
|
with: derived.with.clone(),
|
2021-10-27 13:37:13 +00:00
|
|
|
};
|
2021-10-25 10:56:33 +00:00
|
|
|
|
|
|
|
processed_fields.push(SimpleObjectFieldGenerator {
|
2021-10-26 10:57:39 +00:00
|
|
|
field,
|
2021-10-25 10:56:33 +00:00
|
|
|
derived: Some(derived),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for SimpleObjectFieldGenerator { field, derived } in &processed_fields {
|
2022-01-26 08:57:47 +00:00
|
|
|
if field.skip || field.skip_output {
|
2020-09-28 09:44:00 +00:00
|
|
|
continue;
|
2020-03-27 02:20:20 +00:00
|
|
|
}
|
2021-10-25 10:56:33 +00:00
|
|
|
|
|
|
|
let base_ident = match &field.ident {
|
2020-09-28 09:44:00 +00:00
|
|
|
Some(ident) => ident,
|
|
|
|
None => return Err(Error::new_spanned(&ident, "All fields must be named.").into()),
|
|
|
|
};
|
|
|
|
|
2021-10-25 10:56:33 +00:00
|
|
|
let ident = if let Some(derived) = derived {
|
|
|
|
&derived.ident
|
|
|
|
} else {
|
|
|
|
base_ident
|
|
|
|
};
|
|
|
|
|
2020-10-14 09:08:57 +00:00
|
|
|
let field_name = field.name.clone().unwrap_or_else(|| {
|
|
|
|
object_args
|
|
|
|
.rename_fields
|
|
|
|
.rename(ident.unraw().to_string(), RenameTarget::Field)
|
|
|
|
});
|
2020-09-28 09:44:00 +00:00
|
|
|
let field_desc = get_rustdoc(&field.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 external = field.external;
|
|
|
|
let requires = match &field.requires {
|
2020-10-16 10:37:59 +00:00
|
|
|
Some(requires) => quote! { ::std::option::Option::Some(#requires) },
|
|
|
|
None => quote! { ::std::option::Option::None },
|
2020-09-28 09:44:00 +00:00
|
|
|
};
|
|
|
|
let provides = match &field.provides {
|
2020-10-16 10:37:59 +00:00
|
|
|
Some(provides) => quote! { ::std::option::Option::Some(#provides) },
|
|
|
|
None => quote! { ::std::option::Option::None },
|
2020-09-28 09:44:00 +00:00
|
|
|
};
|
|
|
|
let vis = &field.vis;
|
2021-10-27 13:37:13 +00:00
|
|
|
|
2021-10-25 10:56:33 +00:00
|
|
|
let ty = if let Some(derived) = derived {
|
|
|
|
&derived.into
|
|
|
|
} else {
|
|
|
|
&field.ty
|
|
|
|
};
|
2020-03-27 02:20:20 +00:00
|
|
|
|
2021-10-27 13:37:13 +00:00
|
|
|
let owned = if let Some(derived) = derived {
|
|
|
|
derived.owned.unwrap_or(field.owned)
|
|
|
|
} else {
|
|
|
|
field.owned
|
|
|
|
};
|
|
|
|
|
2020-09-28 09:44:00 +00:00
|
|
|
let cache_control = {
|
|
|
|
let public = field.cache_control.is_public();
|
|
|
|
let max_age = field.cache_control.max_age;
|
|
|
|
quote! {
|
|
|
|
#crate_name::CacheControl {
|
|
|
|
public: #public,
|
|
|
|
max_age: #max_age,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-03-27 02:20:20 +00:00
|
|
|
|
2020-12-11 08:03:28 +00:00
|
|
|
let visible = visible_fn(&field.visible);
|
|
|
|
|
2022-01-18 02:33:07 +00:00
|
|
|
if !field.flatten {
|
|
|
|
schema_fields.push(quote! {
|
|
|
|
fields.insert(::std::borrow::ToOwned::to_owned(#field_name), #crate_name::registry::MetaField {
|
|
|
|
name: ::std::borrow::ToOwned::to_owned(#field_name),
|
|
|
|
description: #field_desc,
|
|
|
|
args: ::std::default::Default::default(),
|
|
|
|
ty: <#ty as #crate_name::OutputType>::create_type_info(registry),
|
|
|
|
deprecation: #field_deprecation,
|
|
|
|
cache_control: #cache_control,
|
|
|
|
external: #external,
|
|
|
|
provides: #provides,
|
|
|
|
requires: #requires,
|
|
|
|
visible: #visible,
|
|
|
|
compute_complexity: ::std::option::Option::None,
|
2022-02-28 07:26:33 +00:00
|
|
|
oneof: false,
|
2022-01-18 02:33:07 +00:00
|
|
|
});
|
2020-09-28 09:44:00 +00:00
|
|
|
});
|
2022-01-18 02:33:07 +00:00
|
|
|
} else {
|
|
|
|
schema_fields.push(quote! {
|
|
|
|
#crate_name::static_assertions::assert_impl_one!(#ty: #crate_name::ObjectType);
|
|
|
|
#ty::create_type_info(registry);
|
|
|
|
if let #crate_name::registry::MetaType::Object { fields: obj_fields, .. } =
|
|
|
|
registry.create_fake_output_type::<#ty>() {
|
|
|
|
fields.extend(obj_fields);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-03-27 02:20:20 +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))
|
|
|
|
};
|
2022-03-14 01:19:27 +00:00
|
|
|
let guard = match field.guard.as_ref().or(object_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,
|
|
|
|
};
|
2020-04-21 07:40:19 +00:00
|
|
|
|
2021-10-28 10:22:39 +00:00
|
|
|
let with_function = derived.as_ref().and_then(|x| x.with.as_ref());
|
|
|
|
|
2021-10-27 13:37:13 +00:00
|
|
|
let mut block = match !owned {
|
2021-10-25 10:56:33 +00:00
|
|
|
true => quote! {
|
|
|
|
&self.#base_ident
|
|
|
|
},
|
|
|
|
false => quote! {
|
|
|
|
::std::clone::Clone::clone(&self.#base_ident)
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-10-28 10:22:39 +00:00
|
|
|
block = match (derived, with_function) {
|
|
|
|
(Some(_), Some(with)) => quote! {
|
|
|
|
#with(#block)
|
2021-10-27 13:37:13 +00:00
|
|
|
},
|
2021-10-28 10:22:39 +00:00
|
|
|
(Some(_), None) => quote! {
|
|
|
|
::std::convert::Into::into(#block)
|
2021-10-27 13:37:13 +00:00
|
|
|
},
|
2021-10-28 10:22:39 +00:00
|
|
|
(_, _) => block,
|
2021-10-25 10:56:33 +00:00
|
|
|
};
|
|
|
|
|
2021-10-27 13:37:13 +00:00
|
|
|
let ty = match !owned {
|
2021-10-25 10:56:33 +00:00
|
|
|
true => quote! { &#ty },
|
|
|
|
false => quote! { #ty },
|
|
|
|
};
|
|
|
|
|
2022-01-18 02:33:07 +00:00
|
|
|
if !field.flatten {
|
|
|
|
getters.push(quote! {
|
2020-09-28 09:44:00 +00:00
|
|
|
#[inline]
|
|
|
|
#[allow(missing_docs)]
|
2021-10-25 10:56:33 +00:00
|
|
|
#vis async fn #ident(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::Result<#ty> {
|
|
|
|
::std::result::Result::Ok(#block)
|
2020-09-28 09:44:00 +00:00
|
|
|
}
|
2022-01-18 02:33:07 +00:00
|
|
|
});
|
2020-09-28 09:44:00 +00:00
|
|
|
|
2022-01-18 02:33:07 +00:00
|
|
|
resolvers.push(quote! {
|
|
|
|
if ctx.item.node.name.node == #field_name {
|
|
|
|
let f = async move {
|
|
|
|
#guard
|
|
|
|
self.#ident(ctx).await.map_err(|err| err.into_server_error(ctx.item.pos))
|
|
|
|
};
|
|
|
|
let obj = f.await.map_err(|err| ctx.set_error_path(err))?;
|
|
|
|
let ctx_obj = ctx.with_selection_set(&ctx.item.node.selection_set);
|
|
|
|
return #crate_name::OutputType::resolve(&obj, &ctx_obj, ctx.item).await.map(::std::option::Option::Some);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resolvers.push(quote! {
|
|
|
|
if let ::std::option::Option::Some(value) = #crate_name::ContainerType::resolve_field(&self.#ident, ctx).await? {
|
|
|
|
return ::std::result::Result::Ok(std::option::Option::Some(value));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-03-27 02:20:20 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 10:52:10 +00:00
|
|
|
if !object_args.fake && resolvers.is_empty() {
|
2020-10-20 03:49:31 +00:00
|
|
|
return Err(Error::new_spanned(
|
|
|
|
&ident,
|
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());
|
|
|
|
}
|
|
|
|
|
2020-03-27 02:20:20 +00:00
|
|
|
let cache_control = {
|
2020-09-28 09:44:00 +00:00
|
|
|
let public = object_args.cache_control.is_public();
|
2020-03-27 02:20:20 +00:00
|
|
|
let max_age = object_args.cache_control.max_age;
|
|
|
|
quote! {
|
|
|
|
#crate_name::CacheControl {
|
|
|
|
public: #public,
|
|
|
|
max_age: #max_age,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-11 08:03:28 +00:00
|
|
|
let visible = visible_fn(&object_args.visible);
|
|
|
|
|
2021-03-17 12:51:30 +00:00
|
|
|
let mut concat_complex_fields = quote!();
|
|
|
|
let mut complex_resolver = quote!();
|
|
|
|
|
|
|
|
if object_args.complex {
|
|
|
|
concat_complex_fields = quote! {
|
|
|
|
fields.extend(<Self as #crate_name::ComplexObject>::fields(registry));
|
|
|
|
};
|
|
|
|
complex_resolver = quote! {
|
|
|
|
if let Some(value) = <Self as #crate_name::ComplexObject>::resolve_field(self, ctx).await? {
|
|
|
|
return Ok(Some(value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-15 00:53:26 +00:00
|
|
|
let resolve_container = if object_args.serial {
|
|
|
|
quote! { #crate_name::resolver_utils::resolve_container_serial(ctx, self).await }
|
|
|
|
} else {
|
|
|
|
quote! { #crate_name::resolver_utils::resolve_container(ctx, self).await }
|
|
|
|
};
|
|
|
|
|
2021-01-14 05:16:30 +00:00
|
|
|
let expanded = if object_args.concretes.is_empty() {
|
|
|
|
quote! {
|
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
|
|
|
impl #impl_generics #ident #ty_generics #where_clause {
|
|
|
|
#(#getters)*
|
2020-03-27 02:20:20 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 05:16:30 +00:00
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
2021-11-09 10:52:10 +00:00
|
|
|
#[#crate_name::async_trait::async_trait]
|
|
|
|
|
|
|
|
impl #impl_generics #crate_name::resolver_utils::ContainerType for #ident #ty_generics #where_clause {
|
|
|
|
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::ServerResult<::std::option::Option<#crate_name::Value>> {
|
|
|
|
#(#resolvers)*
|
|
|
|
#complex_resolver
|
|
|
|
::std::result::Result::Ok(::std::option::Option::None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
|
|
|
impl #impl_generics #crate_name::OutputType for #ident #ty_generics #where_clause {
|
2021-01-14 05:16:30 +00:00
|
|
|
fn type_name() -> ::std::borrow::Cow<'static, ::std::primitive::str> {
|
|
|
|
::std::borrow::Cow::Borrowed(#gql_typename)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String {
|
2021-11-09 10:52:10 +00:00
|
|
|
registry.create_output_type::<Self, _>(|registry| #crate_name::registry::MetaType::Object {
|
2021-01-14 05:16:30 +00:00
|
|
|
name: ::std::borrow::ToOwned::to_owned(#gql_typename),
|
|
|
|
description: #desc,
|
|
|
|
fields: {
|
|
|
|
let mut fields = #crate_name::indexmap::IndexMap::new();
|
|
|
|
#(#schema_fields)*
|
2021-03-17 12:51:30 +00:00
|
|
|
#concat_complex_fields
|
2021-01-14 05:16:30 +00:00
|
|
|
fields
|
|
|
|
},
|
|
|
|
cache_control: #cache_control,
|
|
|
|
extends: #extends,
|
|
|
|
keys: ::std::option::Option::None,
|
|
|
|
visible: #visible,
|
2021-10-28 08:56:08 +00:00
|
|
|
is_subscription: false,
|
2021-11-08 01:33:20 +00:00
|
|
|
rust_typename: ::std::any::type_name::<Self>(),
|
2021-01-14 05:16:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-07 12:51:20 +00:00
|
|
|
async fn resolve(&self, ctx: &#crate_name::ContextSelectionSet<'_>, _field: &#crate_name::Positioned<#crate_name::parser::types::Field>) -> #crate_name::ServerResult<#crate_name::Value> {
|
2021-06-15 00:53:26 +00:00
|
|
|
#resolve_container
|
2021-01-14 05:16:30 +00:00
|
|
|
}
|
2020-03-27 02:20:20 +00:00
|
|
|
}
|
2021-01-14 05:16:30 +00:00
|
|
|
|
|
|
|
impl #impl_generics #crate_name::ObjectType for #ident #ty_generics #where_clause {}
|
2020-03-27 02:20:20 +00:00
|
|
|
}
|
2021-01-14 05:16:30 +00:00
|
|
|
} else {
|
|
|
|
let mut code = Vec::new();
|
|
|
|
|
2022-01-05 03:52:02 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
struct GetLifetimes<'a> {
|
|
|
|
lifetimes: Vec<&'a LifetimeDef>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Visit<'a> for GetLifetimes<'a> {
|
|
|
|
fn visit_lifetime_def(&mut self, i: &'a LifetimeDef) {
|
|
|
|
self.lifetimes.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut visitor = GetLifetimes::default();
|
|
|
|
visitor.visit_generics(&object_args.generics);
|
|
|
|
let lifetimes = visitor.lifetimes;
|
|
|
|
|
|
|
|
let def_lifetimes = if !lifetimes.is_empty() {
|
|
|
|
Some(quote!(<#(#lifetimes),*>))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let type_lifetimes = if !lifetimes.is_empty() {
|
|
|
|
Some(quote!(#(#lifetimes,)*))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2021-01-14 05:16:30 +00:00
|
|
|
code.push(quote! {
|
|
|
|
impl #impl_generics #ident #ty_generics #where_clause {
|
|
|
|
#(#getters)*
|
2020-03-27 02:20:20 +00:00
|
|
|
|
2021-07-04 06:05:08 +00:00
|
|
|
fn __internal_create_type_info(
|
|
|
|
registry: &mut #crate_name::registry::Registry,
|
|
|
|
name: &str,
|
|
|
|
complex_fields: #crate_name::indexmap::IndexMap<::std::string::String, #crate_name::registry::MetaField>,
|
|
|
|
) -> ::std::string::String where Self: #crate_name::OutputType {
|
2021-11-09 10:52:10 +00:00
|
|
|
registry.create_output_type::<Self, _>(|registry| #crate_name::registry::MetaType::Object {
|
2021-01-14 11:20:28 +00:00
|
|
|
name: ::std::borrow::ToOwned::to_owned(name),
|
2021-01-14 05:16:30 +00:00
|
|
|
description: #desc,
|
|
|
|
fields: {
|
|
|
|
let mut fields = #crate_name::indexmap::IndexMap::new();
|
|
|
|
#(#schema_fields)*
|
2021-07-04 06:05:08 +00:00
|
|
|
::std::iter::Extend::extend(&mut fields, complex_fields.clone());
|
2021-01-14 05:16:30 +00:00
|
|
|
fields
|
|
|
|
},
|
|
|
|
cache_control: #cache_control,
|
|
|
|
extends: #extends,
|
|
|
|
keys: ::std::option::Option::None,
|
|
|
|
visible: #visible,
|
2021-10-28 08:56:08 +00:00
|
|
|
is_subscription: false,
|
2021-11-08 01:33:20 +00:00
|
|
|
rust_typename: ::std::any::type_name::<Self>(),
|
2021-01-14 05:16:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn __internal_resolve_field(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::ServerResult<::std::option::Option<#crate_name::Value>> where Self: #crate_name::ContainerType {
|
|
|
|
#(#resolvers)*
|
|
|
|
::std::result::Result::Ok(::std::option::Option::None)
|
|
|
|
}
|
2020-03-27 02:20:20 +00:00
|
|
|
}
|
2021-01-14 05:16:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
for concrete in &object_args.concretes {
|
|
|
|
let gql_typename = &concrete.name;
|
|
|
|
let params = &concrete.params.0;
|
2022-01-05 03:52:02 +00:00
|
|
|
let concrete_type = quote! { #ident<#type_lifetimes #(#params),*> };
|
2021-01-14 05:16:30 +00:00
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
2022-01-05 03:52:02 +00:00
|
|
|
impl #def_lifetimes #crate_name::resolver_utils::ContainerType for #concrete_type {
|
2021-01-14 05:16:30 +00:00
|
|
|
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::ServerResult<::std::option::Option<#crate_name::Value>> {
|
2021-07-04 06:05:08 +00:00
|
|
|
#complex_resolver
|
2021-01-14 05:16:30 +00:00
|
|
|
self.__internal_resolve_field(ctx).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::all, clippy::pedantic)]
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
2022-01-05 03:52:02 +00:00
|
|
|
impl #def_lifetimes #crate_name::OutputType for #concrete_type {
|
2021-11-09 10:52:10 +00:00
|
|
|
fn type_name() -> ::std::borrow::Cow<'static, ::std::primitive::str> {
|
|
|
|
::std::borrow::Cow::Borrowed(#gql_typename)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String {
|
|
|
|
let mut fields = #crate_name::indexmap::IndexMap::new();
|
|
|
|
#concat_complex_fields
|
|
|
|
Self::__internal_create_type_info(registry, #gql_typename, fields)
|
|
|
|
}
|
|
|
|
|
2021-06-07 12:51:20 +00:00
|
|
|
async fn resolve(&self, ctx: &#crate_name::ContextSelectionSet<'_>, _field: &#crate_name::Positioned<#crate_name::parser::types::Field>) -> #crate_name::ServerResult<#crate_name::Value> {
|
2021-06-15 00:53:26 +00:00
|
|
|
#resolve_container
|
2021-01-14 05:16:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 03:52:02 +00:00
|
|
|
impl #def_lifetimes #crate_name::ObjectType for #concrete_type {}
|
2021-01-14 05:16:30 +00:00
|
|
|
};
|
|
|
|
code.push(expanded);
|
2020-03-27 02:20:20 +00:00
|
|
|
}
|
2020-09-27 07:32:55 +00:00
|
|
|
|
2021-01-14 05:16:30 +00:00
|
|
|
quote!(#(#code)*)
|
2020-03-27 02:20:20 +00:00
|
|
|
};
|
2021-01-14 05:16:30 +00:00
|
|
|
|
2020-03-27 02:20:20 +00:00
|
|
|
Ok(expanded.into())
|
|
|
|
}
|