async-graphql/src/types/query_root.rs

50 lines
1.5 KiB
Rust
Raw Normal View History

2020-03-03 11:15:18 +00:00
use crate::model::__Schema;
use crate::{registry, ContextSelectionSet, GQLOutputValue, GQLType, Result};
2020-03-02 00:24:49 +00:00
use graphql_parser::query::Selection;
use std::borrow::Cow;
2020-03-03 11:15:18 +00:00
pub struct QueryRoot<T> {
pub inner: T,
pub query_type: String,
pub mutation_type: String,
}
2020-03-02 00:24:49 +00:00
2020-03-03 03:48:00 +00:00
impl<T: GQLType> GQLType 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-03 03:48:00 +00:00
T::create_type_info(registry)
2020-03-02 00:24:49 +00:00
}
}
#[async_trait::async_trait]
impl<T: GQLOutputValue + Send + Sync> GQLOutputValue for QueryRoot<T> {
async fn resolve(&self, ctx: &ContextSelectionSet<'_>) -> Result<serde_json::Value> {
2020-03-03 11:15:18 +00:00
let mut res = self.inner.resolve(ctx).await?;
if let serde_json::Value::Object(obj) = &mut res {
2020-03-02 00:24:49 +00:00
for item in &ctx.item.items {
if let Selection::Field(field) = item {
if field.name == "__schema" {
2020-03-03 11:15:18 +00:00
let ctx_obj = ctx.with_item(&field.selection_set);
obj.insert(
"__schema".to_string(),
__Schema {
registry: &ctx.registry,
query_type: &self.query_type,
mutation_type: &self.mutation_type,
}
.resolve(&ctx_obj)
.await?,
);
2020-03-02 00:24:49 +00:00
}
}
}
}
2020-03-03 11:15:18 +00:00
Ok(res)
2020-03-02 00:24:49 +00:00
}
}