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
use std::{
    collections::HashMap,
    sync::{Arc, RwLock},
};

use crate::{
    ast::Fragment,
    executor::FieldPath,
    parser::SourcePosition,
    schema::model::{SchemaType, TypeType},
    ExecutionError, Executor, Selection, Variables,
};

/// [`Executor`] owning all its variables. Can be used after [`Executor`] was
/// destroyed.
pub struct OwnedExecutor<'a, CtxT, S> {
    pub(super) fragments: HashMap<&'a str, Fragment<'a, S>>,
    pub(super) variables: Variables<S>,
    pub(super) current_selection_set: Option<Vec<Selection<'a, S>>>,
    pub(super) parent_selection_set: Option<Vec<Selection<'a, S>>>,
    pub(super) current_type: TypeType<'a, S>,
    pub(super) schema: &'a SchemaType<'a, S>,
    pub(super) context: &'a CtxT,
    pub(super) errors: RwLock<Vec<ExecutionError<S>>>,
    pub(super) field_path: Arc<FieldPath<'a>>,
}

impl<'a, CtxT, S> Clone for OwnedExecutor<'a, CtxT, S>
where
    S: Clone,
{
    fn clone(&self) -> Self {
        Self {
            fragments: self.fragments.clone(),
            variables: self.variables.clone(),
            current_selection_set: self.current_selection_set.clone(),
            parent_selection_set: self.parent_selection_set.clone(),
            current_type: self.current_type.clone(),
            schema: self.schema,
            context: self.context,
            errors: RwLock::new(vec![]),
            field_path: self.field_path.clone(),
        }
    }
}

impl<'a, CtxT, S> OwnedExecutor<'a, CtxT, S>
where
    S: Clone,
{
    #[doc(hidden)]
    pub fn type_sub_executor(
        &self,
        type_name: Option<&str>,
        selection_set: Option<Vec<Selection<'a, S>>>,
    ) -> OwnedExecutor<'a, CtxT, S> {
        OwnedExecutor {
            fragments: self.fragments.clone(),
            variables: self.variables.clone(),
            current_selection_set: selection_set,
            parent_selection_set: self.current_selection_set.clone(),
            current_type: match type_name {
                Some(type_name) => self.schema.type_by_name(type_name).expect("Type not found"),
                None => self.current_type.clone(),
            },
            schema: self.schema,
            context: self.context,
            errors: RwLock::new(vec![]),
            field_path: self.field_path.clone(),
        }
    }

    #[doc(hidden)]
    pub fn variables(&self) -> Variables<S> {
        self.variables.clone()
    }

    #[doc(hidden)]
    pub fn field_sub_executor(
        &self,
        field_alias: &'a str,
        field_name: &'a str,
        location: SourcePosition,
        selection_set: Option<Vec<Selection<'a, S>>>,
    ) -> OwnedExecutor<'a, CtxT, S> {
        OwnedExecutor {
            fragments: self.fragments.clone(),
            variables: self.variables.clone(),
            current_selection_set: selection_set,
            parent_selection_set: self.current_selection_set.clone(),
            current_type: self.schema.make_type(
                &self
                    .current_type
                    .innermost_concrete()
                    .field_by_name(field_name)
                    .expect("Field not found on inner type")
                    .field_type,
            ),
            schema: self.schema,
            context: self.context,
            errors: RwLock::new(vec![]),
            field_path: Arc::new(FieldPath::Field(
                field_alias,
                location,
                Arc::clone(&self.field_path),
            )),
        }
    }

    #[doc(hidden)]
    pub fn as_executor(&self) -> Executor<'_, '_, CtxT, S> {
        Executor {
            fragments: &self.fragments,
            variables: &self.variables,
            current_selection_set: self.current_selection_set.as_deref(),
            parent_selection_set: self.parent_selection_set.as_deref(),
            current_type: self.current_type.clone(),
            schema: self.schema,
            context: self.context,
            errors: &self.errors,
            field_path: Arc::clone(&self.field_path),
        }
    }
}

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

    #[doc(hidden)]
    pub fn context(&self) -> &'a CtxT {
        self.context
    }

    #[doc(hidden)]
    pub fn schema(&self) -> &'a SchemaType<S> {
        self.schema
    }

    #[doc(hidden)]
    pub fn location(&self) -> &SourcePosition {
        self.field_path.location()
    }
}