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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use std::fmt;
use std::collections::HashMap;
use std::hash::Hash;
use std::vec;
use std::slice;

use rustc_serialize::json::{ToJson, Json};

use parser::Spanning;

/// A type literal in the syntax tree
///
/// This enum carries no semantic information and might refer to types that do
/// not exist.
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum Type {
    /// A nullable named type, e.g. `String`
    Named(String),
    /// A nullable list type, e.g. `[String]`
    ///
    /// The list itself is what's nullable, the containing type might be non-null.
    List(Box<Type>),
    /// A non-null named type, e.g. `String!`
    NonNullNamed(String),
    /// A non-null list type, e.g. `[String]!`.
    ///
    /// The list itself is what's non-null, the containing type might be null.
    NonNullList(Box<Type>),
}

/// A JSON-like value that can be passed into the query execution, either
/// out-of-band, or in-band as default variable values. These are _not_ constant
/// and might contain variables.
///
/// Lists and objects variants are _spanned_, i.e. they contain a reference to
/// their position in the source file, if available.
#[derive(Clone, PartialEq, Debug)]
#[allow(missing_docs)]
pub enum InputValue {
    Null,
    Int(i64),
    Float(f64),
    String(String),
    Boolean(bool),
    Enum(String),
    Variable(String),
    List(Vec<Spanning<InputValue>>),
    Object(Vec<(Spanning<String>, Spanning<InputValue>)>),
}

#[derive(Clone, PartialEq, Debug)]
pub struct VariableDefinition {
    pub var_type: Spanning<Type>,
    pub default_value: Option<Spanning<InputValue>>,
}

#[derive(Clone, PartialEq, Debug)]
pub struct Arguments {
    pub items: Vec<(Spanning<String>, Spanning<InputValue>)>,
}

#[derive(Clone, PartialEq, Debug)]
pub struct VariableDefinitions {
    pub items: Vec<(Spanning<String>, VariableDefinition)>,
}

#[derive(Clone, PartialEq, Debug)]
pub struct Field {
    pub alias: Option<Spanning<String>>,
    pub name: Spanning<String>,
    pub arguments: Option<Spanning<Arguments>>,
    pub directives: Option<Vec<Spanning<Directive>>>,
    pub selection_set: Option<Vec<Selection>>,
}

#[derive(Clone, PartialEq, Debug)]
pub struct FragmentSpread {
    pub name: Spanning<String>,
    pub directives: Option<Vec<Spanning<Directive>>>,
}

#[derive(Clone, PartialEq, Debug)]
pub struct InlineFragment {
    pub type_condition: Option<Spanning<String>>,
    pub directives: Option<Vec<Spanning<Directive>>>,
    pub selection_set: Vec<Selection>,
}

/// Entry in a GraphQL selection set
///
/// This enum represents one of the three variants of a selection that exists
/// in GraphQL: a field, a fragment spread, or an inline fragment. Each of the
/// variants references their location in the query source.
///
/// ```text
/// {
///   field(withArg: 123) { subField }
///   ...fragmentSpread
///   ...on User {
///     inlineFragmentField
///   }
/// }
/// ```
#[derive(Clone, PartialEq, Debug)]
#[allow(missing_docs)]
pub enum Selection {
    Field(Spanning<Field>),
    FragmentSpread(Spanning<FragmentSpread>),
    InlineFragment(Spanning<InlineFragment>),
}

#[derive(Clone, PartialEq, Debug)]
pub struct Directive {
    pub name: Spanning<String>,
    pub arguments: Option<Spanning<Arguments>>,
}

#[derive(Clone, PartialEq, Debug)]
pub enum OperationType {
    Query,
    Mutation,
}

#[derive(Clone, PartialEq, Debug)]
pub struct Operation {
    pub operation_type: OperationType,
    pub name: Option<Spanning<String>>,
    pub variable_definitions: Option<Spanning<VariableDefinitions>>,
    pub directives: Option<Vec<Spanning<Directive>>>,
    pub selection_set: Vec<Selection>,
}

#[derive(Clone, PartialEq, Debug)]
pub struct Fragment {
    pub name: Spanning<String>,
    pub type_condition: Spanning<String>,
    pub directives: Option<Vec<Spanning<Directive>>>,
    pub selection_set: Vec<Selection>,
}

#[derive(Clone, PartialEq, Debug)]
pub enum Definition {
    Operation(Spanning<Operation>),
    Fragment(Spanning<Fragment>),
}

pub type Document = Vec<Definition>;

/// Parse an unstructured input value into a Rust data type.
///
/// The conversion _can_ fail, and must in that case return None. Implemented
/// automatically by the convenience macros `graphql_enum!` and
/// `graphql_scalar!`. Must be implemented manually when manually exposing new
/// enums or scalars.
pub trait FromInputValue: Sized {
    /// Performs the conversion.
    fn from(v: &InputValue) -> Option<Self>;
}

/// Losslessly clones a Rust data type into an InputValue.
pub trait ToInputValue: Sized {
    /// Performs the conversion.
    fn to(&self) -> InputValue;
}

impl Type {
    /// Get the name of a named type.
    ///
    /// Only applies to named types; lists will return `None`.
    pub fn name(&self) -> Option<&str> {
        match *self {
            Type::Named(ref n) | Type::NonNullNamed(ref n) => Some(n),
            _ => None
        }
    }

    /// Get the innermost name by unpacking lists
    ///
    /// All type literals contain exactly one named type.
    pub fn innermost_name(&self) -> &str {
        match *self {
            Type::Named(ref n) | Type::NonNullNamed(ref n) => n,
            Type::List(ref l) | Type::NonNullList(ref l) => l.innermost_name(),
        }
    }

    /// Determines if a type only can represent non-null values.
    pub fn is_non_null(&self) -> bool {
        match *self {
            Type::NonNullNamed(_) | Type::NonNullList(_) => true,
            _ => false,
        }
    }
}

impl fmt::Display for Type {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Type::Named(ref n) => write!(f, "{}", n),
            Type::NonNullNamed(ref n) => write!(f, "{}!", n),
            Type::List(ref t) => write!(f, "[{}]", t),
            Type::NonNullList(ref t) => write!(f, "[{}]!", t),
        }
    }
}

impl InputValue {
    /// Construct a null value.
    pub fn null() -> InputValue { InputValue::Null }

    /// Construct an integer value.
    pub fn int(i: i64) -> InputValue { InputValue::Int(i) }

    /// Construct a floating point value.
    pub fn float(f: f64) -> InputValue { InputValue::Float(f) }

    /// Construct a boolean value.
    pub fn boolean(b: bool) -> InputValue { InputValue::Boolean(b) }

    /// Construct a string value.
    pub fn string<T: AsRef<str>>(s: T) -> InputValue {
        InputValue::String(s.as_ref().to_owned())
    }

    /// Construct an enum value.
    pub fn enum_value<T: AsRef<str>>(s: T) -> InputValue {
        InputValue::Enum(s.as_ref().to_owned())
    }

    /// Construct a variable value.
    pub fn variable<T: AsRef<str>>(v: T) -> InputValue {
        InputValue::Variable(v.as_ref().to_owned())
    }

    /// Construct an unlocated list.
    ///
    /// Convenience function to make each `InputValue` in the input vector
    /// not contain any location information. Can be used from `ToInputValue`
    /// implementations, where no source code position information is available.
    pub fn list(l: Vec<InputValue>) -> InputValue {
        InputValue::List(l.into_iter().map(|i| Spanning::unlocated(i)).collect())
    }

    /// Construct a located list.
    pub fn parsed_list(l: Vec<Spanning<InputValue>>) -> InputValue {
        InputValue::List(l)
    }

    /// Construct an unlocated object.
    ///
    /// Similar to `InputValue::list`, it makes each key and value in the given
    /// hash map not contain any location information.
    pub fn object<K>(o: HashMap<K, InputValue>) -> InputValue
        where K: AsRef<str> + Eq + Hash
    {
        InputValue::Object(
            o.into_iter()
                .map(|(k, v)|
                    (Spanning::unlocated(k.as_ref().to_owned()), Spanning::unlocated(v)))
                .collect()
        )
    }

    /// Construct a located object.
    pub fn parsed_object(o: Vec<(Spanning<String>, Spanning<InputValue>)>) -> InputValue {
        InputValue::Object(o)
    }

    /// Convert a `Json` structure into an `InputValue`.
    ///
    /// This consumes the JSON instance.
    ///
    /// Notes:
    /// * No enums or variables will be produced by this method.
    /// * All lists and objects will be unlocated
    pub fn from_json(json: Json) -> InputValue {
        match json {
            Json::I64(i) => InputValue::int(i),
            Json::U64(u) => InputValue::float(u as f64),
            Json::F64(f) => InputValue::float(f),
            Json::String(s) => InputValue::string(s),
            Json::Boolean(b) => InputValue::boolean(b),
            Json::Array(a) => InputValue::list(a.into_iter().map(InputValue::from_json).collect()),
            Json::Object(o) => InputValue::object(o.into_iter().map(|(k,v)| (k, InputValue::from_json(v))).collect()),
            Json::Null => InputValue::null(),
        }
    }

    /// Resolve all variables to their values.
    pub fn into_const(self, vars: &HashMap<String, InputValue>) -> InputValue {
        match self {
            InputValue::Variable(v) => vars[&v].clone(),
            InputValue::List(l) => InputValue::List(
                l.into_iter().map(|s| s.map(|v| v.into_const(vars))).collect()
            ),
            InputValue::Object(o) => InputValue::Object(
                o.into_iter().map(|(sk, sv)| (sk, sv.map(|v| v.into_const(vars)))).collect()
            ),
            v => v,
        }
    }

    /// Shorthand form of invoking `FromInputValue::from()`.
    pub fn convert<T>(&self) -> Option<T> where T: FromInputValue {
        <T as FromInputValue>::from(self)
    }

    /// Does the value represent null?
    pub fn is_null(&self) -> bool {
        match *self {
            InputValue::Null => true,
            _ => false,
        }
    }

    /// Does the value represent a variable?
    pub fn is_variable(&self) -> bool {
        match *self {
            InputValue::Variable(_) => true,
            _ => false,
        }
    }

    /// View the underlying enum value, if present.
    pub fn as_enum_value(&self) -> Option<&str> {
        match *self {
            InputValue::Enum(ref e) => Some(e),
            _ => None,
        }
    }

    /// View the underlying string value, if present.
    pub fn as_string_value(&self) -> Option<&str> {
        match *self {
            InputValue::String(ref s) => Some(s),
            _ => None,
        }
    }

    /// Convert the input value to an unlocated object value.
    ///
    /// This constructs a new hashmap that contain references to the keys
    /// and values in `self`.
    pub fn to_object_value(&self) -> Option<HashMap<&str, &InputValue>> {
        match *self {
            InputValue::Object(ref o) => Some(
                o.iter().map(|&(ref sk, ref sv)| (sk.item.as_str(), &sv.item)).collect()),
            _ => None,
        }
    }

    /// Convert the input value to an unlocated list value.
    ///
    /// This constructs a new vector that contain references to the values
    /// in `self`.
    pub fn to_list_value(&self) -> Option<Vec<&InputValue>> {
        match *self {
            InputValue::List(ref l) => Some(l.iter().map(|s| &s.item).collect()),
            _ => None,
        }
    }

    /// Recursively find all variables
    pub fn referenced_variables(&self) -> Vec<&str> {
        match *self {
            InputValue::Variable(ref name) => vec![name],
            InputValue::List(ref l) => l.iter().flat_map(|v| v.item.referenced_variables()).collect(),
            InputValue::Object(ref obj) => obj.iter().flat_map(|&(_, ref v)| v.item.referenced_variables()).collect(),
            _ => vec![],
        }
    }

    /// Compare equality with another `InputValue` ignoring any source position information.
    pub fn unlocated_eq(&self, other: &InputValue) -> bool {
        use InputValue::*;

        match (self, other) {
            (&Null, &Null) => true,
            (&Int(i1), &Int(i2)) => i1 == i2,
            (&Float(f1), &Float(f2)) => f1 == f2,
            (&String(ref s1), &String(ref s2)) |
            (&Enum(ref s1), &Enum(ref s2)) |
            (&Variable(ref s1), &Variable(ref s2)) => s1 == s2,
            (&Boolean(b1), &Boolean(b2)) => b1 == b2,
            (&List(ref l1), &List(ref l2)) =>
                l1.iter().zip(l2.iter()).all(|(ref v1, ref v2)| v1.item.unlocated_eq(&v2.item)),
            (&Object(ref o1), &Object(ref o2)) =>
                o1.len() == o2.len()
                && o1.iter()
                    .all(|&(ref sk1, ref sv1)| o2.iter().any(
                        |&(ref sk2, ref sv2)| sk1.item == sk2.item && sv1.item.unlocated_eq(&sv2.item))),
            _ => false
        }
    }
}

impl ToJson for InputValue {
    fn to_json(&self) -> Json {
        match *self {
            InputValue::Null | InputValue::Variable(_) => Json::Null,
            InputValue::Int(i) => Json::I64(i),
            InputValue::Float(f) => Json::F64(f),
            InputValue::String(ref s) | InputValue::Enum(ref s) => Json::String(s.clone()),
            InputValue::Boolean(b) => Json::Boolean(b),
            InputValue::List(ref l) => Json::Array(l.iter().map(|x| x.item.to_json()).collect()),
            InputValue::Object(ref o) => Json::Object(o.iter().map(|&(ref k, ref v)| (k.item.clone(), v.item.to_json())).collect()),
       }
    }
}

impl Arguments {
    pub fn into_iter(self) -> vec::IntoIter<(Spanning<String>, Spanning<InputValue>)> {
        self.items.into_iter()
    }

    pub fn iter(&self) -> slice::Iter<(Spanning<String>, Spanning<InputValue>)> {
        self.items.iter()
    }

    pub fn iter_mut(&mut self) -> slice::IterMut<(Spanning<String>, Spanning<InputValue>)> {
        self.items.iter_mut()
    }

    pub fn drain<'a>(&'a mut self) -> vec::Drain<'a, (Spanning<String>, Spanning<InputValue>)> {
        self.items.drain(..)
    }

    pub fn len(&self) -> usize {
        self.items.len()
    }

    pub fn get(&self, key: &str) -> Option<&Spanning<InputValue>> {
        self.items
            .iter()
            .filter(|&&(ref k, _)| k.item == key)
            .map(|&(_, ref v)| v)
            .next()
    }
}

impl VariableDefinitions {
    pub fn iter(&self) -> slice::Iter<(Spanning<String>, VariableDefinition)> {
        self.items.iter()
    }
}