2022-04-19 04:25:11 +00:00
|
|
|
use std::{future::Future, pin::Pin, sync::Arc};
|
2020-10-15 06:38:10 +00:00
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
use futures_util::FutureExt;
|
2021-10-26 10:57:39 +00:00
|
|
|
use indexmap::IndexMap;
|
|
|
|
|
2021-11-19 10:49:37 +00:00
|
|
|
use crate::{
|
2022-04-19 04:25:11 +00:00
|
|
|
extensions::ResolveInfo, parser::types::Selection, Context, ContextBase, ContextSelectionSet,
|
|
|
|
Error, Name, OutputType, ServerError, ServerResult, Value,
|
2021-11-19 10:49:37 +00:00
|
|
|
};
|
2020-09-12 09:29:52 +00:00
|
|
|
|
2020-10-13 02:19:30 +00:00
|
|
|
/// Represents a GraphQL container object.
|
2020-09-12 09:29:52 +00:00
|
|
|
///
|
2022-04-19 04:25:11 +00:00
|
|
|
/// This helper trait allows the type to call `resolve_container` on itself in
|
|
|
|
/// its `OutputType::resolve` implementation.
|
2020-09-12 09:29:52 +00:00
|
|
|
#[async_trait::async_trait]
|
2020-12-11 07:37:50 +00:00
|
|
|
pub trait ContainerType: OutputType {
|
2020-09-13 17:52:36 +00:00
|
|
|
/// This function returns true of type `EmptyMutation` only.
|
2020-09-12 09:29:52 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
fn is_empty() -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
/// Resolves a field value and outputs it as a json value
|
|
|
|
/// `async_graphql::Value`.
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
///
|
|
|
|
/// If the field was not found returns None.
|
2020-10-10 02:32:43 +00:00
|
|
|
async fn resolve_field(&self, ctx: &Context<'_>) -> ServerResult<Option<Value>>;
|
2020-09-12 09:29:52 +00:00
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
/// Collect all the fields of the container that are queried in the
|
|
|
|
/// selection set.
|
2020-09-12 09:29:52 +00:00
|
|
|
///
|
2022-04-19 04:25:11 +00:00
|
|
|
/// Objects do not have to override this, but interfaces and unions must
|
|
|
|
/// call it on their internal type.
|
2020-09-12 09:29:52 +00:00
|
|
|
fn collect_all_fields<'a>(
|
|
|
|
&'a self,
|
|
|
|
ctx: &ContextSelectionSet<'a>,
|
|
|
|
fields: &mut Fields<'a>,
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
) -> ServerResult<()>
|
2020-09-12 09:29:52 +00:00
|
|
|
where
|
2021-01-10 03:21:47 +00:00
|
|
|
Self: Send + Sync,
|
2020-09-12 09:29:52 +00:00
|
|
|
{
|
|
|
|
fields.add_set(ctx, self)
|
|
|
|
}
|
|
|
|
|
2020-09-23 18:50:35 +00:00
|
|
|
/// Find the GraphQL entity with the given name from the parameter.
|
|
|
|
///
|
|
|
|
/// Objects should override this in case they are the query root.
|
2020-10-10 02:32:43 +00:00
|
|
|
async fn find_entity(&self, _: &Context<'_>, _params: &Value) -> ServerResult<Option<Value>> {
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
Ok(None)
|
2020-09-12 09:29:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
2022-01-11 01:35:09 +00:00
|
|
|
impl<T: ContainerType + ?Sized> ContainerType for &T {
|
2020-10-10 02:32:43 +00:00
|
|
|
async fn resolve_field(&self, ctx: &Context<'_>) -> ServerResult<Option<Value>> {
|
2020-09-12 09:29:52 +00:00
|
|
|
T::resolve_field(*self, ctx).await
|
|
|
|
}
|
2020-10-20 03:49:31 +00:00
|
|
|
|
|
|
|
async fn find_entity(&self, ctx: &Context<'_>, params: &Value) -> ServerResult<Option<Value>> {
|
|
|
|
T::find_entity(*self, ctx, params).await
|
|
|
|
}
|
2020-09-12 09:29:52 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 01:35:09 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<T: ContainerType + ?Sized> ContainerType for Arc<T> {
|
|
|
|
async fn resolve_field(&self, ctx: &Context<'_>) -> ServerResult<Option<Value>> {
|
|
|
|
T::resolve_field(self, ctx).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn find_entity(&self, ctx: &Context<'_>, params: &Value) -> ServerResult<Option<Value>> {
|
|
|
|
T::find_entity(self, ctx, params).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<T: ContainerType + ?Sized> ContainerType for Box<T> {
|
|
|
|
async fn resolve_field(&self, ctx: &Context<'_>) -> ServerResult<Option<Value>> {
|
|
|
|
T::resolve_field(self, ctx).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn find_entity(&self, ctx: &Context<'_>, params: &Value) -> ServerResult<Option<Value>> {
|
|
|
|
T::find_entity(self, ctx, params).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 02:33:07 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<T: ContainerType, E: Into<Error> + Send + Sync + Clone> ContainerType for Result<T, E> {
|
|
|
|
async fn resolve_field(&self, ctx: &Context<'_>) -> ServerResult<Option<Value>> {
|
|
|
|
match self {
|
|
|
|
Ok(value) => T::resolve_field(value, ctx).await,
|
|
|
|
Err(err) => Err(ctx.set_error_path(err.clone().into().into_server_error(ctx.item.pos))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn find_entity(&self, ctx: &Context<'_>, params: &Value) -> ServerResult<Option<Value>> {
|
|
|
|
match self {
|
|
|
|
Ok(value) => T::find_entity(value, ctx, params).await,
|
|
|
|
Err(err) => Err(ctx.set_error_path(err.clone().into().into_server_error(ctx.item.pos))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 23:45:48 +00:00
|
|
|
/// Resolve an container by executing each of the fields concurrently.
|
2021-01-14 04:41:59 +00:00
|
|
|
pub async fn resolve_container<'a, T: ContainerType + ?Sized>(
|
2020-09-12 09:29:52 +00:00
|
|
|
ctx: &ContextSelectionSet<'a>,
|
|
|
|
root: &'a T,
|
2021-06-07 12:51:20 +00:00
|
|
|
) -> ServerResult<Value> {
|
2020-10-16 06:12:21 +00:00
|
|
|
resolve_container_inner(ctx, root, true).await
|
2020-09-12 09:29:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 23:45:48 +00:00
|
|
|
/// Resolve an container by executing each of the fields serially.
|
2021-01-14 04:41:59 +00:00
|
|
|
pub async fn resolve_container_serial<'a, T: ContainerType + ?Sized>(
|
2020-09-12 09:29:52 +00:00
|
|
|
ctx: &ContextSelectionSet<'a>,
|
|
|
|
root: &'a T,
|
2021-06-07 12:51:20 +00:00
|
|
|
) -> ServerResult<Value> {
|
2020-10-16 06:12:21 +00:00
|
|
|
resolve_container_inner(ctx, root, false).await
|
|
|
|
}
|
|
|
|
|
2021-10-26 10:57:39 +00:00
|
|
|
fn insert_value(target: &mut IndexMap<Name, Value>, name: Name, value: Value) {
|
2020-11-27 02:26:23 +00:00
|
|
|
if let Some(prev_value) = target.get_mut(&name) {
|
|
|
|
if let Value::Object(target_map) = prev_value {
|
|
|
|
if let Value::Object(obj) = value {
|
|
|
|
for (key, value) in obj.into_iter() {
|
|
|
|
insert_value(target_map, key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if let Value::List(target_list) = prev_value {
|
|
|
|
if let Value::List(list) = value {
|
|
|
|
for (idx, value) in list.into_iter().enumerate() {
|
|
|
|
if let Some(Value::Object(target_map)) = target_list.get_mut(idx) {
|
|
|
|
if let Value::Object(obj) = value {
|
|
|
|
for (key, value) in obj.into_iter() {
|
|
|
|
insert_value(target_map, key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
target.insert(name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 04:41:59 +00:00
|
|
|
async fn resolve_container_inner<'a, T: ContainerType + ?Sized>(
|
2020-10-16 06:12:21 +00:00
|
|
|
ctx: &ContextSelectionSet<'a>,
|
|
|
|
root: &'a T,
|
|
|
|
parallel: bool,
|
2021-06-07 12:51:20 +00:00
|
|
|
) -> ServerResult<Value> {
|
2020-09-12 09:29:52 +00:00
|
|
|
let mut fields = Fields(Vec::new());
|
2021-06-07 12:51:20 +00:00
|
|
|
fields.add_set(ctx, root)?;
|
2020-09-12 09:29:52 +00:00
|
|
|
|
2020-10-16 06:12:21 +00:00
|
|
|
let res = if parallel {
|
2021-06-07 12:51:20 +00:00
|
|
|
futures_util::future::try_join_all(fields.0).await?
|
2020-10-16 06:12:21 +00:00
|
|
|
} else {
|
|
|
|
let mut results = Vec::with_capacity(fields.0.len());
|
|
|
|
for field in fields.0 {
|
2021-06-07 12:51:20 +00:00
|
|
|
results.push(field.await?);
|
2020-10-16 06:12:21 +00:00
|
|
|
}
|
|
|
|
results
|
|
|
|
};
|
2020-09-12 09:29:52 +00:00
|
|
|
|
2021-10-26 10:57:39 +00:00
|
|
|
let mut map = IndexMap::new();
|
2020-10-16 06:12:21 +00:00
|
|
|
for (name, value) in res {
|
2020-11-27 02:26:23 +00:00
|
|
|
insert_value(&mut map, name, value);
|
2020-09-12 09:29:52 +00:00
|
|
|
}
|
2021-06-07 12:51:20 +00:00
|
|
|
Ok(Value::Object(map))
|
2020-09-12 09:29:52 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 12:51:20 +00:00
|
|
|
type BoxFieldFuture<'a> = Pin<Box<dyn Future<Output = ServerResult<(Name, Value)>> + 'a + Send>>;
|
2020-09-12 09:29:52 +00:00
|
|
|
|
2020-09-30 03:44:18 +00:00
|
|
|
/// A set of fields on an container that are being selected.
|
2020-09-12 09:29:52 +00:00
|
|
|
pub struct Fields<'a>(Vec<BoxFieldFuture<'a>>);
|
|
|
|
|
|
|
|
impl<'a> Fields<'a> {
|
2022-04-19 04:25:11 +00:00
|
|
|
/// Add another set of fields to this set of fields using the given
|
|
|
|
/// container.
|
2021-01-14 04:41:59 +00:00
|
|
|
pub fn add_set<T: ContainerType + ?Sized>(
|
2020-09-12 09:29:52 +00:00
|
|
|
&mut self,
|
|
|
|
ctx: &ContextSelectionSet<'a>,
|
|
|
|
root: &'a T,
|
Rework errors
This completely overhauls the error system used in async-graphql.
- `Error` has been renamed to `ServerError` and `FieldError` has been
renamed to just `Error`. This is because `FieldError` is by far the most
common error that users will have to use so it makes sense to use the
most obvious error name. Also, the current name didn't make sense as it
was used for things other than field errors, such as the data callback
for websockets.
- `ServerError` has been made completely opaque. Before it was an enum
of all the possible errors, but now it just contains an error message,
the locations, the path and extensions. It is a shame that we lose
information, it makes more sense as _conceptually_ GraphQL does not
provide that information. It also frees us to change the internals of
async-graphql a lot more.
- The path of errors is no longer an opaque JSON value but a regular
type, `Vec<PathSegment>`. The type duplication of `PathSegment` and
`QueryPathSegment` is unfortunate, I plan to work on this in the future.
- Now that `ServerError` is opaque, `RuleError` has been removed from
the public API, making it simpler.
- Additionally `QueryError` has been completely removed. Instead the
error messages are constructed ad-hoc; I took care to never repeat an
error message.
- Instead of constructing field-not-found errors inside the
implementations of field resolvers they now return `Option`s, where a
`None` value is representative of the field not being found.
- As an unfortunate consequence of the last change, self-referential
types based on the output of a subscription resolver can no longer be
created. This does not mean anything for users, but causes lifetime
issues in the implementation of merged objects. I fixed it with a bit of
a hack, but this'll have to be looked into further.
- `InputValueError` now has a generic parameter - it's kind of weird but
it's necessary for ergonomics. It also improves error messages.
- The `ErrorExtensions` trait has been removed. I didn't think the
`extend` method was necessary since `From` impls exist. But the
ergonomics are still there with a new trait `ExtendError`, which
is implemented for both errors and results.
- `Response` now supports serializing multiple errors. This allows for
nice things like having multiple validation errors not be awkwardly
shoved into a single error.
- When an error occurs in execution, data is sent as `null`. This is
slightly more compliant with the spec but the algorithm described in
<https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has
yet to be implemented.
2020-09-29 19:06:44 +00:00
|
|
|
) -> ServerResult<()> {
|
2020-09-12 09:29:52 +00:00
|
|
|
for selection in &ctx.item.node.items {
|
|
|
|
match &selection.node {
|
|
|
|
Selection::Field(field) => {
|
|
|
|
if field.node.name.node == "__typename" {
|
|
|
|
// Get the typename
|
|
|
|
let ctx_field = ctx.with_field(field);
|
2020-10-10 02:32:43 +00:00
|
|
|
let field_name = ctx_field.item.node.response_key().node.clone();
|
2020-09-12 09:29:52 +00:00
|
|
|
let typename = root.introspection_type_name().into_owned();
|
|
|
|
|
2021-06-07 12:51:20 +00:00
|
|
|
self.0.push(Box::pin(async move {
|
|
|
|
Ok((field_name, Value::String(typename)))
|
|
|
|
}));
|
2020-09-12 09:29:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-19 10:49:37 +00:00
|
|
|
let resolve_fut = Box::pin({
|
2020-09-12 09:29:52 +00:00
|
|
|
let ctx = ctx.clone();
|
|
|
|
async move {
|
|
|
|
let ctx_field = ctx.with_field(field);
|
2020-10-10 02:32:43 +00:00
|
|
|
let field_name = ctx_field.item.node.response_key().node.clone();
|
2021-04-04 04:05:54 +00:00
|
|
|
let extensions = &ctx.query_env.extensions;
|
2020-09-12 09:29:52 +00:00
|
|
|
|
2021-11-19 10:49:37 +00:00
|
|
|
if extensions.is_empty() && field.node.directives.is_empty() {
|
2021-06-07 12:51:20 +00:00
|
|
|
Ok((
|
|
|
|
field_name,
|
|
|
|
root.resolve_field(&ctx_field).await?.unwrap_or_default(),
|
|
|
|
))
|
2020-11-07 00:27:50 +00:00
|
|
|
} else {
|
2020-10-24 00:59:35 +00:00
|
|
|
let type_name = T::type_name();
|
|
|
|
let resolve_info = ResolveInfo {
|
|
|
|
path_node: ctx_field.path_node.as_ref().unwrap(),
|
|
|
|
parent_type: &type_name,
|
|
|
|
return_type: match ctx_field
|
|
|
|
.schema_env
|
|
|
|
.registry
|
|
|
|
.types
|
|
|
|
.get(type_name.as_ref())
|
|
|
|
.and_then(|ty| {
|
|
|
|
ty.field_by_name(field.node.name.node.as_str())
|
|
|
|
})
|
|
|
|
.map(|field| &field.ty)
|
|
|
|
{
|
|
|
|
Some(ty) => &ty,
|
|
|
|
None => {
|
2021-06-07 12:51:20 +00:00
|
|
|
return Err(ServerError::new(
|
2021-06-07 06:03:36 +00:00
|
|
|
format!(
|
|
|
|
r#"Cannot query field "{}" on type "{}"."#,
|
|
|
|
field_name, type_name
|
|
|
|
),
|
|
|
|
Some(ctx_field.item.pos),
|
|
|
|
));
|
2020-10-24 00:59:35 +00:00
|
|
|
}
|
|
|
|
},
|
2021-06-29 01:30:26 +00:00
|
|
|
name: field.node.name.node.as_str(),
|
|
|
|
alias: field
|
|
|
|
.node
|
|
|
|
.alias
|
|
|
|
.as_ref()
|
|
|
|
.map(|alias| alias.node.as_str()),
|
2022-10-01 08:17:27 +00:00
|
|
|
is_for_introspection: ctx_field.is_for_introspection,
|
2020-10-24 00:59:35 +00:00
|
|
|
};
|
|
|
|
|
2021-06-07 06:03:36 +00:00
|
|
|
let resolve_fut = root.resolve_field(&ctx_field);
|
2021-11-19 10:49:37 +00:00
|
|
|
|
|
|
|
if field.node.directives.is_empty() {
|
|
|
|
futures_util::pin_mut!(resolve_fut);
|
|
|
|
Ok((
|
|
|
|
field_name,
|
|
|
|
extensions
|
|
|
|
.resolve(resolve_info, &mut resolve_fut)
|
|
|
|
.await?
|
|
|
|
.unwrap_or_default(),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
let mut resolve_fut = resolve_fut.boxed();
|
|
|
|
|
|
|
|
for directive in &field.node.directives {
|
|
|
|
if let Some(directive_factory) = ctx
|
|
|
|
.schema_env
|
|
|
|
.custom_directives
|
|
|
|
.get(directive.node.name.node.as_str())
|
|
|
|
{
|
|
|
|
let ctx_directive = ContextBase {
|
|
|
|
path_node: ctx_field.path_node,
|
2022-10-01 08:17:27 +00:00
|
|
|
is_for_introspection: false,
|
2021-11-19 10:49:37 +00:00
|
|
|
item: directive,
|
|
|
|
schema_env: ctx_field.schema_env,
|
|
|
|
query_env: ctx_field.query_env,
|
|
|
|
};
|
|
|
|
let directive_instance = directive_factory
|
|
|
|
.create(&ctx_directive, &directive.node)?;
|
|
|
|
resolve_fut = Box::pin({
|
|
|
|
let ctx_field = ctx_field.clone();
|
|
|
|
async move {
|
|
|
|
directive_instance
|
|
|
|
.resolve_field(&ctx_field, &mut resolve_fut)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
field_name,
|
|
|
|
extensions
|
|
|
|
.resolve(resolve_info, &mut resolve_fut)
|
|
|
|
.await?
|
|
|
|
.unwrap_or_default(),
|
|
|
|
))
|
|
|
|
}
|
2021-04-04 04:05:54 +00:00
|
|
|
}
|
2020-09-12 09:29:52 +00:00
|
|
|
}
|
2021-11-19 10:49:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
self.0.push(resolve_fut);
|
2020-09-12 09:29:52 +00:00
|
|
|
}
|
|
|
|
selection => {
|
|
|
|
let (type_condition, selection_set) = match selection {
|
|
|
|
Selection::Field(_) => unreachable!(),
|
|
|
|
Selection::FragmentSpread(spread) => {
|
2020-09-22 18:59:48 +00:00
|
|
|
let fragment =
|
|
|
|
ctx.query_env.fragments.get(&spread.node.fragment_name.node);
|
2020-09-12 09:29:52 +00:00
|
|
|
let fragment = match fragment {
|
|
|
|
Some(fragment) => fragment,
|
|
|
|
None => {
|
2021-06-07 06:03:36 +00:00
|
|
|
return Err(ServerError::new(
|
|
|
|
format!(
|
|
|
|
r#"Unknown fragment "{}"."#,
|
|
|
|
spread.node.fragment_name.node
|
|
|
|
),
|
|
|
|
Some(spread.pos),
|
|
|
|
));
|
2020-09-12 09:29:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
(
|
|
|
|
Some(&fragment.node.type_condition),
|
|
|
|
&fragment.node.selection_set,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Selection::InlineFragment(fragment) => (
|
|
|
|
fragment.node.type_condition.as_ref(),
|
|
|
|
&fragment.node.selection_set,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
let type_condition =
|
|
|
|
type_condition.map(|condition| condition.node.on.node.as_str());
|
|
|
|
|
|
|
|
let introspection_type_name = root.introspection_type_name();
|
|
|
|
|
2020-09-12 16:07:46 +00:00
|
|
|
let applies_concrete_object = type_condition.map_or(false, |condition| {
|
2020-09-12 09:29:52 +00:00
|
|
|
introspection_type_name == condition
|
|
|
|
|| ctx
|
|
|
|
.schema_env
|
|
|
|
.registry
|
|
|
|
.implements
|
|
|
|
.get(&*introspection_type_name)
|
|
|
|
.map_or(false, |interfaces| interfaces.contains(condition))
|
2020-09-12 16:07:46 +00:00
|
|
|
});
|
|
|
|
if applies_concrete_object {
|
2020-09-12 09:29:52 +00:00
|
|
|
root.collect_all_fields(&ctx.with_selection_set(selection_set), self)?;
|
|
|
|
} else if type_condition.map_or(true, |condition| T::type_name() == condition) {
|
|
|
|
// The fragment applies to an interface type.
|
|
|
|
self.add_set(&ctx.with_selection_set(selection_set), root)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|