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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
use crate::registry::Registry; use crate::{ErrorWithPosition, GQLInputValue, GQLType, QueryError, Result}; use fnv::FnvHasher; use graphql_parser::query::{ Directive, Field, FragmentDefinition, SelectionSet, Value, VariableDefinition, }; use std::any::{Any, TypeId}; use std::collections::{BTreeMap, HashMap}; use std::hash::BuildHasherDefault; use std::ops::{Deref, DerefMut}; #[derive(Default)] pub struct Variables(BTreeMap<String, Value>); impl Deref for Variables { type Target = BTreeMap<String, Value>; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for Variables { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl Variables { pub(crate) fn parse_from_json(value: serde_json::Value) -> Result<Self> { let gql_value = json_value_to_gql_value(value); if let Value::Object(obj) = gql_value { Ok(Variables(obj)) } else { Ok(Default::default()) } } } fn json_value_to_gql_value(value: serde_json::Value) -> Value { match value { serde_json::Value::Null => Value::Null, serde_json::Value::Bool(n) => Value::Boolean(n), serde_json::Value::Number(n) if n.is_f64() => Value::Float(n.as_f64().unwrap()), serde_json::Value::Number(n) => Value::Int((n.as_i64().unwrap() as i32).into()), serde_json::Value::String(s) => Value::String(s), serde_json::Value::Array(ls) => Value::List( ls.into_iter() .map(|value| json_value_to_gql_value(value)) .collect(), ), serde_json::Value::Object(obj) => Value::Object( obj.into_iter() .map(|(name, value)| (name, json_value_to_gql_value(value))) .collect(), ), } } #[derive(Default)] pub struct Data(HashMap<TypeId, Box<dyn Any + Sync + Send>, BuildHasherDefault<FnvHasher>>); impl Data { pub fn insert<D: Any + Send + Sync>(&mut self, data: D) { self.0.insert(TypeId::of::<D>(), Box::new(data)); } } pub type ContextSelectionSet<'a> = ContextBase<'a, &'a SelectionSet>; pub type Context<'a> = ContextBase<'a, &'a Field>; pub struct ContextBase<'a, T> { pub(crate) item: T, pub(crate) variables: Option<&'a Variables>, pub(crate) variable_definitions: Option<&'a [VariableDefinition]>, pub(crate) registry: &'a Registry, pub(crate) data: &'a Data, pub(crate) fragments: &'a HashMap<String, &'a FragmentDefinition>, } impl<'a, T> Deref for ContextBase<'a, T> { type Target = T; fn deref(&self) -> &Self::Target { &self.item } } impl<'a, T> ContextBase<'a, T> { #[doc(hidden)] pub fn with_item<R>(&self, item: R) -> ContextBase<'a, R> { ContextBase { item, variables: self.variables, variable_definitions: self.variable_definitions, registry: self.registry.clone(), data: self.data, fragments: self.fragments, } } pub fn data<D: Any + Send + Sync>(&self) -> &D { self.data .0 .get(&TypeId::of::<D>()) .and_then(|d| d.downcast_ref::<D>()) .expect("The specified data type does not exist.") } fn resolve_input_value(&self, value: Value) -> Result<Value> { if let Value::Variable(var_name) = value { let def = self .variable_definitions .and_then(|defs| defs.iter().find(|def| def.name == var_name.as_str())); if let Some(def) = def { if let Some(var_value) = self.variables.map(|vars| vars.get(&def.name)).flatten() { return Ok(var_value.clone()); } else if let Some(default) = &def.default_value { return Ok(default.clone()); } } return Err(QueryError::VarNotDefined { var_name: var_name.clone(), } .into()); } else { Ok(value) } } #[doc(hidden)] pub fn is_skip(&self, directives: &[Directive]) -> Result<bool> { for directive in directives { if directive.name == "skip" { if let Some(value) = directive .arguments .iter() .find(|(name, _)| name == "if") .map(|(_, value)| value) { let value = self.resolve_input_value(value.clone())?; let res: bool = GQLInputValue::parse(&value).ok_or_else(|| { QueryError::ExpectedType { expect: bool::qualified_type_name(), actual: value, } .with_position(directive.position) })?; if res { return Ok(true); } } else { return Err(QueryError::RequiredDirectiveArgs { directive: "@skip", arg_name: "if", arg_type: "Boolean!", } .with_position(directive.position) .into()); } } else if directive.name == "include" { if let Some(value) = directive .arguments .iter() .find(|(name, _)| name == "if") .map(|(_, value)| value) { let value = self.resolve_input_value(value.clone())?; let res: bool = GQLInputValue::parse(&value).ok_or_else(|| { QueryError::ExpectedType { expect: bool::qualified_type_name(), actual: value, } .with_position(directive.position) })?; if !res { return Ok(true); } } else { return Err(QueryError::RequiredDirectiveArgs { directive: "@include", arg_name: "if", arg_type: "Boolean!", } .with_position(directive.position) .into()); } } else { return Err(QueryError::UnknownDirective { name: directive.name.clone(), } .with_position(directive.position) .into()); } } Ok(false) } } impl<'a> ContextBase<'a, &'a Field> { #[doc(hidden)] pub fn param_value<T: GQLInputValue, F: FnOnce() -> Value>( &self, name: &str, default: F, ) -> Result<T> { match self .arguments .iter() .find(|(n, _)| n == name) .map(|(_, v)| v) .cloned() { Some(value) => { let value = self.resolve_input_value(value)?; let res = GQLInputValue::parse(&value).ok_or_else(|| { QueryError::ExpectedType { expect: T::qualified_type_name(), actual: value, } .with_position(self.item.position) })?; Ok(res) } None => { let value = default(); let res = GQLInputValue::parse(&value).ok_or_else(|| { QueryError::ExpectedType { expect: T::qualified_type_name(), actual: value.clone(), } .with_position(self.item.position) })?; Ok(res) } } } #[doc(hidden)] pub fn result_name(&self) -> String { self.item.alias.clone().unwrap_or_else(|| self.name.clone()) } }