fix some bugs

This commit is contained in:
sunli 2020-03-07 11:21:56 +08:00
parent 82f37095f2
commit 18e1014788
4 changed files with 20 additions and 16 deletions

View File

@ -555,6 +555,7 @@ pub struct InterfaceField {
pub ty: Type,
pub args: Vec<InterfaceFieldArgument>,
pub deprecation: Option<String>,
pub context: bool,
}
impl InterfaceField {
@ -565,9 +566,13 @@ impl InterfaceField {
let mut ty = None;
let mut args = Vec::new();
let mut deprecation = None;
let mut context = false;
for meta in &ls.nested {
match meta {
NestedMeta::Meta(Meta::Path(p)) if p.is_ident("context") => {
context = true;
}
NestedMeta::Meta(Meta::NameValue(nv)) => {
if nv.path.is_ident("name") {
if let syn::Lit::Str(lit) = &nv.lit {
@ -642,6 +647,7 @@ impl InterfaceField {
ty: ty.unwrap(),
args,
deprecation,
context,
})
}
}

View File

@ -7,8 +7,6 @@ use proc_macro2::{Ident, Span};
use quote::quote;
use syn::{Data, DeriveInput, Error, Fields, Result, Type};
// todo: Context params
pub fn generate(interface_args: &args::Interface, input: &DeriveInput) -> Result<TokenStream> {
let crate_name = get_crate_name(interface_args.internal);
let ident = &input.ident;
@ -52,14 +50,14 @@ pub fn generate(interface_args: &args::Interface, input: &DeriveInput) -> Result
}
});
registry_types.push(quote! {
<#p as async_graphql::GQLType>::create_type_info(registry);
registry.add_implements(&<#p as GQLType>::type_name(), #gql_typename);
<#p as #crate_name::GQLType>::create_type_info(registry);
registry.add_implements(&<#p as #crate_name::GQLType>::type_name(), #gql_typename);
});
possible_types.push(quote! {
<#p as async_graphql::GQLType>::type_name().to_string()
<#p as #crate_name::GQLType>::type_name().to_string()
});
inline_fragment_resolvers.push(quote! {
if name == <#p as async_graphql::GQLType>::type_name() {
if name == <#p as #crate_name::GQLType>::type_name() {
if let #ident::#enum_name(obj) = self {
#crate_name::do_resolve(ctx, obj, result).await?;
}
@ -82,6 +80,7 @@ pub fn generate(interface_args: &args::Interface, input: &DeriveInput) -> Result
ty,
args,
deprecation,
context,
} in &interface_args.fields
{
let method_name = Ident::new(
@ -94,6 +93,11 @@ pub fn generate(interface_args: &args::Interface, input: &DeriveInput) -> Result
let mut get_params = Vec::new();
let mut schema_args = Vec::new();
if *context {
decl_params.push(quote! { ctx: &#crate_name::Context<'_> });
use_params.push(quote! { ctx });
}
for InterfaceFieldArgument {
name,
desc,
@ -103,7 +107,7 @@ pub fn generate(interface_args: &args::Interface, input: &DeriveInput) -> Result
{
let ident = Ident::new(name, Span::call_site());
decl_params.push(quote! { #ident: #ty });
use_params.push(ident.clone());
use_params.push(quote! { #ident });
let param_default = match &default {
Some(default) => {
@ -206,15 +210,15 @@ pub fn generate(interface_args: &args::Interface, input: &DeriveInput) -> Result
}
impl #generics #crate_name::GQLType for #ident #generics {
fn type_name() -> Cow<'static, str> {
Cow::Borrowed(#gql_typename)
fn type_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed(#gql_typename)
}
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> String {
registry.create_type::<Self, _>(|registry| {
#(#registry_types)*
async_graphql::registry::Type::Interface {
#crate_name::registry::Type::Interface {
name: #gql_typename,
description: #desc,
fields: vec![#(#schema_fields),*],

View File

@ -1,6 +1,4 @@
use super::StarWars;
use async_graphql::GQLType;
use std::borrow::Cow;
#[async_graphql::Enum(desc = "One of the films in the Star Wars Trilogy")]
#[allow(non_camel_case_types)]
@ -148,8 +146,6 @@ impl QueryRoot {
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
#[async_graphql::Interface(
field(name = "id", type = "&str"),
field(name = "name", type = "&str"),

View File

@ -73,8 +73,6 @@ impl<'a, T: GQLObject + Send + Sync> Resolver<'a, T> {
self.result,
)
.await?;
} else {
todo!()
}
}
}