2020-03-01 10:54:34 +00:00
|
|
|
use crate::args;
|
2020-03-04 02:38:07 +00:00
|
|
|
use crate::utils::{build_value_repr, get_crate_name};
|
2020-03-02 11:25:21 +00:00
|
|
|
use inflector::Inflector;
|
2020-03-01 10:54:34 +00:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use proc_macro2::Span;
|
|
|
|
use quote::quote;
|
|
|
|
use syn::{Data, DeriveInput, Error, Ident, Result};
|
|
|
|
|
|
|
|
pub fn generate(object_args: &args::Object, input: &DeriveInput) -> Result<TokenStream> {
|
2020-03-02 00:24:49 +00:00
|
|
|
let crate_name = get_crate_name(object_args.internal);
|
2020-03-01 10:54:34 +00:00
|
|
|
let vis = &input.vis;
|
|
|
|
let ident = &input.ident;
|
2020-03-03 11:15:18 +00:00
|
|
|
let generics = &input.generics;
|
2020-03-02 11:25:21 +00:00
|
|
|
match &input.data {
|
|
|
|
Data::Struct(_) => {}
|
2020-03-01 10:54:34 +00:00
|
|
|
_ => return Err(Error::new_spanned(input, "It should be a struct.")),
|
|
|
|
};
|
|
|
|
|
|
|
|
let gql_typename = object_args
|
|
|
|
.name
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| ident.to_string());
|
2020-03-03 11:15:18 +00:00
|
|
|
let desc = object_args
|
|
|
|
.desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
2020-03-01 10:54:34 +00:00
|
|
|
let trait_ident = Ident::new(&format!("{}Fields", ident.to_string()), Span::call_site());
|
|
|
|
let mut trait_fns = Vec::new();
|
|
|
|
let mut resolvers = Vec::new();
|
2020-03-03 03:48:00 +00:00
|
|
|
let mut schema_fields = Vec::new();
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-02 11:25:21 +00:00
|
|
|
for field in &object_args.fields {
|
|
|
|
let ty = &field.ty;
|
|
|
|
let field_name = &field.name;
|
2020-03-03 03:48:00 +00:00
|
|
|
let desc = field
|
|
|
|
.desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! {Some(#s)})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
let deprecation = field
|
|
|
|
.deprecation
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! { Some(#s) })
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
|
|
|
|
2020-03-02 11:25:21 +00:00
|
|
|
let mut decl_params = Vec::new();
|
|
|
|
let mut get_params = Vec::new();
|
|
|
|
let mut use_params = Vec::new();
|
2020-03-03 03:48:00 +00:00
|
|
|
let mut schema_args = Vec::new();
|
2020-03-02 11:25:21 +00:00
|
|
|
|
|
|
|
for arg in &field.arguments {
|
|
|
|
let name = Ident::new(&arg.name, Span::call_site());
|
|
|
|
let ty = &arg.ty;
|
|
|
|
let name_str = name.to_string();
|
|
|
|
let snake_case_name = Ident::new(&name.to_string().to_snake_case(), ident.span());
|
2020-03-03 03:48:00 +00:00
|
|
|
let desc = arg
|
|
|
|
.desc
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| quote! { Some(#s) })
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
2020-03-04 02:38:07 +00:00
|
|
|
let schema_default = arg
|
2020-03-03 03:48:00 +00:00
|
|
|
.default
|
|
|
|
.as_ref()
|
|
|
|
.map(|v| {
|
|
|
|
let s = v.to_string();
|
|
|
|
quote! {Some(#s)}
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| quote! {None});
|
2020-03-02 11:25:21 +00:00
|
|
|
|
|
|
|
decl_params.push(quote! { #snake_case_name: #ty });
|
2020-03-04 02:38:07 +00:00
|
|
|
|
|
|
|
let default = match &arg.default {
|
|
|
|
Some(default) => {
|
|
|
|
let repr = build_value_repr(&crate_name, &default);
|
|
|
|
quote! {Some(|| #repr)}
|
|
|
|
}
|
|
|
|
None => quote! { None },
|
|
|
|
};
|
2020-03-02 11:25:21 +00:00
|
|
|
get_params.push(quote! {
|
2020-03-04 02:38:07 +00:00
|
|
|
let #snake_case_name: #ty = ctx_field.param_value(#name_str, #default)?;
|
2020-03-01 10:54:34 +00:00
|
|
|
});
|
2020-03-04 02:38:07 +00:00
|
|
|
|
2020-03-02 11:25:21 +00:00
|
|
|
use_params.push(quote! { #snake_case_name });
|
2020-03-04 02:38:07 +00:00
|
|
|
|
2020-03-03 03:48:00 +00:00
|
|
|
schema_args.push(quote! {
|
2020-03-03 11:15:18 +00:00
|
|
|
#crate_name::registry::InputValue {
|
2020-03-03 03:48:00 +00:00
|
|
|
name: #name_str,
|
|
|
|
description: #desc,
|
|
|
|
ty: <#ty as #crate_name::GQLType>::create_type_info(registry),
|
2020-03-04 02:38:07 +00:00
|
|
|
default_value: #schema_default,
|
2020-03-03 03:48:00 +00:00
|
|
|
}
|
|
|
|
});
|
2020-03-02 11:25:21 +00:00
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-02 11:25:21 +00:00
|
|
|
let resolver = Ident::new(
|
|
|
|
&field
|
|
|
|
.resolver
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(&field.name.to_snake_case()),
|
|
|
|
Span::call_site(),
|
|
|
|
);
|
|
|
|
if field.is_owned {
|
|
|
|
trait_fns.push(quote! {
|
2020-03-03 03:48:00 +00:00
|
|
|
async fn #resolver(&self, ctx: &#crate_name::Context<'_>, #(#decl_params),*) -> #crate_name::Result<#ty>;
|
2020-03-02 11:25:21 +00:00
|
|
|
});
|
2020-03-01 10:54:34 +00:00
|
|
|
} else {
|
2020-03-02 11:25:21 +00:00
|
|
|
trait_fns.push(quote! {
|
2020-03-03 03:48:00 +00:00
|
|
|
async fn #resolver<'a>(&'a self, ctx: &#crate_name::Context<'_>, #(#decl_params),*) -> #crate_name::Result<&'a #ty>;
|
2020-03-02 11:25:21 +00:00
|
|
|
});
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 11:25:21 +00:00
|
|
|
resolvers.push(quote! {
|
|
|
|
if field.name.as_str() == #field_name {
|
|
|
|
#(#get_params)*
|
|
|
|
let obj = #trait_ident::#resolver(self, &ctx_field, #(#use_params),*).await.
|
|
|
|
map_err(|err| err.with_position(field.position))?;
|
|
|
|
let ctx_obj = ctx_field.with_item(&field.selection_set);
|
|
|
|
let value = obj.resolve(&ctx_obj).await.
|
|
|
|
map_err(|err| err.with_position(field.position))?;
|
|
|
|
let name = field.alias.clone().unwrap_or_else(|| field.name.clone());
|
|
|
|
result.insert(name, value.into());
|
|
|
|
continue;
|
2020-03-02 00:24:49 +00:00
|
|
|
}
|
2020-03-02 11:25:21 +00:00
|
|
|
});
|
2020-03-03 03:48:00 +00:00
|
|
|
|
|
|
|
schema_fields.push(quote! {
|
2020-03-03 11:15:18 +00:00
|
|
|
#crate_name::registry::Field {
|
2020-03-03 03:48:00 +00:00
|
|
|
name: #field_name,
|
|
|
|
description: #desc,
|
|
|
|
args: vec![#(#schema_args),*],
|
|
|
|
ty: <#ty as #crate_name::GQLType>::create_type_info(registry),
|
|
|
|
deprecation: #deprecation,
|
|
|
|
}
|
|
|
|
});
|
2020-03-02 00:24:49 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 10:54:34 +00:00
|
|
|
let expanded = quote! {
|
2020-03-02 11:25:21 +00:00
|
|
|
#input
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-02 00:24:49 +00:00
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-03-01 10:54:34 +00:00
|
|
|
#vis trait #trait_ident {
|
|
|
|
#(#trait_fns)*
|
|
|
|
}
|
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
impl#generics #crate_name::GQLType for #ident#generics {
|
2020-03-02 00:24:49 +00:00
|
|
|
fn type_name() -> std::borrow::Cow<'static, str> {
|
|
|
|
std::borrow::Cow::Borrowed(#gql_typename)
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
2020-03-03 03:48:00 +00:00
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> String {
|
|
|
|
registry.create_type(&Self::type_name(), |registry| #crate_name::registry::Type::Object {
|
|
|
|
name: #gql_typename,
|
|
|
|
description: #desc,
|
2020-03-03 03:48:00 +00:00
|
|
|
fields: vec![#(#schema_fields),*]
|
|
|
|
})
|
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 00:24:49 +00:00
|
|
|
#[#crate_name::async_trait::async_trait]
|
2020-03-03 11:15:18 +00:00
|
|
|
impl#generics #crate_name::GQLOutputValue for #ident#generics {
|
2020-03-02 00:24:49 +00:00
|
|
|
async fn resolve(&self, ctx: &#crate_name::ContextSelectionSet<'_>) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
|
|
|
use #crate_name::ErrorWithPosition;
|
2020-03-01 10:54:34 +00:00
|
|
|
|
|
|
|
if ctx.items.is_empty() {
|
2020-03-02 00:24:49 +00:00
|
|
|
#crate_name::anyhow::bail!(#crate_name::QueryError::MustHaveSubFields {
|
2020-03-01 10:54:34 +00:00
|
|
|
object: #gql_typename,
|
|
|
|
}.with_position(ctx.span.0));
|
|
|
|
}
|
|
|
|
|
2020-03-02 00:24:49 +00:00
|
|
|
let mut result = #crate_name::serde_json::Map::<String, #crate_name::serde_json::Value>::new();
|
2020-03-01 10:54:34 +00:00
|
|
|
for selection in &ctx.items {
|
|
|
|
match selection {
|
2020-03-02 00:24:49 +00:00
|
|
|
#crate_name::graphql_parser::query::Selection::Field(field) => {
|
2020-03-01 10:54:34 +00:00
|
|
|
let ctx_field = ctx.with_item(field);
|
|
|
|
if field.name.as_str() == "__typename" {
|
2020-03-01 16:52:05 +00:00
|
|
|
let name = field.alias.clone().unwrap_or_else(|| field.name.clone());
|
|
|
|
result.insert(name, #gql_typename.into());
|
2020-03-01 10:54:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-03-03 11:15:18 +00:00
|
|
|
if field.name.as_str() == "__schema" {
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
#(#resolvers)*
|
2020-03-02 00:24:49 +00:00
|
|
|
#crate_name::anyhow::bail!(#crate_name::QueryError::FieldNotFound {
|
2020-03-01 10:54:34 +00:00
|
|
|
field_name: field.name.clone(),
|
|
|
|
object: #gql_typename,
|
|
|
|
}.with_position(field.position));
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 00:24:49 +00:00
|
|
|
Ok(#crate_name::serde_json::Value::Object(result))
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
impl#generics #crate_name::GQLObject for #ident#generics {}
|
2020-03-01 10:54:34 +00:00
|
|
|
};
|
|
|
|
Ok(expanded.into())
|
|
|
|
}
|