2020-03-06 15:58:43 +00:00
|
|
|
use crate::model::{__Schema, __Type};
|
|
|
|
use crate::{
|
2020-03-25 03:39:28 +00:00
|
|
|
do_resolve, registry, Context, ContextSelectionSet, ErrorWithPosition, ObjectType,
|
|
|
|
OutputValueType, Result, Type, Value,
|
2020-03-06 15:58:43 +00:00
|
|
|
};
|
|
|
|
use graphql_parser::query::Field;
|
2020-03-02 00:24:49 +00:00
|
|
|
use std::borrow::Cow;
|
2020-03-08 12:35:36 +00:00
|
|
|
use std::collections::HashMap;
|
2020-03-02 00:24:49 +00:00
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
pub struct QueryRoot<T> {
|
|
|
|
pub inner: T,
|
|
|
|
}
|
2020-03-02 00:24:49 +00:00
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
impl<T: Type> Type for QueryRoot<T> {
|
2020-03-02 00:24:49 +00:00
|
|
|
fn type_name() -> Cow<'static, str> {
|
2020-03-03 03:48:00 +00:00
|
|
|
T::type_name()
|
|
|
|
}
|
|
|
|
|
2020-03-03 11:15:18 +00:00
|
|
|
fn create_type_info(registry: &mut registry::Registry) -> String {
|
2020-03-08 12:35:36 +00:00
|
|
|
let schema_type = __Schema::create_type_info(registry);
|
|
|
|
let root = T::create_type_info(registry);
|
2020-03-19 09:20:12 +00:00
|
|
|
if let Some(registry::Type::Object { fields, .. }) =
|
|
|
|
registry.types.get_mut(T::type_name().as_ref())
|
|
|
|
{
|
2020-03-08 12:35:36 +00:00
|
|
|
fields.insert(
|
2020-03-19 09:20:12 +00:00
|
|
|
"__schema".to_string(),
|
2020-03-08 12:35:36 +00:00
|
|
|
registry::Field {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: "__schema".to_string(),
|
2020-03-08 12:35:36 +00:00
|
|
|
description: Some("Access the current type schema of this server."),
|
|
|
|
args: Default::default(),
|
|
|
|
ty: schema_type,
|
|
|
|
deprecation: None,
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: Default::default(),
|
2020-03-08 12:35:36 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
fields.insert(
|
2020-03-19 09:20:12 +00:00
|
|
|
"__type".to_string(),
|
2020-03-08 12:35:36 +00:00
|
|
|
registry::Field {
|
2020-03-19 09:20:12 +00:00
|
|
|
name: "__type".to_string(),
|
2020-03-08 12:35:36 +00:00
|
|
|
description: Some("Request the type information of a single type."),
|
|
|
|
args: {
|
|
|
|
let mut args = HashMap::new();
|
|
|
|
args.insert(
|
|
|
|
"name",
|
|
|
|
registry::InputValue {
|
|
|
|
name: "name",
|
|
|
|
description: None,
|
|
|
|
ty: "String!".to_string(),
|
|
|
|
default_value: None,
|
2020-03-22 01:34:32 +00:00
|
|
|
validator: None,
|
2020-03-08 12:35:36 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
args
|
|
|
|
},
|
|
|
|
ty: "__Type".to_string(),
|
|
|
|
deprecation: None,
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: Default::default(),
|
2020-03-08 12:35:36 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
root
|
2020-03-02 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
2020-03-19 09:20:12 +00:00
|
|
|
impl<T: ObjectType + Send + Sync> ObjectType for QueryRoot<T> {
|
2020-03-25 03:39:28 +00:00
|
|
|
async fn resolve_field(&self, ctx: &Context<'_>, field: &Field) -> Result<serde_json::Value> {
|
2020-03-06 15:58:43 +00:00
|
|
|
if field.name.as_str() == "__schema" {
|
|
|
|
let ctx_obj = ctx.with_item(&field.selection_set);
|
2020-03-19 09:20:12 +00:00
|
|
|
return OutputValueType::resolve(
|
2020-03-06 15:58:43 +00:00
|
|
|
&__Schema {
|
|
|
|
registry: &ctx.registry,
|
|
|
|
},
|
|
|
|
&ctx_obj,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.map_err(|err| err.with_position(field.position).into());
|
|
|
|
} else if field.name.as_str() == "__type" {
|
|
|
|
let type_name: String = ctx.param_value("name", || Value::Null)?;
|
|
|
|
let ctx_obj = ctx.with_item(&field.selection_set);
|
2020-03-19 09:20:12 +00:00
|
|
|
return OutputValueType::resolve(
|
2020-03-06 15:58:43 +00:00
|
|
|
&ctx.registry
|
|
|
|
.types
|
|
|
|
.get(&type_name)
|
|
|
|
.map(|ty| __Type::new_simple(ctx.registry, ty)),
|
|
|
|
&ctx_obj,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.map_err(|err| err.with_position(field.position).into());
|
2020-03-02 00:24:49 +00:00
|
|
|
}
|
2020-03-03 11:15:18 +00:00
|
|
|
|
2020-03-25 03:39:28 +00:00
|
|
|
self.inner.resolve_field(ctx, field).await
|
2020-03-07 02:39:55 +00:00
|
|
|
}
|
2020-03-02 00:24:49 +00:00
|
|
|
}
|
2020-03-19 09:20:12 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<T: ObjectType + Send + Sync> OutputValueType for QueryRoot<T> {
|
2020-03-25 03:39:28 +00:00
|
|
|
async fn resolve(value: &Self, ctx: &ContextSelectionSet<'_>) -> Result<serde_json::Value> {
|
|
|
|
do_resolve(ctx, value).await
|
2020-03-19 09:20:12 +00:00
|
|
|
}
|
|
|
|
}
|