async-graphql/src/resolver.rs

157 lines
6.0 KiB
Rust
Raw Normal View History

2020-03-25 03:39:28 +00:00
use crate::base::BoxFieldFuture;
2020-03-26 03:34:28 +00:00
use crate::extensions::ResolveInfo;
use crate::{ContextSelectionSet, Error, ObjectType, QueryError, Result};
2020-03-25 03:39:28 +00:00
use futures::{future, TryFutureExt};
2020-03-07 02:39:55 +00:00
use graphql_parser::query::{Selection, TypeCondition};
2020-03-25 03:39:28 +00:00
use std::iter::FromIterator;
2020-03-06 15:58:43 +00:00
2020-03-25 03:39:28 +00:00
#[allow(missing_docs)]
pub async fn do_resolve<'a, T: ObjectType + Send + Sync>(
2020-03-06 15:58:43 +00:00
ctx: &'a ContextSelectionSet<'a>,
2020-03-25 03:39:28 +00:00
root: &'a T,
) -> Result<serde_json::Value> {
let mut futures = Vec::new();
2020-03-26 10:30:29 +00:00
collect_fields(ctx, root, &mut futures)?;
2020-03-25 03:39:28 +00:00
let res = futures::future::try_join_all(futures).await?;
let map = serde_json::Map::from_iter(res);
Ok(map.into())
2020-03-06 15:58:43 +00:00
}
2020-03-25 03:39:28 +00:00
#[allow(missing_docs)]
pub fn collect_fields<'a, T: ObjectType + Send + Sync>(
2020-03-26 10:30:29 +00:00
ctx: &ContextSelectionSet<'a>,
2020-03-25 03:39:28 +00:00
root: &'a T,
futures: &mut Vec<BoxFieldFuture<'a>>,
) -> Result<()> {
if ctx.items.is_empty() {
return Err(Error::Query {
pos: ctx.span.0,
path: None,
err: QueryError::MustHaveSubFields {
object: T::type_name().to_string(),
},
});
2020-03-25 03:39:28 +00:00
}
2020-03-24 10:54:22 +00:00
2020-03-25 03:39:28 +00:00
for selection in &ctx.item.items {
match selection {
Selection::Field(field) => {
if ctx.is_skip(&field.directives)? {
continue;
}
2020-03-06 15:58:43 +00:00
2020-03-25 03:39:28 +00:00
if field.name.as_str() == "__typename" {
// Get the typename
2020-03-26 10:30:29 +00:00
let ctx_field = ctx.with_field(field);
let field_name = ctx_field.result_name().to_string();
2020-03-25 03:39:28 +00:00
futures.push(Box::pin(
future::ok::<serde_json::Value, Error>(
root.introspection_type_name().to_string().into(),
)
.map_ok(move |value| (field_name, value)),
2020-03-25 03:39:28 +00:00
));
continue;
}
2020-03-07 02:39:55 +00:00
2020-03-25 03:39:28 +00:00
futures.push(Box::pin({
2020-03-26 10:30:29 +00:00
let ctx = ctx.clone();
2020-03-25 03:39:28 +00:00
async move {
2020-03-26 10:30:29 +00:00
let ctx_field = ctx.with_field(field);
let field_name = ctx_field.result_name().to_string();
2020-03-26 03:34:28 +00:00
let resolve_id = ctx_field.get_resolve_id();
if !ctx_field.extensions.is_empty() {
let resolve_info = ResolveInfo {
resolve_id,
2020-03-26 10:30:29 +00:00
path_node: ctx_field.path_node.as_ref().unwrap(),
2020-03-26 03:34:28 +00:00
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(),
},
});
2020-03-26 03:34:28 +00:00
}
},
};
ctx_field
.extensions
.iter()
.for_each(|e| e.resolve_field_start(&resolve_info));
}
let res = root
.resolve_field(&ctx_field, field)
2020-03-25 03:39:28 +00:00
.map_ok(move |value| (field_name, value))
2020-03-26 03:34:28 +00:00
.await?;
if !ctx_field.extensions.is_empty() {
ctx_field
.extensions
.iter()
.for_each(|e| e.resolve_field_end(resolve_id));
}
Ok(res)
2020-03-06 15:58:43 +00:00
}
2020-03-25 03:39:28 +00:00
}))
}
Selection::FragmentSpread(fragment_spread) => {
if ctx.is_skip(&fragment_spread.directives)? {
continue;
}
2020-03-07 02:39:55 +00:00
2020-04-01 08:53:49 +00:00
if let Some(fragment) = ctx.fragments.get(fragment_spread.fragment_name.as_str()) {
2020-03-26 03:34:28 +00:00
collect_fields(
2020-03-26 10:30:29 +00:00
&ctx.with_selection_set(&fragment.selection_set),
2020-03-26 03:34:28 +00:00
root,
futures,
)?;
2020-03-25 03:39:28 +00:00
} else {
return Err(Error::Query {
pos: fragment_spread.position,
path: None,
err: QueryError::UnknownFragment {
name: fragment_spread.fragment_name.clone(),
},
});
2020-03-06 15:58:43 +00:00
}
}
2020-03-25 03:39:28 +00:00
Selection::InlineFragment(inline_fragment) => {
if ctx.is_skip(&inline_fragment.directives)? {
continue;
}
2020-03-06 15:58:43 +00:00
2020-03-25 03:39:28 +00:00
if let Some(TypeCondition::On(name)) = &inline_fragment.type_condition {
root.collect_inline_fields(
name,
inline_fragment.position,
2020-03-26 10:30:29 +00:00
&ctx.with_selection_set(&inline_fragment.selection_set),
2020-03-25 03:39:28 +00:00
futures,
)?;
2020-03-31 07:09:28 +00:00
} else {
collect_fields(
&ctx.with_selection_set(&inline_fragment.selection_set),
root,
futures,
)?;
2020-03-25 03:39:28 +00:00
}
}
}
2020-03-06 15:58:43 +00:00
}
2020-03-07 02:39:55 +00:00
Ok(())
2020-03-06 15:58:43 +00:00
}