2020-03-06 15:58:43 +00:00
|
|
|
use crate::args;
|
|
|
|
use crate::args::{InterfaceField, InterfaceFieldArgument};
|
|
|
|
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-09 12:00:57 +00:00
|
|
|
use inflector::Inflector;
|
2020-03-06 15:58:43 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use proc_macro2::{Ident, Span};
|
|
|
|
use quote::quote;
|
|
|
|
use syn::{Data, DeriveInput, Error, Fields, Result, Type};
|
|
|
|
|
|
|
|
pub fn generate(interface_args: &args::Interface, input: &DeriveInput) -> Result<TokenStream> {
|
|
|
|
let crate_name = get_crate_name(interface_args.internal);
|
|
|
|
let ident = &input.ident;
|
|
|
|
let generics = &input.generics;
|
|
|
|
let attrs = &input.attrs;
|
|
|
|
let vis = &input.vis;
|
|
|
|
let s = match &input.data {
|
|
|
|
Data::Struct(s) => s,
|
|
|
|
_ => return Err(Error::new_spanned(input, "It should be a struct.")),
|
|
|
|
};
|
|
|
|
let fields = match &s.fields {
|
2020-04-20 06:37:28 +00:00
|
|
|
Fields::Unnamed(fields) => Some(&fields.unnamed),
|
|
|
|
Fields::Unit => None,
|
2020-03-06 15:58:43 +00:00
|
|
|
_ => return Err(Error::new_spanned(input, "All fields must be unnamed.")),
|
|
|
|
};
|
2020-04-09 14:03:09 +00:00
|
|
|
let extends = interface_args.extends;
|
2020-03-06 15:58:43 +00:00
|
|
|
let mut enum_names = Vec::new();
|
|
|
|
let mut enum_items = Vec::new();
|
|
|
|
let mut type_into_impls = Vec::new();
|
|
|
|
let gql_typename = interface_args
|
|
|
|
.name
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| ident.to_string());
|
2020-03-19 09:20:12 +00:00
|
|
|
check_reserved_name(&gql_typename, interface_args.internal)?;
|
2020-03-06 15:58:43 +00:00
|
|
|
let desc = interface_args
|
|
|
|
.desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
let mut registry_types = Vec::new();
|
|
|
|
let mut possible_types = Vec::new();
|
2020-03-25 03:39:28 +00:00
|
|
|
let mut collect_inline_fields = Vec::new();
|
2020-04-02 12:37:01 +00:00
|
|
|
let mut get_introspection_typename = Vec::new();
|
2020-03-06 15:58:43 +00:00
|
|
|
|
2020-04-20 06:37:28 +00:00
|
|
|
if let Some(fields) = fields {
|
|
|
|
for field in fields {
|
|
|
|
if let Type::Path(p) = &field.ty {
|
|
|
|
let enum_name = &p.path.segments.last().unwrap().ident;
|
|
|
|
enum_items.push(quote! { #enum_name(#p) });
|
|
|
|
type_into_impls.push(quote! {
|
|
|
|
impl #generics From<#p> for #ident #generics {
|
|
|
|
fn from(obj: #p) -> Self {
|
|
|
|
#ident::#enum_name(obj)
|
|
|
|
}
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
2020-04-20 06:37:28 +00:00
|
|
|
});
|
|
|
|
enum_names.push(enum_name);
|
|
|
|
|
|
|
|
registry_types.push(quote! {
|
|
|
|
<#p as #crate_name::Type>::create_type_info(registry);
|
|
|
|
registry.add_implements(&<#p as #crate_name::Type>::type_name(), #gql_typename);
|
|
|
|
});
|
|
|
|
|
|
|
|
possible_types.push(quote! {
|
|
|
|
possible_types.insert(<#p as #crate_name::Type>::type_name().to_string());
|
|
|
|
});
|
|
|
|
|
|
|
|
collect_inline_fields.push(quote! {
|
|
|
|
if let #ident::#enum_name(obj) = self {
|
2020-04-22 06:59:14 +00:00
|
|
|
return obj.collect_inline_fields(name, pos, ctx, futures);
|
2020-04-20 06:37:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
get_introspection_typename.push(quote! {
|
|
|
|
#ident::#enum_name(obj) => <#p as #crate_name::Type>::type_name()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return Err(Error::new_spanned(field, "Invalid type"));
|
|
|
|
}
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut methods = Vec::new();
|
|
|
|
let mut schema_fields = Vec::new();
|
|
|
|
let mut resolvers = Vec::new();
|
|
|
|
|
|
|
|
for InterfaceField {
|
|
|
|
name,
|
|
|
|
desc,
|
|
|
|
ty,
|
|
|
|
args,
|
|
|
|
deprecation,
|
2020-04-09 14:03:09 +00:00
|
|
|
external,
|
|
|
|
provides,
|
|
|
|
requires,
|
2020-03-06 15:58:43 +00:00
|
|
|
} in &interface_args.fields
|
|
|
|
{
|
2020-03-09 12:00:57 +00:00
|
|
|
let method_name = Ident::new(name, Span::call_site());
|
|
|
|
let name = name.to_camel_case();
|
2020-03-06 15:58:43 +00:00
|
|
|
let mut calls = Vec::new();
|
|
|
|
let mut use_params = Vec::new();
|
|
|
|
let mut decl_params = Vec::new();
|
|
|
|
let mut get_params = Vec::new();
|
|
|
|
let mut schema_args = Vec::new();
|
2020-04-09 14:03:09 +00:00
|
|
|
let requires = match &requires {
|
|
|
|
Some(requires) => quote! { Some(#requires) },
|
|
|
|
None => quote! { None },
|
|
|
|
};
|
|
|
|
let provides = match &provides {
|
|
|
|
Some(provides) => quote! { Some(#provides) },
|
|
|
|
None => quote! { None },
|
|
|
|
};
|
2020-03-06 15:58:43 +00:00
|
|
|
|
2020-05-01 23:57:34 +00:00
|
|
|
decl_params.push(quote! { ctx: &'ctx #crate_name::Context<'ctx> });
|
|
|
|
use_params.push(quote! { ctx });
|
2020-03-07 03:21:56 +00:00
|
|
|
|
2020-03-06 15:58:43 +00:00
|
|
|
for InterfaceFieldArgument {
|
|
|
|
name,
|
|
|
|
desc,
|
|
|
|
ty,
|
|
|
|
default,
|
|
|
|
} in args
|
|
|
|
{
|
|
|
|
let ident = Ident::new(name, Span::call_site());
|
2020-03-09 12:00:57 +00:00
|
|
|
let name = name.to_camel_case();
|
2020-03-06 15:58:43 +00:00
|
|
|
decl_params.push(quote! { #ident: #ty });
|
2020-03-07 03:21:56 +00:00
|
|
|
use_params.push(quote! { #ident });
|
2020-03-06 15:58:43 +00:00
|
|
|
|
|
|
|
let param_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-26 07:12:18 +00:00
|
|
|
let #ident: #ty = ctx.param_value(#name, ctx.position, #param_default)?;
|
2020-03-06 15:58:43 +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-06 15:58:43 +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-06 15:58:43 +00:00
|
|
|
default_value: #schema_default,
|
2020-03-22 01:34:32 +00:00
|
|
|
validator: None,
|
2020-03-08 12:35:36 +00:00
|
|
|
});
|
2020-03-06 15:58:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for enum_name in &enum_names {
|
|
|
|
calls.push(quote! {
|
|
|
|
#ident::#enum_name(obj) => obj.#method_name(#(#use_params),*).await
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let desc = desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
let deprecation = deprecation
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
|
2020-05-01 23:57:34 +00:00
|
|
|
let oty = OutputType::parse(ty)?;
|
|
|
|
let ty = match oty {
|
|
|
|
OutputType::Value(ty) => ty,
|
|
|
|
OutputType::Result(_, ty) => ty,
|
|
|
|
};
|
|
|
|
let schema_ty = oty.value_type();
|
|
|
|
|
|
|
|
methods.push(quote! {
|
|
|
|
#[inline]
|
|
|
|
async fn #method_name <'ctx>(&self, #(#decl_params),*) -> #crate_name::FieldResult<#ty> {
|
|
|
|
match self {
|
|
|
|
#(#calls,)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-03-06 15:58:43 +00:00
|
|
|
|
|
|
|
schema_fields.push(quote! {
|
2020-03-19 09:20:12 +00:00
|
|
|
fields.insert(#name.to_string(), #crate_name::registry::Field {
|
|
|
|
name: #name.to_string(),
|
2020-03-06 15:58:43 +00:00
|
|
|
description: #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-06 15:58:43 +00:00
|
|
|
deprecation: #deprecation,
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: Default::default(),
|
2020-04-09 14:03:09 +00:00
|
|
|
external: #external,
|
|
|
|
provides: #provides,
|
|
|
|
requires: #requires,
|
2020-03-08 12:35:36 +00:00
|
|
|
});
|
2020-03-06 15:58:43 +00:00
|
|
|
});
|
|
|
|
|
2020-05-01 23:57:34 +00:00
|
|
|
let resolve_obj = quote! {
|
|
|
|
self.#method_name(#(#use_params),*).await.
|
|
|
|
map_err(|err| err.into_error_with_path(ctx.position, ctx.path_node.as_ref().unwrap().to_json()))?
|
2020-03-06 15:58:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
resolvers.push(quote! {
|
2020-04-26 07:12:18 +00:00
|
|
|
if ctx.name.as_str() == #name {
|
2020-03-06 15:58:43 +00:00
|
|
|
#(#get_params)*
|
2020-04-26 07:12:18 +00:00
|
|
|
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
|
|
|
return #crate_name::OutputValueType::resolve(&#resolve_obj, &ctx_obj, ctx.position).await;
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-20 06:37:28 +00:00
|
|
|
let introspection_type_name = if get_introspection_typename.is_empty() {
|
|
|
|
quote! { unreachable!() }
|
|
|
|
} else {
|
|
|
|
quote! {
|
|
|
|
match self {
|
|
|
|
#(#get_introspection_typename),*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-06 15:58:43 +00:00
|
|
|
let expanded = quote! {
|
|
|
|
#(#attrs)*
|
|
|
|
#vis enum #ident #generics { #(#enum_items),* }
|
|
|
|
|
|
|
|
#(#type_into_impls)*
|
|
|
|
|
|
|
|
impl #generics #ident #generics {
|
|
|
|
#(#methods)*
|
|
|
|
}
|
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
impl #generics #crate_name::Type for #ident #generics {
|
2020-03-07 03:21:56 +00:00
|
|
|
fn type_name() -> std::borrow::Cow<'static, str> {
|
|
|
|
std::borrow::Cow::Borrowed(#gql_typename)
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 12:37:01 +00:00
|
|
|
fn introspection_type_name(&self) -> std::borrow::Cow<'static, str> {
|
2020-04-20 06:37:28 +00:00
|
|
|
#introspection_type_name
|
2020-04-02 12:37:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:58:43 +00:00
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> String {
|
|
|
|
registry.create_type::<Self, _>(|registry| {
|
|
|
|
#(#registry_types)*
|
|
|
|
|
2020-03-07 03:21:56 +00:00
|
|
|
#crate_name::registry::Type::Interface {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: #gql_typename.to_string(),
|
2020-03-06 15:58:43 +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-10 10:07:47 +00:00
|
|
|
possible_types: {
|
|
|
|
let mut possible_types = std::collections::HashSet::new();
|
|
|
|
#(#possible_types)*
|
|
|
|
possible_types
|
|
|
|
},
|
2020-04-09 14:03:09 +00:00
|
|
|
extends: #extends,
|
|
|
|
keys: None,
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-03-19 09:20:12 +00:00
|
|
|
impl #generics #crate_name::ObjectType for #ident #generics {
|
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-04-26 07:12:18 +00:00
|
|
|
field_name: ctx.name.clone(),
|
2020-03-06 15:58:43 +00:00
|
|
|
object: #gql_typename.to_string(),
|
2020-04-26 07:12:18 +00:00
|
|
|
}.into_error(ctx.position))
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
2020-03-07 02:39:55 +00:00
|
|
|
|
2020-03-25 03:39:28 +00:00
|
|
|
fn collect_inline_fields<'a>(
|
|
|
|
&'a self,
|
|
|
|
name: &str,
|
2020-04-02 02:21:04 +00:00
|
|
|
pos: #crate_name::Pos,
|
2020-03-26 10:30:29 +00:00
|
|
|
ctx: &#crate_name::ContextSelectionSet<'a>,
|
2020-03-25 03:39:28 +00:00
|
|
|
futures: &mut Vec<#crate_name::BoxFieldFuture<'a>>,
|
|
|
|
) -> #crate_name::Result<()> {
|
|
|
|
#(#collect_inline_fields)*
|
2020-04-22 06:59:14 +00:00
|
|
|
Ok(())
|
2020-03-07 02:39:55 +00:00
|
|
|
}
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
2020-03-19 09:20:12 +00:00
|
|
|
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
|
|
|
impl #generics #crate_name::OutputValueType for #ident #generics {
|
2020-04-02 02:21:04 +00:00
|
|
|
async fn resolve(value: &Self, ctx: &#crate_name::ContextSelectionSet<'_>, pos: #crate_name::Pos) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
2020-03-25 03:39:28 +00:00
|
|
|
#crate_name::do_resolve(ctx, value).await
|
2020-03-19 09:20:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-06 15:58:43 +00:00
|
|
|
};
|
|
|
|
Ok(expanded.into())
|
|
|
|
}
|