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
use crate::extensions::ResolveInfo;
use crate::{ContextSelectionSet, Error, ObjectType, QueryError, Result};
use graphql_parser::query::{Selection, TypeCondition};
use std::future::Future;
use std::pin::Pin;

type BoxMutationFuture<'a> = Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>;

#[allow(missing_docs)]
pub async fn do_mutation_resolve<'a, T: ObjectType + Send + Sync>(
    ctx: &'a ContextSelectionSet<'a>,
    root: &'a T,
) -> Result<serde_json::Value> {
    let mut values = serde_json::Map::new();
    do_resolve(ctx, root, &mut values).await?;
    Ok(values.into())
}

fn do_resolve<'a, T: ObjectType + Send + Sync>(
    ctx: &'a ContextSelectionSet<'a>,
    root: &'a T,
    values: &'a mut serde_json::Map<String, serde_json::Value>,
) -> BoxMutationFuture<'a> {
    Box::pin(async move {
        if ctx.items.is_empty() {
            return Err(Error::Query {
                pos: ctx.span.0,
                path: None,
                err: QueryError::MustHaveSubFields {
                    object: T::type_name().to_string(),
                },
            });
        }

        for selection in &ctx.item.items {
            match selection {
                Selection::Field(field) => {
                    if ctx.is_skip(&field.directives)? {
                        continue;
                    }

                    if field.name.as_str() == "__typename" {
                        values.insert(
                            "__typename".to_string(),
                            root.introspection_type_name().to_string().into(),
                        );
                        continue;
                    }

                    let ctx_field = ctx.with_field(field);
                    let field_name = ctx_field.result_name().to_string();
                    let resolve_id = ctx_field.get_resolve_id();

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

                        ctx_field
                            .extensions
                            .iter()
                            .for_each(|e| e.resolve_field_start(&resolve_info));
                    }

                    let value = root.resolve_field(&ctx_field, field).await?;
                    values.insert(field_name, value);

                    if !ctx_field.extensions.is_empty() {
                        ctx_field
                            .extensions
                            .iter()
                            .for_each(|e| e.resolve_field_end(resolve_id));
                    }
                }
                Selection::FragmentSpread(fragment_spread) => {
                    if ctx.is_skip(&fragment_spread.directives)? {
                        continue;
                    }

                    if let Some(fragment) =
                        ctx.fragments.get(fragment_spread.fragment_name.as_str())
                    {
                        do_resolve(
                            &ctx.with_selection_set(&fragment.selection_set),
                            root,
                            values,
                        )
                        .await?;
                    } else {
                        return Err(Error::Query {
                            pos: fragment_spread.position,
                            path: None,
                            err: QueryError::UnknownFragment {
                                name: fragment_spread.fragment_name.clone(),
                            },
                        });
                    }
                }
                Selection::InlineFragment(inline_fragment) => {
                    if ctx.is_skip(&inline_fragment.directives)? {
                        continue;
                    }

                    if let Some(TypeCondition::On(name)) = &inline_fragment.type_condition {
                        let mut futures = Vec::new();
                        root.collect_inline_fields(
                            name,
                            inline_fragment.position,
                            &ctx.with_selection_set(&inline_fragment.selection_set),
                            &mut futures,
                        )?;
                        for fut in futures {
                            let (name, value) = fut.await?;
                            values.insert(name, value);
                        }
                    } else {
                        do_resolve(
                            &ctx.with_selection_set(&inline_fragment.selection_set),
                            root,
                            values,
                        )
                        .await?;
                    }
                }
            }
        }

        Ok(())
    })
}