2020-03-06 15:58:43 +00:00
|
|
|
use crate::args;
|
|
|
|
use crate::args::{InterfaceField, InterfaceFieldArgument};
|
|
|
|
use crate::output_type::OutputType;
|
|
|
|
use crate::utils::{build_value_repr, 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 {
|
|
|
|
Fields::Unnamed(fields) => fields,
|
|
|
|
_ => return Err(Error::new_spanned(input, "All fields must be unnamed.")),
|
|
|
|
};
|
|
|
|
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());
|
|
|
|
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-07 02:39:55 +00:00
|
|
|
let mut inline_fragment_resolvers = Vec::new();
|
2020-03-06 15:58:43 +00:00
|
|
|
|
|
|
|
for field in &fields.unnamed {
|
|
|
|
if let Type::Path(p) = &field.ty {
|
|
|
|
let enum_name = &p.path.segments.last().unwrap().ident;
|
|
|
|
enum_names.push(enum_name);
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
registry_types.push(quote! {
|
2020-03-07 03:21:56 +00:00
|
|
|
<#p as #crate_name::GQLType>::create_type_info(registry);
|
|
|
|
registry.add_implements(&<#p as #crate_name::GQLType>::type_name(), #gql_typename);
|
2020-03-06 15:58:43 +00:00
|
|
|
});
|
|
|
|
possible_types.push(quote! {
|
2020-03-10 10:07:47 +00:00
|
|
|
possible_types.insert(<#p as #crate_name::GQLType>::type_name().to_string());
|
2020-03-06 15:58:43 +00:00
|
|
|
});
|
2020-03-07 02:39:55 +00:00
|
|
|
inline_fragment_resolvers.push(quote! {
|
2020-03-07 03:21:56 +00:00
|
|
|
if name == <#p as #crate_name::GQLType>::type_name() {
|
2020-03-07 02:39:55 +00:00
|
|
|
if let #ident::#enum_name(obj) = self {
|
|
|
|
#crate_name::do_resolve(ctx, obj, result).await?;
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
});
|
2020-03-06 15:58:43 +00:00
|
|
|
} else {
|
|
|
|
return Err(Error::new_spanned(field, "Invalid type"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut methods = Vec::new();
|
|
|
|
let mut schema_fields = Vec::new();
|
|
|
|
let mut resolvers = Vec::new();
|
|
|
|
|
|
|
|
for InterfaceField {
|
|
|
|
name,
|
|
|
|
desc,
|
|
|
|
ty,
|
|
|
|
args,
|
|
|
|
deprecation,
|
2020-03-07 03:21:56 +00:00
|
|
|
context,
|
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-03-07 03:21:56 +00:00
|
|
|
if *context {
|
2020-03-07 10:03:19 +00:00
|
|
|
decl_params.push(quote! { ctx: &'ctx #crate_name::Context<'ctx> });
|
2020-03-07 03:21:56 +00:00
|
|
|
use_params.push(quote! { ctx });
|
|
|
|
}
|
|
|
|
|
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-03-09 10:05:52 +00:00
|
|
|
let #ident: #ty = ctx.param_value(#name, #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,
|
|
|
|
ty: <#ty as #crate_name::GQLType>::create_type_info(registry),
|
|
|
|
default_value: #schema_default,
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-07 10:03:19 +00:00
|
|
|
let ctx_lifetime = if *context {
|
|
|
|
quote! { <'ctx> }
|
|
|
|
} else {
|
|
|
|
quote! {}
|
|
|
|
};
|
|
|
|
|
2020-03-06 15:58:43 +00:00
|
|
|
methods.push(quote! {
|
2020-03-07 10:03:19 +00:00
|
|
|
async fn #method_name #ctx_lifetime(&self, #(#decl_params),*) -> #ty {
|
2020-03-06 15:58:43 +00:00
|
|
|
match self {
|
|
|
|
#(#calls,)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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});
|
|
|
|
|
|
|
|
let ty = OutputType::parse(ty)?;
|
2020-03-07 10:03:19 +00:00
|
|
|
let schema_ty = ty.value_type();
|
2020-03-06 15:58:43 +00:00
|
|
|
|
|
|
|
schema_fields.push(quote! {
|
2020-03-08 12:35:36 +00:00
|
|
|
fields.insert(#name, #crate_name::registry::Field {
|
2020-03-06 15:58:43 +00:00
|
|
|
name: #name,
|
|
|
|
description: #desc,
|
2020-03-08 12:35:36 +00:00
|
|
|
args: {
|
|
|
|
let mut args = std::collections::HashMap::new();
|
|
|
|
#(#schema_args)*
|
|
|
|
args
|
|
|
|
},
|
2020-03-07 10:03:19 +00:00
|
|
|
ty: <#schema_ty as #crate_name::GQLType>::create_type_info(registry),
|
2020-03-06 15:58:43 +00:00
|
|
|
deprecation: #deprecation,
|
2020-03-08 12:35:36 +00:00
|
|
|
});
|
2020-03-06 15:58:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let resolve_obj = match &ty {
|
|
|
|
OutputType::Value(_) => quote! {
|
|
|
|
self.#method_name(#(#use_params),*).await
|
|
|
|
},
|
|
|
|
OutputType::Result(_, _) => {
|
|
|
|
quote! {
|
|
|
|
self.#method_name(#(#use_params),*).await.
|
|
|
|
map_err(|err| err.with_position(field.position))?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
resolvers.push(quote! {
|
|
|
|
if field.name.as_str() == #name {
|
|
|
|
#(#get_params)*
|
|
|
|
let ctx_obj = ctx.with_item(&field.selection_set);
|
|
|
|
return #crate_name::GQLOutputValue::resolve(&#resolve_obj, &ctx_obj).await.
|
|
|
|
map_err(|err| err.with_position(field.position).into());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let expanded = quote! {
|
|
|
|
#(#attrs)*
|
|
|
|
#vis enum #ident #generics { #(#enum_items),* }
|
|
|
|
|
|
|
|
#(#type_into_impls)*
|
|
|
|
|
|
|
|
impl #generics #ident #generics {
|
|
|
|
#(#methods)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl #generics #crate_name::GQLType 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
|
|
|
}
|
|
|
|
|
|
|
|
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-06 15:58:43 +00:00
|
|
|
name: #gql_typename,
|
|
|
|
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-03-06 15:58:43 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[#crate_name::async_trait::async_trait]
|
|
|
|
impl #generics #crate_name::GQLObject for #ident #generics {
|
|
|
|
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>, field: &#crate_name::graphql_parser::query::Field) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
|
|
|
use #crate_name::ErrorWithPosition;
|
|
|
|
|
|
|
|
#(#resolvers)*
|
|
|
|
|
2020-03-08 12:35:36 +00:00
|
|
|
#crate_name::anyhow::bail!(#crate_name::QueryError::FieldNotFound {
|
2020-03-06 15:58:43 +00:00
|
|
|
field_name: field.name.clone(),
|
|
|
|
object: #gql_typename.to_string(),
|
|
|
|
}
|
|
|
|
.with_position(field.position));
|
|
|
|
}
|
2020-03-07 02:39:55 +00:00
|
|
|
|
|
|
|
async fn resolve_inline_fragment(&self, name: &str, ctx: &#crate_name::ContextSelectionSet<'_>, result: &mut #crate_name::serde_json::Map<String, serde_json::Value>) -> #crate_name::Result<()> {
|
|
|
|
#(#inline_fragment_resolvers)*
|
2020-03-08 12:35:36 +00:00
|
|
|
#crate_name::anyhow::bail!(#crate_name::QueryError::UnrecognizedInlineFragment {
|
2020-03-07 02:39:55 +00:00
|
|
|
object: #gql_typename.to_string(),
|
|
|
|
name: name.to_string(),
|
|
|
|
});
|
|
|
|
}
|
2020-03-06 15:58:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(expanded.into())
|
|
|
|
}
|