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
use crate::model::{__Schema, __Type};
use crate::scalars::Any;
use crate::{
    do_resolve, registry, Context, ContextSelectionSet, Error, ObjectType, OutputValueType,
    Positioned, QueryError, Result, Type, Value,
};
use async_graphql_derive::SimpleObject;
use async_graphql_parser::query::Field;
use indexmap::map::IndexMap;
use std::borrow::Cow;

/// Federation service
#[SimpleObject(internal)]
struct Service {
    sdl: Option<String>,
}

pub struct QueryRoot<T> {
    pub inner: T,
    pub disable_introspection: bool,
}

impl<T: Type> Type for QueryRoot<T> {
    fn type_name() -> Cow<'static, str> {
        T::type_name()
    }

    fn create_type_info(registry: &mut registry::Registry) -> String {
        let schema_type = __Schema::create_type_info(registry);
        let root = T::create_type_info(registry);
        if let Some(registry::MetaType::Object { fields, .. }) =
            registry.types.get_mut(T::type_name().as_ref())
        {
            fields.insert(
                "__schema".to_string(),
                registry::MetaField {
                    name: "__schema".to_string(),
                    description: Some("Access the current type schema of this server."),
                    args: Default::default(),
                    ty: schema_type,
                    deprecation: None,
                    cache_control: Default::default(),
                    external: false,
                    requires: None,
                    provides: None,
                },
            );

            fields.insert(
                "__type".to_string(),
                registry::MetaField {
                    name: "__type".to_string(),
                    description: Some("Request the type information of a single type."),
                    args: {
                        let mut args = IndexMap::new();
                        args.insert(
                            "name",
                            registry::MetaInputValue {
                                name: "name",
                                description: None,
                                ty: "String!".to_string(),
                                default_value: None,
                                validator: None,
                            },
                        );
                        args
                    },
                    ty: "__Type".to_string(),
                    deprecation: None,
                    cache_control: Default::default(),
                    external: false,
                    requires: None,
                    provides: None,
                },
            );
        }
        root
    }
}

#[async_trait::async_trait]
impl<T: ObjectType + Send + Sync> ObjectType for QueryRoot<T> {
    async fn resolve_field(&self, ctx: &Context<'_>) -> Result<serde_json::Value> {
        if ctx.name.node == "__schema" {
            if self.disable_introspection {
                return Err(Error::Query {
                    pos: ctx.position(),
                    path: Some(ctx.path_node.as_ref().unwrap().to_json().into()),
                    err: QueryError::FieldNotFound {
                        field_name: ctx.name.to_string(),
                        object: Self::type_name().to_string(),
                    },
                });
            }

            let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
            return OutputValueType::resolve(
                &__Schema {
                    registry: &ctx.schema_env.registry,
                },
                &ctx_obj,
                ctx.item,
            )
            .await;
        } else if ctx.name.node == "__type" {
            let type_name: String = ctx.param_value("name", || Value::Null)?;
            let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
            return OutputValueType::resolve(
                &ctx.schema_env
                    .registry
                    .types
                    .get(&type_name)
                    .map(|ty| __Type::new_simple(&ctx.schema_env.registry, ty)),
                &ctx_obj,
                ctx.item,
            )
            .await;
        } else if ctx.name.node == "_entities" {
            let representations: Vec<Any> = ctx.param_value("representations", || Value::Null)?;
            let mut res = Vec::new();
            for item in representations {
                res.push(self.inner.find_entity(ctx, &item.0).await?);
            }
            return Ok(res.into());
        } else if ctx.name.node == "_service" {
            let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
            return OutputValueType::resolve(
                &Service {
                    sdl: Some(ctx.schema_env.registry.create_federation_sdl()),
                },
                &ctx_obj,
                ctx.item,
            )
            .await;
        }

        self.inner.resolve_field(ctx).await
    }
}

#[async_trait::async_trait]
impl<T: ObjectType + Send + Sync> OutputValueType for QueryRoot<T> {
    async fn resolve(
        &self,
        ctx: &ContextSelectionSet<'_>,
        _field: &Positioned<Field>,
    ) -> Result<serde_json::Value> {
        do_resolve(ctx, self).await
    }
}