1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
use ast::{FromInputValue, InputValue, Selection, ToInputValue}; use schema::meta::MetaType; use value::Value; use executor::{Executor, Registry}; use types::base::GraphQLType; impl<T, CtxT> GraphQLType for Option<T> where T: GraphQLType<Context = CtxT>, { type Context = CtxT; type TypeInfo = T::TypeInfo; fn name(_: &T::TypeInfo) -> Option<&str> { None } fn meta<'r>(info: &T::TypeInfo, registry: &mut Registry<'r>) -> MetaType<'r> { registry.build_nullable_type::<T>(info).into_meta() } fn resolve( &self, info: &T::TypeInfo, _: Option<&[Selection]>, executor: &Executor<CtxT>, ) -> Value { match *self { Some(ref obj) => executor.resolve_into_value(info, obj), None => Value::null(), } } } impl<T> FromInputValue for Option<T> where T: FromInputValue, { fn from_input_value(v: &InputValue) -> Option<Option<T>> { match v { &InputValue::Null => Some(None), v => match v.convert() { Some(x) => Some(Some(x)), None => None, }, } } } impl<T> ToInputValue for Option<T> where T: ToInputValue, { fn to_input_value(&self) -> InputValue { match *self { Some(ref v) => v.to_input_value(), None => InputValue::null(), } } } impl<T, CtxT> GraphQLType for Vec<T> where T: GraphQLType<Context = CtxT>, { type Context = CtxT; type TypeInfo = T::TypeInfo; fn name(_: &T::TypeInfo) -> Option<&str> { None } fn meta<'r>(info: &T::TypeInfo, registry: &mut Registry<'r>) -> MetaType<'r> { registry.build_list_type::<T>(info).into_meta() } fn resolve( &self, info: &T::TypeInfo, _: Option<&[Selection]>, executor: &Executor<CtxT>, ) -> Value { resolve_into_list(executor, info, self.iter()) } } impl<T> FromInputValue for Vec<T> where T: FromInputValue, { fn from_input_value(v: &InputValue) -> Option<Vec<T>> { match *v { InputValue::List(ref ls) => { let v: Vec<_> = ls.iter().filter_map(|i| i.item.convert()).collect(); if v.len() == ls.len() { Some(v) } else { None } } ref other => if let Some(e) = other.convert() { Some(vec![e]) } else { None }, } } } impl<T> ToInputValue for Vec<T> where T: ToInputValue, { fn to_input_value(&self) -> InputValue { InputValue::list(self.iter().map(|v| v.to_input_value()).collect()) } } impl<'a, T, CtxT> GraphQLType for &'a [T] where T: GraphQLType<Context = CtxT>, { type Context = CtxT; type TypeInfo = T::TypeInfo; fn name(_: &T::TypeInfo) -> Option<&str> { None } fn meta<'r>(info: &T::TypeInfo, registry: &mut Registry<'r>) -> MetaType<'r> { registry.build_list_type::<T>(info).into_meta() } fn resolve( &self, info: &T::TypeInfo, _: Option<&[Selection]>, executor: &Executor<CtxT>, ) -> Value { resolve_into_list(executor, info, self.iter()) } } impl<'a, T> ToInputValue for &'a [T] where T: ToInputValue, { fn to_input_value(&self) -> InputValue { InputValue::list(self.iter().map(|v| v.to_input_value()).collect()) } } fn resolve_into_list<T, I>(executor: &Executor<T::Context>, info: &T::TypeInfo, iter: I) -> Value where I: Iterator<Item = T> + ExactSizeIterator, T: GraphQLType, { let stop_on_null = executor .current_type() .list_contents() .expect("Current type is not a list type") .is_non_null(); let mut result = Vec::with_capacity(iter.len()); for o in iter { let value = executor.resolve_into_value(info, &o); if stop_on_null && value.is_null() { return value; } result.push(value); } Value::list(result) }