diff --git a/src/ast.rs b/src/ast.rs index fa563341..908e910b 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -13,19 +13,19 @@ use parser::Spanning; /// This enum carries no semantic information and might refer to types that do /// not exist. #[derive(Clone, Eq, PartialEq, Debug)] -pub enum Type { +pub enum Type<'a> { /// A nullable named type, e.g. `String` - Named(String), + Named(&'a str), /// A nullable list type, e.g. `[String]` /// /// The list itself is what's nullable, the containing type might be non-null. - List(Box), + List(Box>), /// A non-null named type, e.g. `String!` - NonNullNamed(String), + NonNullNamed(&'a str), /// A non-null list type, e.g. `[String]!`. /// /// The list itself is what's non-null, the containing type might be null. - NonNullList(Box), + NonNullList(Box>), } /// A JSON-like value that can be passed into the query execution, either @@ -49,8 +49,8 @@ pub enum InputValue { } #[derive(Clone, PartialEq, Debug)] -pub struct VariableDefinition { - pub var_type: Spanning, +pub struct VariableDefinition<'a> { + pub var_type: Spanning>, pub default_value: Option>, } @@ -60,8 +60,8 @@ pub struct Arguments { } #[derive(Clone, PartialEq, Debug)] -pub struct VariableDefinitions { - pub items: Vec<(Spanning, VariableDefinition)>, +pub struct VariableDefinitions<'a> { + pub items: Vec<(Spanning, VariableDefinition<'a>)>, } #[derive(Clone, PartialEq, Debug)] @@ -122,10 +122,10 @@ pub enum OperationType { } #[derive(Clone, PartialEq, Debug)] -pub struct Operation { +pub struct Operation<'a> { pub operation_type: OperationType, pub name: Option>, - pub variable_definitions: Option>, + pub variable_definitions: Option>>, pub directives: Option>>, pub selection_set: Vec, } @@ -139,12 +139,12 @@ pub struct Fragment { } #[derive(Clone, PartialEq, Debug)] -pub enum Definition { - Operation(Spanning), +pub enum Definition<'a> { + Operation(Spanning>), Fragment(Spanning), } -pub type Document = Vec; +pub type Document<'a> = Vec>; /// Parse an unstructured input value into a Rust data type. /// @@ -163,7 +163,7 @@ pub trait ToInputValue: Sized { fn to(&self) -> InputValue; } -impl Type { +impl<'a> Type<'a> { /// Get the name of a named type. /// /// Only applies to named types; lists will return `None`. @@ -193,7 +193,7 @@ impl Type { } } -impl fmt::Display for Type { +impl<'a> fmt::Display for Type<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Type::Named(ref n) => write!(f, "{}", n), @@ -447,7 +447,7 @@ impl Arguments { } } -impl VariableDefinitions { +impl<'a> VariableDefinitions<'a> { pub fn iter(&self) -> slice::Iter<(Spanning, VariableDefinition)> { self.items.iter() } diff --git a/src/executor.rs b/src/executor.rs index 5fd307eb..bb707378 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -19,9 +19,9 @@ use types::base::GraphQLType; /// The registry gathers metadata for all types in a schema. It provides /// convenience methods to convert types implementing the `GraphQLType` trait /// into `Type` instances and automatically registers them. -pub struct Registry { +pub struct Registry<'r> { /// Currently registered types - pub types: HashMap, + pub types: HashMap>, } #[derive(Clone)] @@ -38,7 +38,7 @@ pub struct Executor<'a, CtxT> where CtxT: 'a { fragments: &'a HashMap<&'a str, &'a Fragment>, variables: &'a HashMap, current_selection_set: Option<&'a [Selection]>, - schema: &'a SchemaType, + schema: &'a SchemaType<'a>, context: &'a CtxT, errors: &'a RwLock>, field_path: FieldPath<'a>, @@ -353,9 +353,9 @@ pub fn execute_validated_query<'a, QueryT, MutationT, CtxT>( Ok((value, errors)) } -impl Registry { +impl<'r> Registry<'r> { /// Construct a new registry - pub fn new(types: HashMap) -> Registry { + pub fn new(types: HashMap>) -> Registry<'r> { Registry { types: types, } @@ -365,10 +365,10 @@ impl Registry { /// /// If the registry hasn't seen a type with this name before, it will /// construct its metadata and store it. - pub fn get_type(&mut self) -> Type where T: GraphQLType { + pub fn get_type(&mut self) -> Type<'r> where T: GraphQLType { if let Some(name) = T::name() { if !self.types.contains_key(name) { - self.insert_placeholder(name, Type::NonNullNamed(name.to_owned())); + self.insert_placeholder(name, Type::NonNullNamed(name)); let meta = T::meta(self); self.types.insert(name.to_owned(), meta); } @@ -380,7 +380,7 @@ impl Registry { } /// Create a field with the provided name - pub fn field(&mut self, name: &str) -> Field where T: GraphQLType { + pub fn field(&mut self, name: &str) -> Field<'r> where T: GraphQLType { Field { name: name.to_owned(), description: None, @@ -391,7 +391,7 @@ impl Registry { } #[doc(hidden)] - pub fn field_convert<'a, T: IntoResolvable<'a, I, C>, I, C>(&mut self, name: &str) -> Field + pub fn field_convert<'a, T: IntoResolvable<'a, I, C>, I, C>(&mut self, name: &str) -> Field<'r> where I: GraphQLType { Field { @@ -404,7 +404,7 @@ impl Registry { } /// Create an argument with the provided name - pub fn arg(&mut self, name: &str) -> Argument where T: GraphQLType + FromInputValue { + pub fn arg(&mut self, name: &str) -> Argument<'r> where T: GraphQLType + FromInputValue { Argument::new(name, self.get_type::()) } @@ -417,14 +417,14 @@ impl Registry { name: &str, value: &T, ) - -> Argument + -> Argument<'r> where T: GraphQLType + ToInputValue + FromInputValue { Argument::new(name, self.get_type::>()) .default_value(value.to()) } - fn insert_placeholder(&mut self, name: &str, of_type: Type) { + fn insert_placeholder(&mut self, name: &str, of_type: Type<'r>) { if !self.types.contains_key(name) { self.types.insert( name.to_owned(), @@ -435,8 +435,7 @@ impl Registry { /// Create a scalar meta type /// /// This expects the type to implement `FromInputValue`. - pub fn build_scalar_type(&mut self) - -> ScalarMeta + pub fn build_scalar_type(&mut self) -> ScalarMeta<'r> where T: FromInputValue + GraphQLType { let name = T::name().expect("Scalar types must be named. Implement name()"); @@ -444,13 +443,13 @@ impl Registry { } /// Create a list meta type - pub fn build_list_type(&mut self) -> ListMeta { + pub fn build_list_type(&mut self) -> ListMeta<'r> { let of_type = self.get_type::(); ListMeta::new(of_type) } /// Create a nullable meta type - pub fn build_nullable_type(&mut self) -> NullableMeta { + pub fn build_nullable_type(&mut self) -> NullableMeta<'r> { let of_type = self.get_type::(); NullableMeta::new(of_type) } @@ -459,62 +458,51 @@ impl Registry { /// /// To prevent infinite recursion by enforcing ordering, this returns a /// function that needs to be called with the list of fields on the object. - pub fn build_object_type(&mut self) - -> Box ObjectMeta> + pub fn build_object_type(&mut self, fields: &[Field<'r>]) -> ObjectMeta<'r> where T: GraphQLType { let name = T::name().expect("Object types must be named. Implement name()"); - let typename_field = self.field::("__typename"); - Box::new(move |fs: &[Field]| { - let mut v = fs.to_vec(); - v.push(typename_field.clone()); - ObjectMeta::new(name, &v) - }) + let mut v = fields.to_vec(); + v.push(self.field::("__typename")); + ObjectMeta::new(name, &v) } /// Create an enum meta type - pub fn build_enum_type(&mut self) - -> Box EnumMeta> + pub fn build_enum_type(&mut self, values: &[EnumValue]) -> EnumMeta<'r> where T: FromInputValue + GraphQLType { let name = T::name().expect("Enum types must be named. Implement name()"); - Box::new(move |values: &[EnumValue]| EnumMeta::new::(name, values)) + EnumMeta::new::(name, values) } /// Create an interface meta type builder - pub fn build_interface_type(&mut self) - -> Box InterfaceMeta> + pub fn build_interface_type(&mut self, fields: &[Field<'r>]) -> InterfaceMeta<'r> where T: GraphQLType { let name = T::name().expect("Interface types must be named. Implement name()"); - let typename_field = self.field::("__typename"); - Box::new(move |fs: &[Field]| { - let mut v = fs.to_vec(); - v.push(typename_field.clone()); - InterfaceMeta::new(name, &v) - }) + let mut v = fields.to_vec(); + v.push(self.field::("__typename")); + InterfaceMeta::new(name, &v) } /// Create a union meta type builder - pub fn build_union_type(&mut self) - -> Box UnionMeta> + pub fn build_union_type(&mut self, types: &[Type<'r>]) -> UnionMeta<'r> where T: GraphQLType { let name = T::name().expect("Union types must be named. Implement name()"); - Box::new(move |ts: &[Type]| UnionMeta::new(name, ts)) + UnionMeta::new(name, types) } /// Create an input object meta type builder - pub fn build_input_object_type(&mut self) - -> Box InputObjectMeta> + pub fn build_input_object_type(&mut self, args: &[Argument<'r>]) -> InputObjectMeta<'r> where T: FromInputValue + GraphQLType { let name = T::name().expect("Input object types must be named. Implement name()"); - Box::new(move |args: &[Argument]| InputObjectMeta::new::(name, args)) + InputObjectMeta::new::(name, args) } } diff --git a/src/integrations/iron_handlers.rs b/src/integrations/iron_handlers.rs index 339c75f9..6ce2b662 100644 --- a/src/integrations/iron_handlers.rs +++ b/src/integrations/iron_handlers.rs @@ -22,14 +22,14 @@ use ::{InputValue, GraphQLType, RootNode, execute}; /// this endpoint containing the field `"query"` and optionally `"variables"`. /// The variables should be a JSON object containing the variable to value /// mapping. -pub struct GraphQLHandler +pub struct GraphQLHandler<'a, CtxFactory, Query, Mutation, CtxT> where CtxFactory: Fn(&mut Request) -> CtxT + Send + Sync + 'static, CtxT: 'static, Query: GraphQLType + Send + Sync + 'static, Mutation: GraphQLType + Send + Sync + 'static, { context_factory: CtxFactory, - root_node: RootNode, + root_node: RootNode<'a, Query, Mutation>, } /// Handler that renders GraphiQL - a graphical query editor interface @@ -37,8 +37,8 @@ pub struct GraphiQLHandler { graphql_url: String, } -impl - GraphQLHandler +impl<'a, CtxFactory, Query, Mutation, CtxT> + GraphQLHandler<'a, CtxFactory, Query, Mutation, CtxT> where CtxFactory: Fn(&mut Request) -> CtxT + Send + Sync + 'static, CtxT: 'static, Query: GraphQLType + Send + Sync + 'static, @@ -145,13 +145,13 @@ impl GraphiQLHandler { } } -impl +impl<'a, CtxFactory, Query, Mutation, CtxT> Handler - for GraphQLHandler + for GraphQLHandler<'a, CtxFactory, Query, Mutation, CtxT> where CtxFactory: Fn(&mut Request) -> CtxT + Send + Sync + 'static, CtxT: 'static, Query: GraphQLType + Send + Sync + 'static, - Mutation: GraphQLType + Send + Sync + 'static, + Mutation: GraphQLType + Send + Sync + 'static, 'a: 'static, { fn handle(&self, req: &mut Request) -> IronResult { match req.method { diff --git a/src/macros/enums.rs b/src/macros/enums.rs index 82d7863b..8b7ec829 100644 --- a/src/macros/enums.rs +++ b/src/macros/enums.rs @@ -66,10 +66,10 @@ macro_rules! graphql_enum { Some(graphql_enum!(@as_expr, $outname)) } - fn meta(registry: &mut $crate::Registry) -> $crate::meta::MetaType { + fn meta<'r>(registry: &mut $crate::Registry<'r>) -> $crate::meta::MetaType<'r> { graphql_enum!( @maybe_apply, $descr, description, - registry.build_enum_type::<$name>()(&[ + registry.build_enum_type::<$name>(&[ $( graphql_enum!( @maybe_apply, diff --git a/src/macros/input_object.rs b/src/macros/input_object.rs index 7c76f2b2..ee7a1a1b 100644 --- a/src/macros/input_object.rs +++ b/src/macros/input_object.rs @@ -155,12 +155,11 @@ macro_rules! graphql_input_object { Some($outname) } - fn meta(registry: &mut $crate::Registry) -> $crate::meta::MetaType { + fn meta<'r>(registry: &mut $crate::Registry<'r>) -> $crate::meta::MetaType<'r> { + let fields = graphql_input_object!(@generate_meta_fields, registry, $fields); graphql_input_object!( @maybe_apply, $descr, description, - registry.build_input_object_type::<$name>()( - graphql_input_object!(@generate_meta_fields, registry, $fields) - )).into_meta() + registry.build_input_object_type::<$name>(fields)).into_meta() } } }; diff --git a/src/macros/interface.rs b/src/macros/interface.rs index d31b79fb..5960f87c 100644 --- a/src/macros/interface.rs +++ b/src/macros/interface.rs @@ -236,11 +236,11 @@ macro_rules! graphql_interface { #[allow(unused_assignments)] #[allow(unused_mut)] - fn meta(registry: &mut $crate::Registry) -> $crate::meta::MetaType { + fn meta<'r>(registry: &mut $crate::Registry<'r>) -> $crate::meta::MetaType<'r> { let mut fields = Vec::new(); let mut description = None; graphql_interface!(@ gather_meta, (registry, fields, description), $($items)*); - let mut mt = registry.build_interface_type::<$name>()(&fields); + let mut mt = registry.build_interface_type::<$name>(&fields); if let Some(description) = description { mt = mt.description(description); diff --git a/src/macros/object.rs b/src/macros/object.rs index a6116c7d..6a7231f0 100644 --- a/src/macros/object.rs +++ b/src/macros/object.rs @@ -357,7 +357,7 @@ macro_rules! graphql_object { #[allow(unused_assignments)] #[allow(unused_mut)] - fn meta(registry: &mut $crate::Registry) -> $crate::meta::MetaType { + fn meta<'r>(registry: &mut $crate::Registry<'r>) -> $crate::meta::MetaType<'r> { let mut fields = Vec::new(); let mut description = None; let mut interfaces: Option> = None; @@ -365,7 +365,7 @@ macro_rules! graphql_object { @gather_object_meta, registry, fields, description, interfaces, $($items)* ); - let mut mt = registry.build_object_type::<$name>()(&fields); + let mut mt = registry.build_object_type::<$name>(&fields); if let Some(description) = description { mt = mt.description(description); diff --git a/src/macros/scalar.rs b/src/macros/scalar.rs index 62bc4a6d..12283860 100644 --- a/src/macros/scalar.rs +++ b/src/macros/scalar.rs @@ -68,7 +68,7 @@ macro_rules! graphql_scalar { Some($outname) } - fn meta(registry: &mut $crate::Registry) -> $crate::meta::MetaType { + fn meta<'r>(registry: &mut $crate::Registry<'r>) -> $crate::meta::MetaType<'r> { graphql_scalar!( @maybe_apply, $descr, description, registry.build_scalar_type::()) diff --git a/src/macros/union.rs b/src/macros/union.rs index 182a464a..d78467fa 100644 --- a/src/macros/union.rs +++ b/src/macros/union.rs @@ -114,11 +114,11 @@ macro_rules! graphql_union { #[allow(unused_assignments)] #[allow(unused_mut)] - fn meta(registry: &mut $crate::Registry) -> $crate::meta::MetaType { + fn meta<'r>(registry: &mut $crate::Registry<'r>) -> $crate::meta::MetaType<'r> { let mut types; let mut description = None; graphql_union!(@ gather_meta, (registry, types, description), $($items)*); - let mut mt = registry.build_union_type::<$name>()(&types); + let mut mt = registry.build_union_type::<$name>(&types); if let Some(description) = description { mt = mt.description(description); diff --git a/src/parser/document.rs b/src/parser/document.rs index 5473e830..8d47c63d 100644 --- a/src/parser/document.rs +++ b/src/parser/document.rs @@ -13,7 +13,7 @@ pub fn parse_document_source(s: &str) -> UnlocatedParseResult { parse_document(&mut parser) } -fn parse_document<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Document> { +fn parse_document<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Document<'a>> { let mut defs = Vec::new(); loop { @@ -25,7 +25,7 @@ fn parse_document<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Docum } } -fn parse_definition<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Definition> { +fn parse_definition<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Definition<'a>> { match parser.peek().item { Token::CurlyOpen | Token::Name("query") | Token::Name("mutation") => Ok(Definition::Operation(try!(parse_operation_definition(parser)))), @@ -35,7 +35,7 @@ fn parse_definition<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Def } } -fn parse_operation_definition<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Operation> { +fn parse_operation_definition<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Operation<'a>> { if parser.peek().item == Token::CurlyOpen { let selection_set = try!(parse_selection_set(parser)); @@ -54,7 +54,7 @@ fn parse_operation_definition<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Op let start_pos = parser.peek().start.clone(); let operation_type = try!(parse_operation_type(parser)); let name = match parser.peek().item { - Token::Name(_) => Some(try!(parser.expect_name())), + Token::Name(_) => Some(try!(parser.expect_name()).map(|s| s.to_owned())), _ => None }; let variable_definitions = try!(parse_variable_definitions(parser)); @@ -77,7 +77,7 @@ fn parse_operation_definition<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Op fn parse_fragment_definition<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Fragment> { let Spanning { start: start_pos, .. } = try!(parser.expect(&Token::Name("fragment"))); let name = match parser.expect_name() { - Ok(n) => if &n.item == "on" { + Ok(n) => if n.item == "on" { return Err(n.map(|_| ParseError::UnexpectedToken(Token::Name("on")))); } else { @@ -95,8 +95,8 @@ fn parse_fragment_definition<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Fra &start_pos, &selection_set.end, Fragment { - name: name, - type_condition: type_cond, + name: name.map(|s| s.to_owned()), + type_condition: type_cond.map(|s| s.to_owned()), directives: directives.map(|s| s.item), selection_set: selection_set.item, })) @@ -140,7 +140,7 @@ fn parse_fragment<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Selec &start_pos.clone(), &selection_set.end, InlineFragment { - type_condition: Some(name), + type_condition: Some(name.map(|s| s.to_owned())), directives: directives.map(|s| s.item), selection_set: selection_set.item, }))) @@ -167,7 +167,7 @@ fn parse_fragment<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Selec &start_pos.clone(), &directives.as_ref().map_or(&frag_name.end, |s| &s.end).clone(), FragmentSpread { - name: frag_name, + name: frag_name.map(|s| s.to_owned()), directives: directives.map(|s| s.item), }))) }, @@ -190,10 +190,10 @@ fn parse_fragment<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Selec } fn parse_field<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Field> { - let mut alias = Some(try!(parser.expect_name())); + let mut alias = Some(try!(parser.expect_name()).map(|s| s.to_owned())); let name = if try!(parser.skip(&Token::Colon)).is_some() { - try!(parser.expect_name()) + try!(parser.expect_name()).map(|s| s.to_owned()) } else { alias.take().unwrap() @@ -212,7 +212,7 @@ fn parse_field<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Field> { .clone(), Field { alias: alias, - name: name, + name: name.map(|s| s.to_owned()), arguments: arguments, directives: directives.map(|s| s.item), selection_set: selection_set.map(|s| s.item), @@ -239,7 +239,7 @@ fn parse_argument<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, (Spanning(parser: &mut Parser<'a>) -> ParseResult<'a, OperationType> { @@ -250,7 +250,7 @@ fn parse_operation_type<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Operatio } } -fn parse_variable_definitions<'a>(parser: &mut Parser<'a>) -> OptionParseResult<'a, VariableDefinitions> { +fn parse_variable_definitions<'a>(parser: &mut Parser<'a>) -> OptionParseResult<'a, VariableDefinitions<'a>> { if parser.peek().item != Token::ParenOpen { Ok(None) } @@ -263,7 +263,7 @@ fn parse_variable_definitions<'a>(parser: &mut Parser<'a>) -> OptionParseResult< } } -fn parse_variable_definition<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, (Spanning, VariableDefinition)> { +fn parse_variable_definition<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, (Spanning, VariableDefinition<'a>)> { let Spanning { start: start_pos, .. } = try!(parser.expect(&Token::Dollar)); let var_name = try!(parser.expect_name()); try!(parser.expect(&Token::Colon)); @@ -283,7 +283,7 @@ fn parse_variable_definition<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, (Sp Spanning::start_end( &start_pos, &var_name.end, - var_name.item + var_name.item.to_owned(), ), VariableDefinition { var_type: var_type, @@ -315,23 +315,23 @@ fn parse_directive<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Directive> { &start_pos, &arguments.as_ref().map_or(&name.end, |s| &s.end).clone(), Directive { - name: name, + name: name.map(|s| s.to_owned()), arguments: arguments, })) } -pub fn parse_type<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Type> { +pub fn parse_type<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Type<'a>> { let parsed_type = if let Some(Spanning { start: start_pos, ..}) = try!(parser.skip(&Token::BracketOpen)) { - let inner_type = try!(parse_type(parser)); - let Spanning { end: end_pos, .. } = try!(parser.expect(&Token::BracketClose)); - Spanning::start_end( - &start_pos, - &end_pos, - Type::List(Box::new(inner_type.item))) - } - else { - try!(parser.expect_name()).map(Type::Named) - }; + let inner_type = try!(parse_type(parser)); + let Spanning { end: end_pos, .. } = try!(parser.expect(&Token::BracketClose)); + Spanning::start_end( + &start_pos, + &end_pos, + Type::List(Box::new(inner_type.item))) + } + else { + try!(parser.expect_name()).map(Type::Named) + }; Ok(match *parser.peek() { Spanning { item: Token::ExclamationMark, .. } => @@ -340,7 +340,7 @@ pub fn parse_type<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Type> { }) } -fn wrap_non_null<'a>(parser: &mut Parser<'a>, inner: Spanning) -> ParseResult<'a, Type> { +fn wrap_non_null<'a>(parser: &mut Parser<'a>, inner: Spanning>) -> ParseResult<'a, Type<'a>> { let Spanning { end: end_pos, .. } = try!(parser.expect(&Token::ExclamationMark)); let wrapped = match inner.item { diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 9e05cd21..21d9b715 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -148,12 +148,12 @@ impl<'a> Parser<'a> { } #[doc(hidden)] - pub fn expect_name(&mut self) -> ParseResult<'a, String> { + pub fn expect_name(&mut self) -> ParseResult<'a, &'a str> { match *self.peek() { Spanning { item: Token::Name(_), .. } => Ok(self.next().map(|token| if let Token::Name(name) = token { - name.to_owned() + name } else { panic!("Internal parse error in `expect_name`"); diff --git a/src/parser/value.rs b/src/parser/value.rs index 0c1cd086..e7aa912a 100644 --- a/src/parser/value.rs +++ b/src/parser/value.rs @@ -57,7 +57,7 @@ fn parse_object_field<'a>(parser: &mut Parser<'a>, is_const: bool) -> ParseResul Ok(Spanning::start_end( &key.start.clone(), &value.end.clone(), - (key, value))) + (key.map(|s| s.to_owned()), value))) } fn parse_variable_literal<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, InputValue> { diff --git a/src/schema/meta.rs b/src/schema/meta.rs index 111df60c..067d3b23 100644 --- a/src/schema/meta.rs +++ b/src/schema/meta.rs @@ -7,9 +7,9 @@ use ast::{InputValue, FromInputValue, Type}; use types::base::TypeKind; /// Scalar type metadata -pub struct ScalarMeta { +pub struct ScalarMeta<'a> { #[doc(hidden)] - pub name: String, + pub name: &'a str, #[doc(hidden)] pub description: Option, #[doc(hidden)] @@ -18,35 +18,35 @@ pub struct ScalarMeta { /// List type metadata #[derive(Debug)] -pub struct ListMeta { +pub struct ListMeta<'a> { #[doc(hidden)] - pub of_type: Type, + pub of_type: Type<'a>, } /// Nullable type metadata #[derive(Debug)] -pub struct NullableMeta { +pub struct NullableMeta<'a> { #[doc(hidden)] - pub of_type: Type, + pub of_type: Type<'a>, } /// Object type metadata #[derive(Debug)] -pub struct ObjectMeta { +pub struct ObjectMeta<'a> { #[doc(hidden)] - pub name: String, + pub name: &'a str, #[doc(hidden)] pub description: Option, #[doc(hidden)] - pub fields: Vec, + pub fields: Vec>, #[doc(hidden)] pub interface_names: Vec, } /// Enum type metadata -pub struct EnumMeta { +pub struct EnumMeta<'a> { #[doc(hidden)] - pub name: String, + pub name: &'a str, #[doc(hidden)] pub description: Option, #[doc(hidden)] @@ -57,20 +57,20 @@ pub struct EnumMeta { /// Interface type metadata #[derive(Debug)] -pub struct InterfaceMeta { +pub struct InterfaceMeta<'a> { #[doc(hidden)] - pub name: String, + pub name: &'a str, #[doc(hidden)] pub description: Option, #[doc(hidden)] - pub fields: Vec, + pub fields: Vec>, } /// Union type metadata #[derive(Debug)] -pub struct UnionMeta { +pub struct UnionMeta<'a> { #[doc(hidden)] - pub name: String, + pub name: &'a str, #[doc(hidden)] pub description: Option, #[doc(hidden)] @@ -78,13 +78,13 @@ pub struct UnionMeta { } /// Input object metadata -pub struct InputObjectMeta { +pub struct InputObjectMeta<'a> { #[doc(hidden)] - pub name: String, + pub name: &'a str, #[doc(hidden)] pub description: Option, #[doc(hidden)] - pub input_fields: Vec, + pub input_fields: Vec>, #[doc(hidden)] pub try_parse_fn: Box bool + Send + Sync>, } @@ -94,58 +94,58 @@ pub struct InputObjectMeta { /// After a type's `meta` method has been called but before it has returned, a placeholder type /// is inserted into a registry to indicate existence. #[derive(Debug)] -pub struct PlaceholderMeta { +pub struct PlaceholderMeta<'a> { #[doc(hidden)] - pub of_type: Type, + pub of_type: Type<'a>, } /// Generic type metadata #[derive(Debug)] -pub enum MetaType { +pub enum MetaType<'a> { #[doc(hidden)] - Scalar(ScalarMeta), + Scalar(ScalarMeta<'a>), #[doc(hidden)] - List(ListMeta), + List(ListMeta<'a>), #[doc(hidden)] - Nullable(NullableMeta), + Nullable(NullableMeta<'a>), #[doc(hidden)] - Object(ObjectMeta), + Object(ObjectMeta<'a>), #[doc(hidden)] - Enum(EnumMeta), + Enum(EnumMeta<'a>), #[doc(hidden)] - Interface(InterfaceMeta), + Interface(InterfaceMeta<'a>), #[doc(hidden)] - Union(UnionMeta), + Union(UnionMeta<'a>), #[doc(hidden)] - InputObject(InputObjectMeta), + InputObject(InputObjectMeta<'a>), #[doc(hidden)] - Placeholder(PlaceholderMeta), + Placeholder(PlaceholderMeta<'a>), } /// Metadata for a field #[derive(Debug, Clone)] -pub struct Field { +pub struct Field<'a> { #[doc(hidden)] pub name: String, #[doc(hidden)] pub description: Option, #[doc(hidden)] - pub arguments: Option>, + pub arguments: Option>>, #[doc(hidden)] - pub field_type: Type, + pub field_type: Type<'a>, #[doc(hidden)] pub deprecation_reason: Option, } /// Metadata for an argument to a field #[derive(Debug, Clone)] -pub struct Argument { +pub struct Argument<'a> { #[doc(hidden)] pub name: String, #[doc(hidden)] pub description: Option, #[doc(hidden)] - pub arg_type: Type, + pub arg_type: Type<'a>, #[doc(hidden)] pub default_value: Option, } @@ -168,7 +168,7 @@ pub struct EnumValue { pub deprecation_reason: Option, } -impl MetaType { +impl<'a> MetaType<'a> { /// Access the name of the type, if applicable /// /// Lists, non-null wrappers, and placeholders don't have names. @@ -243,7 +243,7 @@ impl MetaType { } /// Construct a `Type` literal instance based on the metadata - pub fn as_type(&self) -> Type { + pub fn as_type(&self) -> Type<'a> { match *self { MetaType::Scalar(ScalarMeta { ref name, .. }) | MetaType::Object(ObjectMeta { ref name, .. }) | @@ -251,7 +251,7 @@ impl MetaType { MetaType::Interface(InterfaceMeta { ref name, .. }) | MetaType::Union(UnionMeta { ref name, .. }) | MetaType::InputObject(InputObjectMeta { ref name, .. }) => - Type::NonNullNamed(name.to_owned()), + Type::NonNullNamed(name), MetaType::List(ListMeta { ref of_type }) => Type::NonNullList(Box::new(of_type.clone())), MetaType::Nullable(NullableMeta { ref of_type }) => @@ -327,11 +327,11 @@ impl MetaType { } } -impl ScalarMeta { +impl<'a> ScalarMeta<'a> { /// Build a new scalar type metadata with the specified name - pub fn new(name: &str) -> ScalarMeta { + pub fn new(name: &'a str) -> ScalarMeta<'a> { ScalarMeta { - name: name.to_owned(), + name: name, description: None, try_parse_fn: Box::new( |v: &InputValue| ::from(v).is_some()), @@ -341,50 +341,50 @@ impl ScalarMeta { /// Set the description for the given scalar type /// /// If a description already was set prior to calling this method, it will be overwritten. - pub fn description(mut self, description: &str) -> ScalarMeta { + pub fn description(mut self, description: &str) -> ScalarMeta<'a> { self.description = Some(description.to_owned()); self } /// Wrap the scalar in a generic meta type - pub fn into_meta(self) -> MetaType { + pub fn into_meta(self) -> MetaType<'a> { MetaType::Scalar(self) } } -impl ListMeta { +impl<'a> ListMeta<'a> { /// Build a new list type by wrapping the specified type - pub fn new(of_type: Type) -> ListMeta { + pub fn new(of_type: Type<'a>) -> ListMeta<'a> { ListMeta { of_type: of_type, } } /// Wrap the list in a generic meta type - pub fn into_meta(self) -> MetaType { + pub fn into_meta(self) -> MetaType<'a> { MetaType::List(self) } } -impl NullableMeta { +impl<'a> NullableMeta<'a> { /// Build a new nullable type by wrapping the specified type - pub fn new(of_type: Type) -> NullableMeta { + pub fn new(of_type: Type<'a>) -> NullableMeta<'a> { NullableMeta { of_type: of_type, } } /// Wrap the nullable type in a generic meta type - pub fn into_meta(self) -> MetaType { + pub fn into_meta(self) -> MetaType<'a> { MetaType::Nullable(self) } } -impl ObjectMeta { +impl<'a> ObjectMeta<'a> { /// Build a new object type with the specified name and fields - pub fn new(name: &str, fields: &[Field]) -> ObjectMeta { + pub fn new(name: &'a str, fields: &[Field<'a>]) -> ObjectMeta<'a> { ObjectMeta { - name: name.to_owned(), + name: name, description: None, fields: fields.to_vec(), interface_names: vec![], @@ -394,32 +394,32 @@ impl ObjectMeta { /// Set the description for the object /// /// If a description was provided prior to calling this method, it will be overwritten. - pub fn description(mut self, description: &str) -> ObjectMeta { + pub fn description(mut self, description: &str) -> ObjectMeta<'a> { self.description = Some(description.to_owned()); self } /// Set the interfaces this type implements /// - /// If a list of interfaces already was provided prior to calling this method, they will be + /// If a list of interfaces already was provided prior to calling this method, they will be /// overwritten. - pub fn interfaces(mut self, interfaces: &[Type]) -> ObjectMeta { + pub fn interfaces(mut self, interfaces: &[Type<'a>]) -> ObjectMeta<'a> { self.interface_names = interfaces.iter() .map(|t| t.innermost_name().to_owned()).collect(); self } /// Wrap this object type in a generic meta type - pub fn into_meta(self) -> MetaType { + pub fn into_meta(self) -> MetaType<'a> { MetaType::Object(self) } } -impl EnumMeta { +impl<'a> EnumMeta<'a> { /// Build a new enum type with the specified name and possible values - pub fn new(name: &str, values: &[EnumValue]) -> EnumMeta { + pub fn new(name: &'a str, values: &[EnumValue]) -> EnumMeta<'a> { EnumMeta { - name: name.to_owned(), + name: name, description: None, values: values.to_vec(), try_parse_fn: Box::new( @@ -430,22 +430,22 @@ impl EnumMeta { /// Set the description of the type /// /// If a description was provided prior to calling this method, it will be overwritten - pub fn description(mut self, description: &str) -> EnumMeta { + pub fn description(mut self, description: &str) -> EnumMeta<'a> { self.description = Some(description.to_owned()); self } /// Wrap this enum type in a generic meta type - pub fn into_meta(self) -> MetaType { + pub fn into_meta(self) -> MetaType<'a> { MetaType::Enum(self) } } -impl InterfaceMeta { +impl<'a> InterfaceMeta<'a> { /// Build a new interface type with the specified name and fields - pub fn new(name: &str, fields: &[Field]) -> InterfaceMeta { + pub fn new(name: &'a str, fields: &[Field<'a>]) -> InterfaceMeta<'a> { InterfaceMeta { - name: name.to_owned(), + name: name, description: None, fields: fields.to_vec(), } @@ -454,22 +454,22 @@ impl InterfaceMeta { /// Set the description of the type /// /// If a description was provided prior to calling this method, it will be overwritten. - pub fn description(mut self, description: &str) -> InterfaceMeta { + pub fn description(mut self, description: &str) -> InterfaceMeta<'a> { self.description = Some(description.to_owned()); self } /// Wrap this interface type in a generic meta type - pub fn into_meta(self) -> MetaType { + pub fn into_meta(self) -> MetaType<'a> { MetaType::Interface(self) } } -impl UnionMeta { +impl<'a> UnionMeta<'a> { /// Build a new union type with the specified name and possible types - pub fn new(name: &str, of_types: &[Type]) -> UnionMeta { + pub fn new(name: &'a str, of_types: &[Type]) -> UnionMeta<'a> { UnionMeta { - name: name.to_owned(), + name: name, description: None, of_type_names: of_types.iter() .map(|t| t.innermost_name().to_owned()).collect(), @@ -479,22 +479,22 @@ impl UnionMeta { /// Set the description of the type /// /// If a description was provided prior to calling this method, it will be overwritten. - pub fn description(mut self, description: &str) -> UnionMeta { + pub fn description(mut self, description: &str) -> UnionMeta<'a> { self.description = Some(description.to_owned()); self } /// Wrap this union type in a generic meta type - pub fn into_meta(self) -> MetaType { + pub fn into_meta(self) -> MetaType<'a> { MetaType::Union(self) } } -impl InputObjectMeta { +impl<'a> InputObjectMeta<'a> { /// Build a new input type with the specified name and input fields - pub fn new(name: &str, input_fields: &[Argument]) -> InputObjectMeta { + pub fn new(name: &'a str, input_fields: &[Argument<'a>]) -> InputObjectMeta<'a> { InputObjectMeta { - name: name.to_owned(), + name: name, description: None, input_fields: input_fields.to_vec(), try_parse_fn: Box::new( @@ -505,22 +505,22 @@ impl InputObjectMeta { /// Set the description of the type /// /// If a description was provided prior to calling this method, it will be overwritten. - pub fn description(mut self, description: &str) -> InputObjectMeta { + pub fn description(mut self, description: &str) -> InputObjectMeta<'a> { self.description = Some(description.to_owned()); self } /// Wrap this union type in a generic meta type - pub fn into_meta(self) -> MetaType { + pub fn into_meta(self) -> MetaType<'a> { MetaType::InputObject(self) } } -impl Field { +impl<'a> Field<'a> { /// Set the description of the field /// /// This overwrites the description if any was previously set. - pub fn description(mut self, description: &str) -> Field { + pub fn description(mut self, description: &str) -> Field<'a> { self.description = Some(description.to_owned()); self } @@ -528,7 +528,7 @@ impl Field { /// Add an argument to the field /// /// Arguments are unordered and can't contain duplicates by name. - pub fn argument(mut self, argument: Argument) -> Field { + pub fn argument(mut self, argument: Argument<'a>) -> Field<'a> { match self.arguments { None => { self.arguments = Some(vec![argument]); } Some(ref mut args) => { args.push(argument); } @@ -540,15 +540,15 @@ impl Field { /// Set the deprecation reason /// /// This overwrites the deprecation reason if any was previously set. - pub fn deprecated(mut self, reason: &str) -> Field { + pub fn deprecated(mut self, reason: &str) -> Field<'a> { self.deprecation_reason = Some(reason.to_owned()); self } } -impl Argument { +impl<'a> Argument<'a> { #[doc(hidden)] - pub fn new(name: &str, arg_type: Type) -> Argument { + pub fn new(name: &str, arg_type: Type<'a>) -> Argument<'a> { Argument { name: name.to_owned(), description: None, @@ -560,7 +560,7 @@ impl Argument { /// Set the description of the argument /// /// This overwrites the description if any was previously set. - pub fn description(mut self, description: &str) -> Argument { + pub fn description(mut self, description: &str) -> Argument<'a> { self.description = Some(description.to_owned()); self } @@ -568,7 +568,7 @@ impl Argument { /// Set the default value of the argument /// /// This overwrites the description if any was previously set. - pub fn default_value(mut self, default_value: InputValue) -> Argument { + pub fn default_value(mut self, default_value: InputValue) -> Argument<'a> { self.default_value = Some(default_value); self } @@ -601,7 +601,7 @@ impl EnumValue { } } -impl fmt::Debug for ScalarMeta { +impl<'a> fmt::Debug for ScalarMeta<'a> { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_struct("ScalarMeta") .field("name", &self.name) @@ -610,7 +610,7 @@ impl fmt::Debug for ScalarMeta { } } -impl fmt::Debug for EnumMeta { +impl<'a> fmt::Debug for EnumMeta<'a> { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_struct("EnumMeta") .field("name", &self.name) @@ -620,7 +620,7 @@ impl fmt::Debug for EnumMeta { } } -impl fmt::Debug for InputObjectMeta { +impl<'a> fmt::Debug for InputObjectMeta<'a> { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_struct("InputObjectMeta") .field("name", &self.name) diff --git a/src/schema/model.rs b/src/schema/model.rs index 870ac477..a33c9faf 100644 --- a/src/schema/model.rs +++ b/src/schema/model.rs @@ -10,36 +10,36 @@ use schema::meta::{MetaType, ObjectMeta, PlaceholderMeta, UnionMeta, InterfaceMe /// /// This brings the mutation and query types together, and provides the /// predefined metadata fields. -pub struct RootNode { +pub struct RootNode<'a, QueryT, MutationT> { #[doc(hidden)] pub query_type: QueryT, #[doc(hidden)] pub mutation_type: MutationT, #[doc(hidden)] - pub schema: SchemaType, + pub schema: SchemaType<'a>, } /// Metadata for a schema -pub struct SchemaType { - types: HashMap, +pub struct SchemaType<'a> { + types: HashMap>, query_type_name: String, mutation_type_name: Option, - directives: HashMap, + directives: HashMap>, } -impl Context for SchemaType {} +impl<'a> Context for SchemaType<'a> {} pub enum TypeType<'a> { - Concrete(&'a MetaType), + Concrete(&'a MetaType<'a>), NonNull(Box>), List(Box>), } -pub struct DirectiveType { +pub struct DirectiveType<'a> { pub name: String, pub description: Option, pub locations: Vec, - pub arguments: Vec, + pub arguments: Vec>, } #[derive(Clone, PartialEq, Eq, Debug)] @@ -52,7 +52,7 @@ pub enum DirectiveLocation { InlineFragment, } -impl RootNode +impl<'a, QueryT, MutationT> RootNode<'a, QueryT, MutationT> where QueryT: GraphQLType, MutationT: GraphQLType, { @@ -60,7 +60,7 @@ impl RootNode /// /// If the schema should not support mutations, use the /// `new` constructor instead. - pub fn new(query_obj: QueryT, mutation_obj: MutationT) -> RootNode { + pub fn new(query_obj: QueryT, mutation_obj: MutationT) -> RootNode<'a, QueryT, MutationT> { RootNode { query_type: query_obj, mutation_type: mutation_obj, @@ -69,8 +69,8 @@ impl RootNode } } -impl SchemaType { - pub fn new() -> SchemaType +impl<'a> SchemaType<'a> { + pub fn new() -> SchemaType<'a> where QueryT: GraphQLType, MutationT: GraphQLType, { @@ -122,7 +122,7 @@ impl SchemaType { } } - pub fn add_directive(&mut self, directive: DirectiveType) { + pub fn add_directive(&mut self, directive: DirectiveType<'a>) { self.directives.insert(directive.name.clone(), directive); } @@ -230,7 +230,7 @@ impl SchemaType { .any(|t| (t as *const MetaType) == (possible_type as *const MetaType)) } - pub fn is_subtype(&self, sub_type: &Type, super_type: &Type) -> bool { + pub fn is_subtype<'b>(&self, sub_type: &Type<'b>, super_type: &Type<'b>) -> bool { use ast::Type::*; if super_type == sub_type { @@ -274,8 +274,8 @@ impl<'a> TypeType<'a> { } } -impl DirectiveType { - pub fn new(name: &str, locations: &[DirectiveLocation], arguments: &[Argument]) -> DirectiveType { +impl<'a> DirectiveType<'a> { + pub fn new(name: &str, locations: &[DirectiveLocation], arguments: &[Argument<'a>]) -> DirectiveType<'a> { DirectiveType { name: name.to_owned(), description: None, @@ -284,7 +284,7 @@ impl DirectiveType { } } - fn new_skip(registry: &mut Registry) -> DirectiveType { + fn new_skip(registry: &mut Registry<'a>) -> DirectiveType<'a> { Self::new( "skip", &[ @@ -297,7 +297,7 @@ impl DirectiveType { ]) } - fn new_include(registry: &mut Registry) -> DirectiveType { + fn new_include(registry: &mut Registry<'a>) -> DirectiveType<'a> { Self::new( "include", &[ @@ -310,7 +310,7 @@ impl DirectiveType { ]) } - pub fn description(mut self, description: &str) -> DirectiveType { + pub fn description(mut self, description: &str) -> DirectiveType<'a> { self.description = Some(description.to_owned()); self } diff --git a/src/schema/schema.rs b/src/schema/schema.rs index a7fca03e..9cfd737e 100644 --- a/src/schema/schema.rs +++ b/src/schema/schema.rs @@ -7,7 +7,7 @@ use schema::meta::{MetaType, ObjectMeta, EnumMeta, InputObjectMeta, UnionMeta, I Field, Argument, EnumValue}; use schema::model::{RootNode, SchemaType, TypeType, DirectiveType, DirectiveLocation}; -impl GraphQLType for RootNode +impl<'a, CtxT, QueryT, MutationT> GraphQLType for RootNode<'a, QueryT, MutationT> where QueryT: GraphQLType, MutationT: GraphQLType { @@ -17,7 +17,7 @@ impl GraphQLType for RootNode QueryT::name() } - fn meta(registry: &mut Registry) -> MetaType { + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { QueryT::meta(registry) } @@ -33,7 +33,7 @@ impl GraphQLType for RootNode } } -graphql_object!(SchemaType: SchemaType as "__Schema" |&self| { +graphql_object!(<'a> SchemaType<'a>: SchemaType<'a> as "__Schema" |&self| { field types() -> Vec { self.type_list() } @@ -51,7 +51,7 @@ graphql_object!(SchemaType: SchemaType as "__Schema" |&self| { } }); -graphql_object!(<'a> TypeType<'a>: SchemaType as "__Type" |&self| { +graphql_object!(<'a> TypeType<'a>: SchemaType<'a> as "__Type" |&self| { field name() -> Option<&str> { match *self { TypeType::Concrete(t) => t.name(), @@ -123,12 +123,12 @@ graphql_object!(<'a> TypeType<'a>: SchemaType as "__Type" |&self| { .filter_map(|tn| schema.type_by_name(tn)) .collect()) } - TypeType::Concrete(&MetaType::Interface(InterfaceMeta { name: ref iface_name, .. })) => { + TypeType::Concrete(&MetaType::Interface(InterfaceMeta { name: iface_name, .. })) => { Some(schema.concrete_type_list() .iter() .filter_map(|&ct| if let &MetaType::Object(ObjectMeta { ref name, ref interface_names, .. }) = ct { - if interface_names.contains(iface_name) { + if interface_names.contains(&iface_name.to_owned()) { schema.type_by_name(name) } else { None } } else { None } @@ -151,7 +151,7 @@ graphql_object!(<'a> TypeType<'a>: SchemaType as "__Type" |&self| { } }); -graphql_object!(Field: SchemaType as "__Field" |&self| { +graphql_object!(<'a> Field<'a>: SchemaType<'a> as "__Field" |&self| { field name() -> &String { &self.name } @@ -177,7 +177,7 @@ graphql_object!(Field: SchemaType as "__Field" |&self| { } }); -graphql_object!(Argument: SchemaType as "__InputValue" |&self| { +graphql_object!(<'a> Argument<'a>: SchemaType<'a> as "__InputValue" |&self| { field name() -> &String { &self.name } @@ -195,7 +195,7 @@ graphql_object!(Argument: SchemaType as "__InputValue" |&self| { } }); -graphql_object!(EnumValue: SchemaType as "__EnumValue" |&self| { +graphql_object!(EnumValue: () as "__EnumValue" |&self| { field name() -> &String { &self.name } @@ -225,7 +225,7 @@ graphql_enum!(TypeKind as "__TypeKind" { }); -graphql_object!(DirectiveType: SchemaType as "__Directive" |&self| { +graphql_object!(<'a> DirectiveType<'a>: SchemaType<'a> as "__Directive" |&self| { field name() -> &String { &self.name } diff --git a/src/types/base.rs b/src/types/base.rs index 8a7ab067..149669ba 100644 --- a/src/types/base.rs +++ b/src/types/base.rs @@ -152,17 +152,18 @@ impl GraphQLType for User { Some("User") } - fn meta(registry: &mut Registry) -> MetaType { + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { // First, we need to define all fields and their types on this type. // // If we need arguments, want to implement interfaces, or want to add // documentation strings, we can do it here. - registry.build_object_type::()(&[ - registry.field::<&String>("id"), - registry.field::<&String>("name"), - registry.field::>("friends"), - ]) - .into_meta() + let fields = &[ + registry.field::<&String>("id"), + registry.field::<&String>("name"), + registry.field::>("friends"), + ]; + + registry.build_object_type::(fields).into_meta() } fn resolve_field( @@ -224,7 +225,7 @@ pub trait GraphQLType: Sized { fn name() -> Option<&'static str>; /// The meta type representing this GraphQL type. - fn meta(registry: &mut Registry) -> MetaType; + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r>; /// Resolve the value of a single field on this type. /// diff --git a/src/types/containers.rs b/src/types/containers.rs index f7f1ef84..9591b41c 100644 --- a/src/types/containers.rs +++ b/src/types/containers.rs @@ -12,7 +12,7 @@ impl GraphQLType for Option where T: GraphQLType { None } - fn meta(registry: &mut Registry) -> MetaType { + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { registry.build_nullable_type::().into_meta() } @@ -52,7 +52,7 @@ impl GraphQLType for Vec where T: GraphQLType { None } - fn meta(registry: &mut Registry) -> MetaType { + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { registry.build_list_type::().into_meta() } @@ -99,7 +99,7 @@ impl<'a, T, CtxT> GraphQLType for &'a [T] where T: GraphQLType { None } - fn meta(registry: &mut Registry) -> MetaType { + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { registry.build_list_type::().into_meta() } diff --git a/src/types/pointers.rs b/src/types/pointers.rs index 8b3962f2..2ba17788 100644 --- a/src/types/pointers.rs +++ b/src/types/pointers.rs @@ -12,7 +12,7 @@ impl GraphQLType for Box where T: GraphQLType { T::name() } - fn meta(registry: &mut Registry) -> MetaType { + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { T::meta(registry) } @@ -52,7 +52,7 @@ impl<'a, T, CtxT> GraphQLType for &'a T where T: GraphQLType { T::name() } - fn meta(registry: &mut Registry) -> MetaType { + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { T::meta(registry) } diff --git a/src/types/scalars.rs b/src/types/scalars.rs index 4687803c..0b541523 100644 --- a/src/types/scalars.rs +++ b/src/types/scalars.rs @@ -49,7 +49,7 @@ impl<'a> GraphQLType for &'a str { Some("String") } - fn meta(registry: &mut Registry) -> MetaType { + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { registry.build_scalar_type::().into_meta() } @@ -116,7 +116,7 @@ impl GraphQLType for () { Some("__Unit") } - fn meta(registry: &mut Registry) -> MetaType { + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { registry.build_scalar_type::().into_meta() } } @@ -152,7 +152,7 @@ impl GraphQLType for EmptyMutation { Some("__EmptyMutation") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_object_type::()(&[]).into_meta() + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + registry.build_object_type::(&[]).into_meta() } } diff --git a/src/validation/context.rs b/src/validation/context.rs index 99994ec8..9073ac8a 100644 --- a/src/validation/context.rs +++ b/src/validation/context.rs @@ -16,13 +16,13 @@ pub struct RuleError { #[doc(hidden)] pub struct ValidatorContext<'a> { - pub schema: &'a SchemaType, + pub schema: &'a SchemaType<'a>, errors: Vec, - type_stack: Vec>, - type_literal_stack: Vec>, - input_type_stack: Vec>, - input_type_literal_stack: Vec>, - parent_type_stack: Vec>, + type_stack: Vec>>, + type_literal_stack: Vec>>, + input_type_stack: Vec>>, + input_type_literal_stack: Vec>>, + parent_type_stack: Vec>>, fragment_names: HashSet, } @@ -86,7 +86,7 @@ impl<'a> ValidatorContext<'a> { } #[doc(hidden)] - pub fn with_pushed_type(&mut self, t: Option<&Type>, f: F) + pub fn with_pushed_type(&mut self, t: Option<&Type<'a>>, f: F) -> R where F: FnOnce(&mut ValidatorContext<'a>) -> R { @@ -120,7 +120,7 @@ impl<'a> ValidatorContext<'a> { } #[doc(hidden)] - pub fn with_pushed_input_type(&mut self, t: Option<&Type>, f: F) + pub fn with_pushed_input_type(&mut self, t: Option<&Type<'a>>, f: F) -> R where F: FnOnce(&mut ValidatorContext<'a>) -> R { @@ -142,12 +142,12 @@ impl<'a> ValidatorContext<'a> { } #[doc(hidden)] - pub fn current_type(&self) -> Option<&'a MetaType> { + pub fn current_type(&self) -> Option<&'a MetaType<'a>> { *self.type_stack.last().unwrap_or(&None) } #[doc(hidden)] - pub fn current_type_literal(&self) -> Option<&Type> { + pub fn current_type_literal(&self) -> Option<&Type<'a>> { match self.type_literal_stack.last() { Some(&Some(ref t)) => Some(t), _ => None @@ -155,12 +155,12 @@ impl<'a> ValidatorContext<'a> { } #[doc(hidden)] - pub fn parent_type(&self) -> Option<&'a MetaType> { + pub fn parent_type(&self) -> Option<&'a MetaType<'a>> { *self.parent_type_stack.last().unwrap_or(&None) } #[doc(hidden)] - pub fn current_input_type_literal(&self) -> Option<&Type> { + pub fn current_input_type_literal(&self) -> Option<&Type<'a>> { match self.input_type_literal_stack.last() { Some(&Some(ref t)) => Some(t), _ => None, diff --git a/src/validation/rules/arguments_of_correct_type.rs b/src/validation/rules/arguments_of_correct_type.rs index b924174f..8a972324 100644 --- a/src/validation/rules/arguments_of_correct_type.rs +++ b/src/validation/rules/arguments_of_correct_type.rs @@ -5,7 +5,7 @@ use parser::Spanning; use validation::{Visitor, ValidatorContext}; pub struct ArgumentsOfCorrectType<'a> { - current_args: Option<&'a Vec>, + current_args: Option<&'a Vec>>, } pub fn factory<'a>() -> ArgumentsOfCorrectType<'a> { diff --git a/src/validation/rules/known_argument_names.rs b/src/validation/rules/known_argument_names.rs index c9c48ebc..1de82cb5 100644 --- a/src/validation/rules/known_argument_names.rs +++ b/src/validation/rules/known_argument_names.rs @@ -10,7 +10,7 @@ enum ArgumentPosition<'a> { } pub struct KnownArgumentNames<'a> { - current_args: Option<(ArgumentPosition<'a>, &'a Vec)>, + current_args: Option<(ArgumentPosition<'a>, &'a Vec>)>, } pub fn factory<'a>() -> KnownArgumentNames<'a> { diff --git a/src/validation/rules/overlapping_fields_can_be_merged.rs b/src/validation/rules/overlapping_fields_can_be_merged.rs index 2011279f..361d13c3 100644 --- a/src/validation/rules/overlapping_fields_can_be_merged.rs +++ b/src/validation/rules/overlapping_fields_can_be_merged.rs @@ -14,7 +14,7 @@ struct Conflict(ConflictReason, Vec, Vec); struct ConflictReason(String, ConflictReasonMessage); #[derive(Debug)] -struct AstAndDef<'a>(Option<&'a str>, &'a Spanning, Option<&'a FieldType>); +struct AstAndDef<'a>(Option<&'a str>, &'a Spanning, Option<&'a FieldType<'a>>); type AstAndDefCollection<'a> = OrderedMap<&'a str, Vec>>; @@ -1171,11 +1171,13 @@ mod tests { Some("SomeBox") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_interface_type::()(&[ - registry.field::>("deepBox"), - registry.field::>("unrelatedField"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>("deepBox"), + registry.field::>("unrelatedField"), + ]; + + registry.build_interface_type::(fields) .into_meta() } } @@ -1187,15 +1189,17 @@ mod tests { Some("StringBox") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_object_type::()(&[ - registry.field::>("scalar"), - registry.field::>("deepBox"), - registry.field::>("unrelatedField"), - registry.field::>>>("listStringBox"), - registry.field::>("stringBox"), - registry.field::>("intBox"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>("scalar"), + registry.field::>("deepBox"), + registry.field::>("unrelatedField"), + registry.field::>>>("listStringBox"), + registry.field::>("stringBox"), + registry.field::>("intBox"), + ]; + + registry.build_object_type::(fields) .interfaces(&[ registry.get_type::(), ]) @@ -1210,15 +1214,17 @@ mod tests { Some("IntBox") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_object_type::()(&[ - registry.field::>("scalar"), - registry.field::>("deepBox"), - registry.field::>("unrelatedField"), - registry.field::>>>("listStringBox"), - registry.field::>("stringBox"), - registry.field::>("intBox"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>("scalar"), + registry.field::>("deepBox"), + registry.field::>("unrelatedField"), + registry.field::>>>("listStringBox"), + registry.field::>("stringBox"), + registry.field::>("intBox"), + ]; + + registry.build_object_type::(fields) .interfaces(&[ registry.get_type::(), ]) @@ -1233,10 +1239,12 @@ mod tests { Some("NonNullStringBox1") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_interface_type::()(&[ - registry.field::("scalar"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::("scalar"), + ]; + + registry.build_interface_type::(fields) .into_meta() } } @@ -1248,12 +1256,14 @@ mod tests { Some("NonNullStringBox1Impl") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_object_type::()(&[ - registry.field::("scalar"), - registry.field::>("deepBox"), - registry.field::>("unrelatedField"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::("scalar"), + registry.field::>("deepBox"), + registry.field::>("unrelatedField"), + ]; + + registry.build_object_type::(fields) .interfaces(&[ registry.get_type::(), registry.get_type::(), @@ -1269,10 +1279,12 @@ mod tests { Some("NonNullStringBox2") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_interface_type::()(&[ - registry.field::("scalar"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::("scalar"), + ]; + + registry.build_interface_type::(fields) .into_meta() } } @@ -1284,12 +1296,14 @@ mod tests { Some("NonNullStringBox2Impl") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_object_type::()(&[ - registry.field::("scalar"), - registry.field::>("deepBox"), - registry.field::>("unrelatedField"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::("scalar"), + registry.field::>("deepBox"), + registry.field::>("unrelatedField"), + ]; + + registry.build_object_type::(fields) .interfaces(&[ registry.get_type::(), registry.get_type::(), @@ -1305,11 +1319,13 @@ mod tests { Some("Node") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_object_type::()(&[ - registry.field::>("id"), - registry.field::>("name"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>("id"), + registry.field::>("name"), + ]; + + registry.build_object_type::(fields) .into_meta() } } @@ -1321,10 +1337,12 @@ mod tests { Some("Edge") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_object_type::()(&[ - registry.field::>("node"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>("node"), + ]; + + registry.build_object_type::(fields) .into_meta() } } @@ -1336,10 +1354,12 @@ mod tests { Some("Connection") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_object_type::()(&[ - registry.field::>>>("edges"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>>>("edges"), + ]; + + registry.build_object_type::(fields) .into_meta() } } @@ -1351,16 +1371,17 @@ mod tests { Some("QueryRoot") } - fn meta(registry: &mut Registry) -> MetaType { + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { registry.get_type::(); registry.get_type::(); registry.get_type::(); registry.get_type::(); - registry.build_object_type::()(&[ - registry.field::>("someBox"), - registry.field::>("connection"), - ]) + let fields = &[ + registry.field::>("someBox"), + registry.field::>("connection"), + ]; + registry.build_object_type::(fields) .into_meta() } } diff --git a/src/validation/rules/possible_fragment_spreads.rs b/src/validation/rules/possible_fragment_spreads.rs index 3389f273..aed2cb7f 100644 --- a/src/validation/rules/possible_fragment_spreads.rs +++ b/src/validation/rules/possible_fragment_spreads.rs @@ -5,7 +5,7 @@ use parser::Spanning; use schema::meta::MetaType; pub struct PossibleFragmentSpreads<'a> { - fragment_types: HashMap<&'a str, &'a MetaType>, + fragment_types: HashMap<&'a str, &'a MetaType<'a>>, } pub fn factory<'a>() -> PossibleFragmentSpreads<'a> { diff --git a/src/validation/rules/variables_in_allowed_position.rs b/src/validation/rules/variables_in_allowed_position.rs index 007448c8..d7f5131d 100644 --- a/src/validation/rules/variables_in_allowed_position.rs +++ b/src/validation/rules/variables_in_allowed_position.rs @@ -12,8 +12,8 @@ pub enum Scope<'a> { pub struct VariableInAllowedPosition<'a> { spreads: HashMap, HashSet<&'a str>>, - variable_usages: HashMap, Vec<(Spanning<&'a String>, Type)>>, - variable_defs: HashMap, Vec<&'a (Spanning, VariableDefinition)>>, + variable_usages: HashMap, Vec<(Spanning<&'a String>, Type<'a>)>>, + variable_defs: HashMap, Vec<&'a (Spanning, VariableDefinition<'a>)>>, current_scope: Option>, } diff --git a/src/validation/test_harness.rs b/src/validation/test_harness.rs index 40edc7e0..3d0de147 100644 --- a/src/validation/test_harness.rs +++ b/src/validation/test_harness.rs @@ -58,11 +58,13 @@ impl GraphQLType for Being { Some("Being") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_interface_type::()(&[ - registry.field::>("name") - .argument(registry.arg::>("surname")), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields =&[ + registry.field::>("name") + .argument(registry.arg::>("surname")), + ]; + + registry.build_interface_type::(fields) .into_meta() } } @@ -74,11 +76,13 @@ impl GraphQLType for Pet { Some("Pet") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_interface_type::()(&[ - registry.field::>("name") - .argument(registry.arg::>("surname")), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>("name") + .argument(registry.arg::>("surname")), + ]; + + registry.build_interface_type::(fields) .into_meta() } } @@ -90,11 +94,13 @@ impl GraphQLType for Canine { Some("Canine") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_interface_type::()(&[ - registry.field::>("name") - .argument(registry.arg::>("surname")), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>("name") + .argument(registry.arg::>("surname")), + ]; + + registry.build_interface_type::(fields) .into_meta() } } @@ -106,13 +112,12 @@ impl GraphQLType for DogCommand { Some("DogCommand") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_enum_type::()(&[ - EnumValue::new("SIT"), - EnumValue::new("HEEL"), - EnumValue::new("DOWN"), - ]) - .into_meta() + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + registry.build_enum_type::(&[ + EnumValue::new("SIT"), + EnumValue::new("HEEL"), + EnumValue::new("DOWN"), + ]).into_meta() } } @@ -134,21 +139,23 @@ impl GraphQLType for Dog { Some("Dog") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_object_type::()(&[ - registry.field::>("name") - .argument(registry.arg::>("surname")), - registry.field::>("nickname"), - registry.field::>("barkVolume"), - registry.field::>("barks"), - registry.field::>("doesKnowCommand") - .argument(registry.arg::>("dogCommand")), - registry.field::>("isHousetrained") - .argument(registry.arg_with_default("atOtherHomes", &true)), - registry.field::>("isAtLocation") - .argument(registry.arg::>("x")) - .argument(registry.arg::>("y")), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>("name") + .argument(registry.arg::>("surname")), + registry.field::>("nickname"), + registry.field::>("barkVolume"), + registry.field::>("barks"), + registry.field::>("doesKnowCommand") + .argument(registry.arg::>("dogCommand")), + registry.field::>("isHousetrained") + .argument(registry.arg_with_default("atOtherHomes", &true)), + registry.field::>("isAtLocation") + .argument(registry.arg::>("x")) + .argument(registry.arg::>("y")), + ]; + + registry.build_object_type::(fields) .interfaces(&[ registry.get_type::(), registry.get_type::(), @@ -165,13 +172,13 @@ impl GraphQLType for FurColor { Some("FurColor") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_enum_type::()(&[ - EnumValue::new("BROWN"), - EnumValue::new("BLACK"), - EnumValue::new("TAN"), - EnumValue::new("SPOTTED"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + registry.build_enum_type::(&[ + EnumValue::new("BROWN"), + EnumValue::new("BLACK"), + EnumValue::new("TAN"), + EnumValue::new("SPOTTED"), + ]) .into_meta() } } @@ -195,15 +202,17 @@ impl GraphQLType for Cat { Some("Cat") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_object_type::()(&[ - registry.field::>("name") - .argument(registry.arg::>("surname")), - registry.field::>("nickname"), - registry.field::>("meows"), - registry.field::>("meowVolume"), - registry.field::>("furColor"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>("name") + .argument(registry.arg::>("surname")), + registry.field::>("nickname"), + registry.field::>("meows"), + registry.field::>("meowVolume"), + registry.field::>("furColor"), + ]; + + registry.build_object_type::(fields) .interfaces(&[ registry.get_type::(), registry.get_type::(), @@ -219,11 +228,13 @@ impl GraphQLType for CatOrDog { Some("CatOrDog") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_union_type::()(&[ - registry.get_type::(), - registry.get_type::(), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let types = &[ + registry.get_type::(), + registry.get_type::(), + ]; + + registry.build_union_type::(types) .into_meta() } } @@ -235,10 +246,12 @@ impl GraphQLType for Intelligent { Some("Intelligent") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_interface_type::()(&[ - registry.field::>("iq"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>("iq"), + ]; + + registry.build_interface_type::(fields) .into_meta() } } @@ -250,14 +263,15 @@ impl GraphQLType for Human { Some("Human") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_object_type::()(&[ - registry.field::>("name") - .argument(registry.arg::>("surname")), - registry.field::>>>("pets"), - registry.field::>>("relatives"), - registry.field::>("iq"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>("name") + .argument(registry.arg::>("surname")), + registry.field::>>>("pets"), + registry.field::>>("relatives"), + registry.field::>("iq"), + ]; + registry.build_object_type::(fields) .interfaces(&[ registry.get_type::(), registry.get_type::(), @@ -273,13 +287,15 @@ impl GraphQLType for Alien { Some("Alien") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_object_type::()(&[ - registry.field::>("name") - .argument(registry.arg::>("surname")), - registry.field::>("iq"), - registry.field::>("numEyes"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>("name") + .argument(registry.arg::>("surname")), + registry.field::>("iq"), + registry.field::>("numEyes"), + ]; + + registry.build_object_type::(fields) .interfaces(&[ registry.get_type::(), registry.get_type::(), @@ -295,11 +311,13 @@ impl GraphQLType for DogOrHuman { Some("DogOrHuman") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_union_type::()(&[ - registry.get_type::(), - registry.get_type::(), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let types = &[ + registry.get_type::(), + registry.get_type::(), + ]; + + registry.build_union_type::(types) .into_meta() } } @@ -311,11 +329,13 @@ impl GraphQLType for HumanOrAlien { Some("HumanOrAlien") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_union_type::()(&[ - registry.get_type::(), - registry.get_type::(), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let types = &[ + registry.get_type::(), + registry.get_type::(), + ]; + + registry.build_union_type::(types) .into_meta() } } @@ -327,14 +347,16 @@ impl GraphQLType for ComplexInput { Some("ComplexInput") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_input_object_type::()(&[ - registry.arg::("requiredField"), - registry.arg::>("intField"), - registry.arg::>("stringField"), - registry.arg::>("booleanField"), - registry.arg::>>>("stringListField"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.arg::("requiredField"), + registry.arg::>("intField"), + registry.arg::>("stringField"), + registry.arg::>("booleanField"), + registry.arg::>>>("stringListField"), + ]; + + registry.build_input_object_type::(fields) .into_meta() } } @@ -366,38 +388,40 @@ impl GraphQLType for ComplicatedArgs { Some("ComplicatedArgs") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_object_type::()(&[ - registry.field::>("intArgField") - .argument(registry.arg::>("intArg")), - registry.field::>("nonNullIntArgField") - .argument(registry.arg::("nonNullIntArg")), - registry.field::>("stringArgField") - .argument(registry.arg::>("stringArg")), - registry.field::>("booleanArgField") - .argument(registry.arg::>("booleanArg")), - registry.field::>("enumArgField") - .argument(registry.arg::>("enumArg")), - registry.field::>("floatArgField") - .argument(registry.arg::>("floatArg")), - registry.field::>("idArgField") - .argument(registry.arg::>("idArg")), - registry.field::>("stringListArgField") - .argument(registry.arg::>>>("stringListArg")), - registry.field::>("complexArgField") - .argument(registry.arg::>("complexArg")), - registry.field::>("multipleReqs") - .argument(registry.arg::("req1")) - .argument(registry.arg::("req2")), - registry.field::>("multipleOpts") - .argument(registry.arg_with_default("opt1", &0i64)) - .argument(registry.arg_with_default("opt2", &0i64)), - registry.field::>("multipleOptAndReq") - .argument(registry.arg::("req1")) - .argument(registry.arg::("req2")) - .argument(registry.arg_with_default("opt1", &0i64)) - .argument(registry.arg_with_default("opt2", &0i64)), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>("intArgField") + .argument(registry.arg::>("intArg")), + registry.field::>("nonNullIntArgField") + .argument(registry.arg::("nonNullIntArg")), + registry.field::>("stringArgField") + .argument(registry.arg::>("stringArg")), + registry.field::>("booleanArgField") + .argument(registry.arg::>("booleanArg")), + registry.field::>("enumArgField") + .argument(registry.arg::>("enumArg")), + registry.field::>("floatArgField") + .argument(registry.arg::>("floatArg")), + registry.field::>("idArgField") + .argument(registry.arg::>("idArg")), + registry.field::>("stringListArgField") + .argument(registry.arg::>>>("stringListArg")), + registry.field::>("complexArgField") + .argument(registry.arg::>("complexArg")), + registry.field::>("multipleReqs") + .argument(registry.arg::("req1")) + .argument(registry.arg::("req2")), + registry.field::>("multipleOpts") + .argument(registry.arg_with_default("opt1", &0i64)) + .argument(registry.arg_with_default("opt2", &0i64)), + registry.field::>("multipleOptAndReq") + .argument(registry.arg::("req1")) + .argument(registry.arg::("req2")) + .argument(registry.arg_with_default("opt1", &0i64)) + .argument(registry.arg_with_default("opt2", &0i64)), + ]; + + registry.build_object_type::(fields) .into_meta() } } @@ -409,19 +433,21 @@ impl GraphQLType for QueryRoot { Some("QueryRoot") } - fn meta(registry: &mut Registry) -> MetaType { - registry.build_object_type::()(&[ - registry.field::>("human") - .argument(registry.arg::>("id")), - registry.field::>("alien"), - registry.field::>("dog"), - registry.field::>("cat"), - registry.field::>("pet"), - registry.field::>("catOrDog"), - registry.field::>("dorOrHuman"), - registry.field::>("humanOrAlien"), - registry.field::>("complicatedArgs"), - ]) + fn meta<'r>(registry: &mut Registry<'r>) -> MetaType<'r> { + let fields = &[ + registry.field::>("human") + .argument(registry.arg::>("id")), + registry.field::>("alien"), + registry.field::>("dog"), + registry.field::>("cat"), + registry.field::>("pet"), + registry.field::>("catOrDog"), + registry.field::>("dorOrHuman"), + registry.field::>("humanOrAlien"), + registry.field::>("complicatedArgs"), + ]; + + registry.build_object_type::(fields) .into_meta() } } diff --git a/src/validation/visitor.rs b/src/validation/visitor.rs index a3814280..8c5cd255 100644 --- a/src/validation/visitor.rs +++ b/src/validation/visitor.rs @@ -17,14 +17,14 @@ fn visit_definitions<'a, V: Visitor<'a>>(v: &mut V, ctx: &mut ValidatorContext<' let def_type = match *def { Definition::Fragment(Spanning { item: Fragment { type_condition: Spanning { item: ref name, .. }, .. }, .. }) => - Some(Type::NonNullNamed(name.to_owned())), + Some(Type::NonNullNamed(name)), Definition::Operation(Spanning { item: Operation { operation_type: OperationType::Query, .. }, .. }) => - Some(Type::NonNullNamed(ctx.schema.concrete_query_type().name().unwrap().to_owned())), + Some(Type::NonNullNamed(ctx.schema.concrete_query_type().name().unwrap())), Definition::Operation(Spanning { item: Operation { operation_type: OperationType::Mutation, .. }, .. }) => ctx.schema.concrete_mutation_type() - .map(|t| Type::NonNullNamed(t.name().unwrap().to_owned())), + .map(|t| Type::NonNullNamed(t.name().unwrap())), }; ctx.with_pushed_type(def_type.as_ref(), |ctx| { @@ -93,7 +93,7 @@ fn visit_directives<'a, V: Visitor<'a>>(v: &mut V, ctx: &mut ValidatorContext<'a } } -fn visit_arguments<'a, V: Visitor<'a>>(v: &mut V, ctx: &mut ValidatorContext<'a>, meta_args: &Option<&Vec>, arguments: &'a Option>) { +fn visit_arguments<'a, V: Visitor<'a>>(v: &mut V, ctx: &mut ValidatorContext<'a>, meta_args: &Option<&Vec>>, arguments: &'a Option>) { if let Some(ref arguments) = *arguments { for argument in arguments.item.iter() { let arg_type = meta_args @@ -161,7 +161,7 @@ fn visit_fragment_spread<'a, V: Visitor<'a>>(v: &mut V, ctx: &mut ValidatorConte } fn visit_inline_fragment<'a, V: Visitor<'a>>(v: &mut V, ctx: &mut ValidatorContext<'a>, fragment: &'a Spanning) { - let type_name = fragment.item.type_condition.clone().map(|s| s.item); + let type_name = fragment.item.type_condition.as_ref().map(|s| s.item.as_str()); let mut visit_fn = move |ctx: &mut ValidatorContext<'a>| { v.enter_inline_fragment(ctx, fragment);