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
use crate::extensions::{ErrorLogger, Extension, ResolveInfo};
use crate::parser::types::Selection;
use crate::registry::MetaType;
use crate::{Context, ContextSelectionSet, Error, OutputValueType, QueryError, Result, Value};
use futures::TryFutureExt;
use std::future::Future;
use std::pin::Pin;

/// A GraphQL object.
///
/// This helper trait allows the type to call `resolve_object` on itself in its
/// `OutputValueType::resolve` implementation.
#[async_trait::async_trait]
pub trait ObjectType: OutputValueType {
    /// This function returns true of type `EmptyMutation` only.
    #[doc(hidden)]
    fn is_empty() -> bool {
        false
    }

    /// Resolves a field value and outputs it as a json value `serde_json::Value`.
    async fn resolve_field(&self, ctx: &Context<'_>) -> Result<serde_json::Value>;

    /// Collect all the fields of the object that are queried in the selection set.
    ///
    /// Objects do not have to override this, but interfaces and unions must call it on their
    /// internal type.
    fn collect_all_fields<'a>(
        &'a self,
        ctx: &ContextSelectionSet<'a>,
        fields: &mut Fields<'a>,
    ) -> Result<()>
    where
        Self: Sized + Send + Sync,
    {
        fields.add_set(ctx, self)
    }

    /// Find the GraphQL entity with the given name from the parameter.
    ///
    /// Objects should override this in case they are the query root.
    async fn find_entity(&self, ctx: &Context<'_>, _params: &Value) -> Result<serde_json::Value> {
        Err(QueryError::EntityNotFound.into_error(ctx.item.pos))
    }
}

#[async_trait::async_trait]
impl<T: ObjectType + Send + Sync> ObjectType for &T {
    async fn resolve_field(&self, ctx: &Context<'_>) -> Result<serde_json::Value> {
        T::resolve_field(*self, ctx).await
    }
}

// TODO: reduce code duplication between the two below functions?

/// Resolve an object by executing each of the fields concurrently.
pub async fn resolve_object<'a, T: ObjectType + Send + Sync>(
    ctx: &ContextSelectionSet<'a>,
    root: &'a T,
) -> Result<serde_json::Value> {
    let mut fields = Fields(Vec::new());
    fields.add_set(ctx, root)?;
    let futures = fields.0;

    let res = futures::future::try_join_all(futures).await?;
    let mut map = serde_json::Map::new();
    for (name, value) in res {
        if let serde_json::Value::Object(b) = value {
            if let Some(serde_json::Value::Object(a)) = map.get_mut(&name) {
                a.extend(b);
            } else {
                map.insert(name, b.into());
            }
        } else {
            map.insert(name, value);
        }
    }
    Ok(map.into())
}

/// Resolve an object by executing each of the fields serially.
pub async fn resolve_object_serial<'a, T: ObjectType + Send + Sync>(
    ctx: &ContextSelectionSet<'a>,
    root: &'a T,
) -> Result<serde_json::Value> {
    let mut fields = Fields(Vec::new());
    fields.add_set(ctx, root)?;
    let futures = fields.0;

    let mut map = serde_json::Map::new();
    for field in futures {
        let (name, value) = field.await?;

        if let serde_json::Value::Object(b) = value {
            if let Some(serde_json::Value::Object(a)) = map.get_mut(&name) {
                a.extend(b);
            } else {
                map.insert(name, b.into());
            }
        } else {
            map.insert(name, value);
        }
    }
    Ok(map.into())
}

type BoxFieldFuture<'a> =
    Pin<Box<dyn Future<Output = Result<(String, serde_json::Value)>> + 'a + Send>>;

/// A set of fields on an object that are being selected.
pub struct Fields<'a>(Vec<BoxFieldFuture<'a>>);

impl<'a> Fields<'a> {
    /// Add another set of fields to this set of fields using the given object.
    pub fn add_set<T: ObjectType + Send + Sync>(
        &mut self,
        ctx: &ContextSelectionSet<'a>,
        root: &'a T,
    ) -> Result<()> {
        for selection in &ctx.item.node.items {
            if ctx.is_skip(&selection.node.directives())? {
                continue;
            }

            match &selection.node {
                Selection::Field(field) => {
                    if field.node.name.node == "__typename" {
                        // Get the typename
                        let ctx_field = ctx.with_field(field);
                        let field_name = ctx_field
                            .item
                            .node
                            .response_key()
                            .node
                            .clone()
                            .into_string();
                        let typename = root.introspection_type_name().into_owned();

                        self.0.push(Box::pin(async move {
                            Ok((field_name, serde_json::Value::String(typename)))
                        }));
                        continue;
                    }

                    if ctx.is_ifdef(&field.node.directives) {
                        if let Some(MetaType::Object { fields, .. }) =
                            ctx.schema_env.registry.types.get(T::type_name().as_ref())
                        {
                            if !fields.contains_key(field.node.name.node.as_str()) {
                                continue;
                            }
                        }
                    }

                    self.0.push(Box::pin({
                        // TODO: investigate removing this
                        let ctx = ctx.clone();
                        async move {
                            let ctx_field = ctx.with_field(field);
                            let field_name = ctx_field
                                .item
                                .node
                                .response_key()
                                .node
                                .clone()
                                .into_string();

                            let resolve_info = ResolveInfo {
                                resolve_id: ctx_field.resolve_id,
                                path_node: ctx_field.path_node.as_ref().unwrap(),
                                parent_type: &T::type_name(),
                                return_type: match ctx_field
                                    .schema_env
                                    .registry
                                    .types
                                    .get(T::type_name().as_ref())
                                    .and_then(|ty| ty.field_by_name(field.node.name.node.as_str()))
                                    .map(|field| &field.ty)
                                {
                                    Some(ty) => &ty,
                                    None => {
                                        return Err(Error::Query {
                                            pos: field.pos,
                                            path: None,
                                            err: QueryError::FieldNotFound {
                                                field_name: field
                                                    .node
                                                    .name
                                                    .node
                                                    .clone()
                                                    .into_string(),
                                                object: T::type_name().to_string(),
                                            },
                                        })
                                    }
                                },
                                schema_env: ctx.schema_env,
                                query_env: ctx.query_env,
                            };

                            ctx_field
                                .query_env
                                .extensions
                                .lock()
                                .resolve_start(&resolve_info);

                            let res = root
                                .resolve_field(&ctx_field)
                                .map_ok(move |value| (field_name, value))
                                .await
                                .log_error(&ctx_field.query_env.extensions)?;

                            ctx_field
                                .query_env
                                .extensions
                                .lock()
                                .resolve_end(&resolve_info);
                            Ok(res)
                        }
                    }));
                }
                selection => {
                    let (type_condition, selection_set) = match selection {
                        Selection::Field(_) => unreachable!(),
                        Selection::FragmentSpread(spread) => {
                            let fragment =
                                ctx.query_env.fragments.get(&spread.node.fragment_name.node);
                            let fragment = match fragment {
                                Some(fragment) => fragment,
                                None => {
                                    return Err(Error::Query {
                                        pos: spread.pos,
                                        path: None,
                                        err: QueryError::UnknownFragment {
                                            name: spread.node.fragment_name.to_string(),
                                        },
                                    })
                                }
                            };
                            (
                                Some(&fragment.node.type_condition),
                                &fragment.node.selection_set,
                            )
                        }
                        Selection::InlineFragment(fragment) => (
                            fragment.node.type_condition.as_ref(),
                            &fragment.node.selection_set,
                        ),
                    };
                    let type_condition =
                        type_condition.map(|condition| condition.node.on.node.as_str());

                    let introspection_type_name = root.introspection_type_name();

                    let applies_concrete_object = type_condition.map_or(false, |condition| {
                        introspection_type_name == condition
                            || ctx
                                .schema_env
                                .registry
                                .implements
                                .get(&*introspection_type_name)
                                .map_or(false, |interfaces| interfaces.contains(condition))
                    });
                    if applies_concrete_object {
                        // The fragment applies to the concrete object type.

                        // TODO: This solution isn't ideal. If there are two interfaces InterfaceA
                        // and InterfaceB and one type MyObj that implements both, then if you have
                        // a type condition for `InterfaceA` on an `InterfaceB` and when resolving,
                        // the `InterfaceB` is actually a `MyObj` then the contents of the fragment
                        // will be treated as a `MyObj` rather than an `InterfaceB`. Example:
                        //
                        // myObjAsInterfaceB {
                        //     ... on InterfaceA {
                        //         # here you can query MyObj fields even when you should only be
                        //         # able to query InterfaceA fields.
                        //     }
                        // }
                        root.collect_all_fields(&ctx.with_selection_set(selection_set), self)?;
                    } else if type_condition.map_or(true, |condition| T::type_name() == condition) {
                        // The fragment applies to an interface type.
                        self.add_set(&ctx.with_selection_set(selection_set), root)?;
                    }
                }
            }
        }
        Ok(())
    }
}