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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use std::collections::HashMap;
use std::marker::PhantomData;

use ::GraphQLError;
use ast::{InputValue, ToInputValue, Document, Selection, Fragment, Definition, Type, FromInputValue, OperationType};
use value::Value;
use parser::SourcePosition;

use schema::meta::{MetaType, ScalarMeta, ListMeta, NullableMeta,
                   ObjectMeta, EnumMeta, InterfaceMeta, UnionMeta,
                   InputObjectMeta, PlaceholderMeta, Field, Argument,
                   EnumValue};
use schema::model::{RootNode, SchemaType};

use types::base::GraphQLType;

/// A type registry used to build schemas
///
/// 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<CtxT> {
    /// Currently registered types
    pub types: HashMap<String, MetaType>,
    phantom: PhantomData<CtxT>,
}

#[derive(Clone)]
pub enum FieldPath<'a> {
    Root(SourcePosition),
    Field(String, SourcePosition, &'a FieldPath<'a>),
}

/// Query execution engine
///
/// The executor helps drive the query execution in a schema. It keeps track
/// of the current field stack, context, variables, and errors.
pub struct Executor<'a, CtxT> where CtxT: 'a {
    fragments: &'a HashMap<String, Fragment>,
    variables: &'a HashMap<String, InputValue>,
    current_selection_set: Option<Vec<Selection>>,
    schema: &'a SchemaType,
    context: &'a CtxT,
    errors: &'a mut Vec<ExecutionError>,
    field_path: FieldPath<'a>,
}

/// Error type for errors that occur during query execution
///
/// All execution errors contain the source position in the query of the field
/// that failed to resolve. It also contains the field stack.
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq)]
pub struct ExecutionError {
    location: SourcePosition,
    path: Vec<String>,
    message: String,
}

/// The result of resolving the value of a field of type `T`
pub type FieldResult<T> = Result<T, String>;

/// The result of resolving an unspecified field
pub type ExecutionResult = Result<Value, String>;

/// Convert a value into a successful field result
///
/// Used by the helper macros to support both returning a naked value
/// *and* a `FieldResult` from a field.
pub trait IntoFieldResult<T>: Sized {
    /// Wrap `self` in a `Result`
    ///
    /// The implementation of this should always be `Ok(self)`.
    fn into(self) -> FieldResult<T>;
}

impl<T> IntoFieldResult<T> for FieldResult<T> {
    fn into(self) -> FieldResult<T> {
        self
    }
}

impl<'a, CtxT> Executor<'a, CtxT> {
    /// Resolve a single arbitrary value into an `ExecutionResult`
    pub fn resolve<T: GraphQLType<CtxT>>(&mut self, value: &T) -> ExecutionResult {
        Ok(value.resolve(
            match self.current_selection_set {
                Some(ref sel) => Some(sel.clone()),
                None => None,
            },
            self))
    }

    /// Resolve a single arbitrary value into a return value
    ///
    /// If the field fails to resolve, `null` will be returned.
    pub fn resolve_into_value<T: GraphQLType<CtxT>>(&mut self, value: &T) -> Value {
        match self.resolve(value) {
            Ok(v) => v,
            Err(e) => {
                let position = self.field_path.location().clone();
                self.push_error(e, position);
                Value::null()
            },
        }
    }

    /// Derive a new executor by replacing the context
    ///
    /// This can be used to connect different types, e.g. from different Rust
    /// libraries, that require different context types.
    pub fn replaced_context<'b, NewCtxT>(&'b mut self, ctx: &'b NewCtxT) -> Executor<'b, NewCtxT> {
        Executor {
            fragments: self.fragments,
            variables: self.variables,
            current_selection_set: self.current_selection_set.clone(),
            schema: self.schema,
            context: ctx,
            errors: self.errors,
            field_path: self.field_path.clone(),
        }
    }

    #[doc(hidden)]
    pub fn sub_executor(
        &mut self,
        field_name: Option<String>,
        location: SourcePosition,
        selection_set: Option<Vec<Selection>>,
    )
        -> Executor<CtxT>
    {
        Executor {
            fragments: self.fragments,
            variables: self.variables,
            current_selection_set: selection_set,
            schema: self.schema,
            context: self.context,
            errors: self.errors,
            field_path: match field_name {
                Some(name) => FieldPath::Field(name, location, &self.field_path),
                None => self.field_path.clone(),
            },
        }
    }

    /// Access the current context
    ///
    /// You usually provide the context when calling the top-level `execute`
    /// function, or using the context factory in the Iron integration.
    pub fn context(&self) -> &'a CtxT {
        self.context
    }

    /// The currently executing schema
    pub fn schema(&self) -> &'a SchemaType {
        self.schema
    }

    #[doc(hidden)]
    pub fn variables(&self) -> &'a HashMap<String, InputValue> {
        self.variables
    }

    #[doc(hidden)]
    pub fn fragment_by_name(&self, name: &str) -> Option<&'a Fragment> {
        self.fragments.get(name)
    }

    /// Add an error to the execution engine
    pub fn push_error(&mut self, error: String, location: SourcePosition) {
        let mut path = Vec::new();
        self.field_path.construct_path(&mut path);

        self.errors.push(ExecutionError {
            location: location,
            path: path,
            message: error,
        });
    }
}

impl<'a> FieldPath<'a> {
    fn construct_path(&self, acc: &mut Vec<String>) {
        match *self {
            FieldPath::Root(_) => (),
            FieldPath::Field(ref name, _, ref parent) => {
                parent.construct_path(acc);
                acc.push(name.clone());
            }
        }
    }

    fn location(&self) -> &SourcePosition {
        match *self {
            FieldPath::Root(ref pos) |
            FieldPath::Field(_, ref pos, _) => pos
        }
    }
}

impl ExecutionError {
    #[doc(hidden)]
    pub fn new(location: SourcePosition, path: &[&str], message: &str) -> ExecutionError {
        ExecutionError {
            location: location,
            path: path.iter().map(|s| (*s).to_owned()).collect(),
            message: message.to_owned(),
        }
    }

    /// The error message
    pub fn message(&self) -> &str {
        &self.message
    }

    /// The source location _in the query_ of the field that failed to resolve
    pub fn location(&self) -> &SourcePosition {
        &self.location
    }

    /// The path of fields leading to the field that generated this error
    pub fn path(&self) -> &[String] {
        &self.path
    }
}

pub fn execute_validated_query<'a, QueryT, MutationT, CtxT>(
    document: Document,
    operation_name: Option<&str>,
    root_node: &RootNode<CtxT, QueryT, MutationT>,
    variables: &HashMap<String, InputValue>,
    context: &CtxT
)
    -> Result<(Value, Vec<ExecutionError>), GraphQLError<'a>>
    where QueryT: GraphQLType<CtxT>,
          MutationT: GraphQLType<CtxT>
{
    let mut fragments = vec![];
    let mut operation = None;

    for def in document {
        match def {
            Definition::Operation(op) => {
                if operation_name.is_none() && operation.is_some() {
                    return Err(GraphQLError::MultipleOperationsProvided);
                }

                let move_op = operation_name.is_none()
                    || op.item.name.as_ref().map(|s| s.item.as_ref()) == operation_name;

                if move_op {
                    operation = Some(op);
                }
            }
            Definition::Fragment(f) => fragments.push(f),
        };
    }

    let op = match operation {
        Some(op) => op,
        None => return Err(GraphQLError::UnknownOperationName),
    };

    let mut errors = Vec::new();
    let value;

    {
        let mut executor = Executor {
            fragments: &fragments.into_iter().map(|f| (f.item.name.item.clone(), f.item)).collect(),
            variables: variables,
            current_selection_set: Some(op.item.selection_set),
            schema: &root_node.schema,
            context: context,
            errors: &mut errors,
            field_path: FieldPath::Root(op.start),
        };

        value = match op.item.operation_type {
            OperationType::Query => executor.resolve_into_value(&root_node),
            OperationType::Mutation => executor.resolve_into_value(&root_node.mutation_type),
        };
    }

    errors.sort();

    Ok((value, errors))
}

impl<CtxT> Registry<CtxT> {
    /// Construct a new registry
    pub fn new(types: HashMap<String, MetaType>) -> Registry<CtxT> {
        Registry {
            types: types,
            phantom: PhantomData,
        }
    }

    /// Get the `Type` instance for a given GraphQL type
    ///
    /// If the registry hasn't seen a type with this name before, it will
    /// construct its metadata and store it.
    pub fn get_type<T>(&mut self) -> Type where T: GraphQLType<CtxT> {
        if let Some(name) = T::name() {
            if !self.types.contains_key(name) {
                self.insert_placeholder(name, Type::NonNullNamed(name.to_owned()));
                let meta = T::meta(self);
                self.types.insert(name.to_owned(), meta);
            }
            self.types[name].as_type()
        }
        else {
            T::meta(self).as_type()
        }
    }

    /// Create a field with the provided name
    pub fn field<T>(&mut self, name: &str) -> Field where T: GraphQLType<CtxT> {
        Field {
            name: name.to_owned(),
            description: None,
            arguments: None,
            field_type: self.get_type::<T>(),
            deprecation_reason: None,
        }
    }

    #[doc(hidden)]
    pub fn field_convert<T: IntoFieldResult<I>, I>(&mut self, name: &str) -> Field where I: GraphQLType<CtxT> {
        Field {
            name: name.to_owned(),
            description: None,
            arguments: None,
            field_type: self.get_type::<I>(),
            deprecation_reason: None,
        }
    }

    #[doc(hidden)]
    pub fn field_inside_result<T>(&mut self, name: &str, _: FieldResult<T>) -> Field where T: GraphQLType<CtxT> {
        Field {
            name: name.to_owned(),
            description: None,
            arguments: None,
            field_type: self.get_type::<T>(),
            deprecation_reason: None,
        }
    }

    /// Create an argument with the provided name
    pub fn arg<T>(&mut self, name: &str) -> Argument where T: GraphQLType<CtxT> + FromInputValue {
        Argument::new(name, self.get_type::<T>())
    }

    /// Create an argument with a default value
    ///
    /// When called with type `T`, the actual argument will be given the type
    /// `Option<T>`.
    pub fn arg_with_default<T>(
        &mut self,
        name: &str,
        value: &T,
    )
        -> Argument
        where T: GraphQLType<CtxT> + ToInputValue + FromInputValue
    {
        Argument::new(name, self.get_type::<Option<T>>())
            .default_value(value.to())
    }

    fn insert_placeholder(&mut self, name: &str, of_type: Type) {
        if !self.types.contains_key(name) {
            self.types.insert(
                name.to_owned(),
                MetaType::Placeholder(PlaceholderMeta { of_type: of_type }));
        }
    }

    /// Create a scalar meta type
    ///
    /// This expects the type to implement `FromInputValue`.
    pub fn build_scalar_type<T>(&mut self)
        -> ScalarMeta
        where T: FromInputValue + GraphQLType<CtxT>
    {
        let name = T::name().expect("Scalar types must be named. Implement name()");
        ScalarMeta::new::<T>(name)
    }

    /// Create a list meta type
    pub fn build_list_type<T: GraphQLType<CtxT>>(&mut self) -> ListMeta {
        let of_type = self.get_type::<T>();
        ListMeta::new(of_type)
    }

    /// Create a nullable meta type
    pub fn build_nullable_type<T: GraphQLType<CtxT>>(&mut self) -> NullableMeta {
        let of_type = self.get_type::<T>();
        NullableMeta::new(of_type)
    }

    /// Create an object meta type builder
    ///
    /// 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<T>(&mut self)
        -> Box<Fn(&[Field]) -> ObjectMeta>
        where T: GraphQLType<CtxT>
    {
        let name = T::name().expect("Object types must be named. Implement name()");
        let typename_field = self.field::<String>("__typename");

        Box::new(move |fs: &[Field]| {
            let mut v = fs.to_vec();
            v.push(typename_field.clone());
            ObjectMeta::new(name, &v)
        })
    }

    /// Create an enum meta type
    pub fn build_enum_type<T>(&mut self)
        -> Box<Fn(&[EnumValue]) -> EnumMeta>
        where T: FromInputValue + GraphQLType<CtxT>
    {
        let name = T::name().expect("Enum types must be named. Implement name()");

        Box::new(move |values: &[EnumValue]| EnumMeta::new::<T>(name, values))
    }

    /// Create an interface meta type builder
    pub fn build_interface_type<T>(&mut self)
        -> Box<Fn(&[Field]) -> InterfaceMeta>
        where T: GraphQLType<CtxT>
    {
        let name = T::name().expect("Interface types must be named. Implement name()");
        let typename_field = self.field::<String>("__typename");

        Box::new(move |fs: &[Field]| {
            let mut v = fs.to_vec();
            v.push(typename_field.clone());
            InterfaceMeta::new(name, &v)
        })
    }

    /// Create a union meta type builder
    pub fn build_union_type<T>(&mut self)
        -> Box<Fn(&[Type]) -> UnionMeta>
        where T: GraphQLType<CtxT>
    {
        let name = T::name().expect("Union types must be named. Implement name()");

        Box::new(move |ts: &[Type]| UnionMeta::new(name, ts))
    }

    /// Create an input object meta type builder
    pub fn build_input_object_type<T>(&mut self)
        -> Box<Fn(&[Argument]) -> InputObjectMeta>
        where T: FromInputValue + GraphQLType<CtxT>
    {
        let name = T::name().expect("Input object types must be named. Implement name()");

        Box::new(move |args: &[Argument]| InputObjectMeta::new::<T>(name, args))
    }
}