Fix a bug that cause generic InputObject and SimpleObject is incorrect name. #387

This commit is contained in:
Sunli 2021-01-14 19:20:28 +08:00
parent 3b9199a931
commit 9c6fe67a23
3 changed files with 129 additions and 9 deletions

View File

@ -220,9 +220,9 @@ pub fn generate(object_args: &args::InputObject) -> GeneratorResult<TokenStream>
code.push(quote! { code.push(quote! {
#[allow(clippy::all, clippy::pedantic)] #[allow(clippy::all, clippy::pedantic)]
impl #impl_generics #ident #ty_generics #where_clause { impl #impl_generics #ident #ty_generics #where_clause {
fn __internal_create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String where Self: #crate_name::InputType { fn __internal_create_type_info(registry: &mut #crate_name::registry::Registry, name: &str) -> ::std::string::String where Self: #crate_name::InputType {
registry.create_type::<Self, _>(|registry| #crate_name::registry::MetaType::InputObject { registry.create_type::<Self, _>(|registry| #crate_name::registry::MetaType::InputObject {
name: ::std::borrow::ToOwned::to_owned(#gql_typename), name: ::std::borrow::ToOwned::to_owned(name),
description: #desc, description: #desc,
input_fields: { input_fields: {
let mut fields = #crate_name::indexmap::IndexMap::new(); let mut fields = #crate_name::indexmap::IndexMap::new();
@ -263,7 +263,7 @@ pub fn generate(object_args: &args::InputObject) -> GeneratorResult<TokenStream>
} }
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String { fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String {
Self::__internal_create_type_info(registry) Self::__internal_create_type_info(registry, #gql_typename)
} }
} }

View File

@ -210,9 +210,9 @@ pub fn generate(object_args: &args::SimpleObject) -> GeneratorResult<TokenStream
impl #impl_generics #ident #ty_generics #where_clause { impl #impl_generics #ident #ty_generics #where_clause {
#(#getters)* #(#getters)*
fn __internal_create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String where Self: #crate_name::OutputType { fn __internal_create_type_info(registry: &mut #crate_name::registry::Registry, name: &str) -> ::std::string::String where Self: #crate_name::OutputType {
registry.create_type::<Self, _>(|registry| #crate_name::registry::MetaType::Object { registry.create_type::<Self, _>(|registry| #crate_name::registry::MetaType::Object {
name: ::std::borrow::ToOwned::to_owned(#gql_typename), name: ::std::borrow::ToOwned::to_owned(name),
description: #desc, description: #desc,
fields: { fields: {
let mut fields = #crate_name::indexmap::IndexMap::new(); let mut fields = #crate_name::indexmap::IndexMap::new();
@ -246,7 +246,7 @@ pub fn generate(object_args: &args::SimpleObject) -> GeneratorResult<TokenStream
} }
fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String { fn create_type_info(registry: &mut #crate_name::registry::Registry) -> ::std::string::String {
Self::__internal_create_type_info(registry) Self::__internal_create_type_info(registry, #gql_typename)
} }
} }

View File

@ -70,19 +70,79 @@ pub async fn test_input_object_generic() {
#[Object] #[Object]
impl Query { impl Query {
async fn q(&self, input: EqualityFilter<i32>) -> i32 { async fn q1(&self, input: EqualityFilter<i32>) -> i32 {
input.equals.unwrap_or_default() + input.not_equals.unwrap_or_default() input.equals.unwrap_or_default() + input.not_equals.unwrap_or_default()
} }
async fn q2(&self, input: EqualityFilter<String>) -> String {
input.equals.unwrap_or_default() + &input.not_equals.unwrap_or_default()
}
} }
let schema = Schema::new(Query, EmptyMutation, EmptySubscription); let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
let query = r#"{ let query = r#"{
q(input: { equals: 7, notEquals: 8 } ) q1(input: { equals: 7, notEquals: 8 } )
q2(input: { equals: "ab", notEquals: "cd" } )
}"#; }"#;
assert_eq!( assert_eq!(
schema.execute(query).await.into_result().unwrap().data, schema.execute(query).await.into_result().unwrap().data,
value!({ value!({
"q": 15 "q1": 15,
"q2": "abcd",
})
);
assert_eq!(
schema
.execute(
r#"{ __type(name: "IntEqualityFilter") { inputFields { name type { name } } } }"#
)
.await
.into_result()
.unwrap()
.data,
value!({
"__type": {
"inputFields": [
{"name": "equals", "type": { "name": "Int" } },
{"name": "notEquals", "type": { "name": "Int" } },
]
}
})
);
assert_eq!(
schema
.execute(r#"{ __type(name: "Query") { fields { name args { name type { kind ofType { name } } } } } }"#)
.await
.into_result()
.unwrap()
.data,
value!({
"__type": {
"fields": [
{
"name": "q1",
"args": [{
"name": "input",
"type": {
"kind": "NON_NULL",
"ofType": { "name": "IntEqualityFilter" },
},
}]
},
{
"name": "q2",
"args": [{
"name": "input",
"type": {
"kind": "NON_NULL",
"ofType": { "name": "StringEqualityFilter" },
},
}],
}
]
}
}) })
); );
} }
@ -128,4 +188,64 @@ pub async fn test_generic_simple_object() {
} }
}) })
); );
assert_eq!(
schema
.execute(r#"{ __type(name: "MyObjIntString") { fields { name type { kind ofType { name } } } } }"#)
.await
.into_result()
.unwrap()
.data,
value!({
"__type": {
"fields": [
{
"name": "a",
"type": {
"kind": "NON_NULL",
"ofType": { "name": "Int" },
},
},
{
"name": "b",
"type": {
"kind": "NON_NULL",
"ofType": { "name": "String" },
},
},
]
}
})
);
assert_eq!(
schema
.execute(
r#"{ __type(name: "Query") { fields { name type { kind ofType { name } } } } }"#
)
.await
.into_result()
.unwrap()
.data,
value!({
"__type": {
"fields": [
{
"name": "q1",
"type": {
"kind": "NON_NULL",
"ofType": { "name": "MyObjIntString" },
},
},
{
"name": "q2",
"type": {
"kind": "NON_NULL",
"ofType": { "name": "MyObji64f32" },
},
},
]
}
})
);
} }