From e2903cf0f7cbb19949646e35a88310301c04e8ec Mon Sep 17 00:00:00 2001 From: tyranron Date: Thu, 10 Oct 2019 14:50:10 +0200 Subject: [PATCH] Fix type inferring for trivial resolver code Additionally: - fix inconsistencies after merge with master --- juniper/src/executor/mod.rs | 117 +-------------------------- juniper_codegen/src/derive_object.rs | 1 + juniper_codegen/src/impl_object.rs | 1 + juniper_codegen/src/util.rs | 21 +++-- 4 files changed, 22 insertions(+), 118 deletions(-) diff --git a/juniper/src/executor/mod.rs b/juniper/src/executor/mod.rs index 27300882..2fdcd4fd 100644 --- a/juniper/src/executor/mod.rs +++ b/juniper/src/executor/mod.rs @@ -828,121 +828,10 @@ where None => return Err(GraphQLError::UnknownOperationName), }; - let default_variable_values = op.item.variable_definitions.map(|defs| { - defs.item - .items - .iter() - .filter_map(|&(ref name, ref def)| { - def.default_value - .as_ref() - .map(|i| (name.item.to_owned(), i.item.clone())) - }) - .collect::>>() - }); - - let errors = RwLock::new(Vec::new()); - let value; - - { - let mut all_vars; - let mut final_vars = variables; - - if let Some(defaults) = default_variable_values { - all_vars = variables.clone(); - - for (name, value) in defaults { - all_vars.entry(name).or_insert(value); - } - - final_vars = &all_vars; - } - - let root_type = match op.item.operation_type { - OperationType::Query => root_node.schema.query_type(), - OperationType::Mutation => root_node - .schema - .mutation_type() - .expect("No mutation type found"), - }; - - let executor = Executor { - fragments: &fragments - .iter() - .map(|f| (f.item.name.item, &f.item)) - .collect(), - variables: final_vars, - current_selection_set: Some(&op.item.selection_set[..]), - parent_selection_set: None, - current_type: root_type, - schema: &root_node.schema, - context, - errors: &errors, - field_path: FieldPath::Root(op.start), - }; - - value = match op.item.operation_type { - OperationType::Query => { - executor - .resolve_into_value_async(&root_node.query_info, &root_node) - .await - } - OperationType::Mutation => { - executor - .resolve_into_value_async(&root_node.mutation_info, &root_node.mutation_type) - .await - } - }; + if op.item.operation_type == OperationType::Subscription { + return Err(GraphQLError::IsSubscription); } - let mut errors = errors.into_inner().unwrap(); - errors.sort(); - - Ok((value, errors)) -} - -#[cfg(feature = "async")] -pub async fn execute_validated_query_async<'a, QueryT, MutationT, CtxT, S>( - document: Document<'a, S>, - operation_name: Option<&str>, - root_node: &RootNode<'a, QueryT, MutationT, S>, - variables: &Variables, - context: &CtxT, -) -> Result<(Value, Vec>), GraphQLError<'a>> -where - S: ScalarValue + Send + Sync, - QueryT: crate::GraphQLTypeAsync + Send + Sync, - QueryT::TypeInfo: Send + Sync, - MutationT: crate::GraphQLTypeAsync + Send + Sync, - MutationT::TypeInfo: Send + Sync, - CtxT: Send + Sync, - for<'b> &'b S: ScalarRefValue<'b>, -{ - let mut fragments = vec![]; - let mut operation = None; - - for def in document { - match def { - Definition::Operation(op) => { - if operation_name.is_none() && operation.is_some() { - return Err(GraphQLError::MultipleOperationsProvided); - } - - let move_op = operation_name.is_none() - || op.item.name.as_ref().map(|s| s.item) == operation_name; - - if move_op { - operation = Some(op); - } - } - Definition::Fragment(f) => fragments.push(f), - }; - } - - let op = match operation { - Some(op) => op, - None => return Err(GraphQLError::UnknownOperationName), - }; - let default_variable_values = op.item.variable_definitions.map(|defs| { defs.item .items @@ -978,6 +867,7 @@ where .schema .mutation_type() .expect("No mutation type found"), + OperationType::Subscription => unreachable!(), }; let executor = Executor { @@ -1006,6 +896,7 @@ where .resolve_into_value_async(&root_node.mutation_info, &root_node.mutation_type) .await } + OperationType::Subscription => unreachable!(), }; } diff --git a/juniper_codegen/src/derive_object.rs b/juniper_codegen/src/derive_object.rs index 911a21cb..f7a4110f 100644 --- a/juniper_codegen/src/derive_object.rs +++ b/juniper_codegen/src/derive_object.rs @@ -59,6 +59,7 @@ pub fn build_derive_object(ast: syn::DeriveInput, is_internal: bool) -> TokenStr description: field_attrs.description, deprecation: field_attrs.deprecation, resolver_code, + is_type_inferred: true, is_async: false, }) } diff --git a/juniper_codegen/src/impl_object.rs b/juniper_codegen/src/impl_object.rs index eb3f5c0f..88822cc7 100644 --- a/juniper_codegen/src/impl_object.rs +++ b/juniper_codegen/src/impl_object.rs @@ -214,6 +214,7 @@ pub fn build_object(args: TokenStream, body: TokenStream, is_internal: bool) -> description: attrs.description, deprecation: attrs.deprecation, resolver_code, + is_type_inferred: false, is_async, }); } diff --git a/juniper_codegen/src/util.rs b/juniper_codegen/src/util.rs index 875b0df6..a0dae69e 100644 --- a/juniper_codegen/src/util.rs +++ b/juniper_codegen/src/util.rs @@ -597,6 +597,7 @@ pub struct GraphQLTypeDefinitionField { pub deprecation: Option, pub args: Vec, pub resolver_code: proc_macro2::TokenStream, + pub is_type_inferred: bool, pub is_async: bool, } @@ -712,10 +713,15 @@ impl GraphQLTypeDefiniton { }, ) } else { - let _type = &field._type; + let _type = if field.is_type_inferred { + quote!() + } else { + let _type = &field._type; + quote!(: #_type) + }; quote!( #name => { - let res: #_type = { #code }; + let res #_type = { #code }; #juniper_crate_name::IntoResolvable::into( res, executor.context() @@ -805,14 +811,19 @@ impl GraphQLTypeDefiniton { let resolve_field_async = { let resolve_matches_async = self.fields.iter().map(|field| { let name = &field.name; - let _type = &field._type; let code = &field.resolver_code; + let _type = if field.is_type_inferred { + quote!() + } else { + let _type = &field._type; + quote!(: #_type) + }; if field.is_async { quote!( #name => { let f = async move { - let res: #_type = async move { #code }.await; + let res #_type = async move { #code }.await; let inner_res = #juniper_crate_name::IntoResolvable::into( res, @@ -860,7 +871,7 @@ impl GraphQLTypeDefiniton { quote!( #name => { - let res: #_type = { #code }; + let res #_type = { #code }; let res2 = #juniper_crate_name::IntoResolvable::into( res, executor.context()