From 46764de1a68b1e157dd8efc5e211e6a2a87d8bc2 Mon Sep 17 00:00:00 2001 From: Magnus Hallin Date: Wed, 30 Nov 2016 21:49:50 +0100 Subject: [PATCH] Add support for return statements in field resolver functions --- src/executor_tests/executor.rs | 4 ++-- src/macros/field.rs | 4 ++-- src/macros/tests/field.rs | 9 +++++++++ src/schema/schema.rs | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/executor_tests/executor.rs b/src/executor_tests/executor.rs index 6d416579..f85995c4 100644 --- a/src/executor_tests/executor.rs +++ b/src/executor_tests/executor.rs @@ -26,9 +26,9 @@ mod field_execution { graphql_object!(DeepDataType: () |&self| { field a() -> &str { "Already Been Done" } field b() -> &str { "Boring" } - field c() -> &[Option<&str>] { &[Some("Contrived"), None, Some("Confusing")] } + field c() -> Vec> { vec![Some("Contrived"), None, Some("Confusing")] } - field deeper() -> &[Option] { &[Some(DataType), None, Some(DataType) ] } + field deeper() -> Vec> { vec![Some(DataType), None, Some(DataType) ] } }); #[test] diff --git a/src/macros/field.rs b/src/macros/field.rs index d983096d..a03d6a12 100644 --- a/src/macros/field.rs +++ b/src/macros/field.rs @@ -69,13 +69,13 @@ macro_rules! __graphql__build_field_matches { ) => { $( if $fieldvar == &$crate::to_snake_case(stringify!($name)) { - let result: $t = { + let result: $t = (|| { __graphql__args!( @assign_arg_vars, $argsvar, $executorvar, $($args)* ); $body - }; + })(); return ($crate::IntoFieldResult::into(result)).and_then(|r| $executorvar.resolve(&r)) } diff --git a/src/macros/tests/field.rs b/src/macros/tests/field.rs index 78ee2a6c..16d6e8cd 100644 --- a/src/macros/tests/field.rs +++ b/src/macros/tests/field.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use value::Value; use ast::InputValue; use schema::model::RootNode; +use executor::FieldResult; struct Interface; struct Root; @@ -14,6 +15,8 @@ Syntax to validate: * Object vs. interface * Description vs. no description * Deprecated vs. not deprecated +* FieldResult vs. object directly +* Return vs. implicit return */ @@ -28,6 +31,12 @@ graphql_object!(Root: () |&self| { field deprecated "Deprecation reason" deprecated_descr() -> i64 as "Field description" { 0 } + field with_field_result() -> FieldResult { Ok(0) } + + field with_return() -> i64 { return 0; } + + field with_return_field_result() -> FieldResult { return Ok(0); } + interfaces: [Interface] }); diff --git a/src/schema/schema.rs b/src/schema/schema.rs index 28588a59..736d7c7c 100644 --- a/src/schema/schema.rs +++ b/src/schema/schema.rs @@ -84,7 +84,7 @@ graphql_object!(<'a> TypeType<'a>: SchemaType as "__Type" |&self| { } } - field of_type() -> Option<&TypeType> { + field of_type() -> Option<&Box> { match *self { TypeType::Concrete(_) => None, TypeType::List(ref l) | TypeType::NonNull(ref l) => Some(l),