2020-03-26 03:34:28 +00:00
|
|
|
//! Extensions for schema
|
|
|
|
|
2020-12-18 15:58:03 +00:00
|
|
|
mod analyzer;
|
2020-09-30 03:37:12 +00:00
|
|
|
#[cfg(feature = "apollo_persisted_queries")]
|
|
|
|
pub mod apollo_persisted_queries;
|
2020-09-14 18:38:41 +00:00
|
|
|
#[cfg(feature = "apollo_tracing")]
|
2020-04-28 07:01:19 +00:00
|
|
|
mod apollo_tracing;
|
2020-09-14 18:38:41 +00:00
|
|
|
#[cfg(feature = "log")]
|
2020-05-22 03:58:49 +00:00
|
|
|
mod logger;
|
2021-03-20 11:42:00 +00:00
|
|
|
#[cfg(feature = "opentelemetry")]
|
|
|
|
mod opentelemetry;
|
2020-09-14 18:38:41 +00:00
|
|
|
#[cfg(feature = "tracing")]
|
2020-03-26 03:34:28 +00:00
|
|
|
mod tracing;
|
|
|
|
|
2020-10-15 06:38:10 +00:00
|
|
|
use std::any::{Any, TypeId};
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
2020-04-28 07:01:19 +00:00
|
|
|
use crate::context::{QueryPathNode, ResolveId};
|
2020-10-15 06:38:10 +00:00
|
|
|
use crate::parser::types::ExecutableDocument;
|
2020-12-18 15:58:03 +00:00
|
|
|
use crate::{Data, Request, Result, ServerError, ServerResult, ValidationResult, Variables};
|
2020-10-15 06:38:10 +00:00
|
|
|
use crate::{Error, Name, Value};
|
2020-05-22 03:58:49 +00:00
|
|
|
|
2020-12-18 15:58:03 +00:00
|
|
|
pub use self::analyzer::Analyzer;
|
2020-09-14 18:38:41 +00:00
|
|
|
#[cfg(feature = "apollo_tracing")]
|
2020-05-22 03:58:49 +00:00
|
|
|
pub use self::apollo_tracing::ApolloTracing;
|
2020-09-14 18:38:41 +00:00
|
|
|
#[cfg(feature = "log")]
|
2020-05-22 03:58:49 +00:00
|
|
|
pub use self::logger::Logger;
|
2021-03-20 11:42:00 +00:00
|
|
|
#[cfg(feature = "opentelemetry")]
|
|
|
|
pub use self::opentelemetry::OpenTelemetry;
|
2020-09-14 18:38:41 +00:00
|
|
|
#[cfg(feature = "tracing")]
|
2020-11-07 13:23:22 +00:00
|
|
|
pub use self::tracing::{Tracing, TracingConfig};
|
2020-03-26 03:34:28 +00:00
|
|
|
|
|
|
|
pub(crate) type BoxExtension = Box<dyn Extension>;
|
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
/// Context for extension
|
|
|
|
pub struct ExtensionContext<'a> {
|
2020-09-26 07:52:59 +00:00
|
|
|
#[doc(hidden)]
|
2020-09-29 12:47:37 +00:00
|
|
|
pub schema_data: &'a Data,
|
2020-09-25 07:26:29 +00:00
|
|
|
|
2020-09-26 07:52:59 +00:00
|
|
|
#[doc(hidden)]
|
2020-09-29 12:47:37 +00:00
|
|
|
pub query_data: &'a Data,
|
2020-09-25 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
impl<'a> ExtensionContext<'a> {
|
2020-09-25 07:26:29 +00:00
|
|
|
/// Gets the global data defined in the `Context` or `Schema`.
|
|
|
|
///
|
|
|
|
/// If both `Schema` and `Query` have the same data type, the data in the `Query` is obtained.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
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
|
|
|
/// Returns a `Error` if the specified type data does not exist.
|
2021-02-23 15:05:08 +00:00
|
|
|
pub fn data<D: Any + Send + Sync>(&self) -> Result<&'a D> {
|
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
|
|
|
self.data_opt::<D>().ok_or_else(|| {
|
|
|
|
Error::new(format!(
|
|
|
|
"Data `{}` does not exist.",
|
|
|
|
std::any::type_name::<D>()
|
|
|
|
))
|
|
|
|
})
|
2020-09-25 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the global data defined in the `Context` or `Schema`.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// It will panic if the specified data type does not exist.
|
2021-02-23 15:05:08 +00:00
|
|
|
pub fn data_unchecked<D: Any + Send + Sync>(&self) -> &'a D {
|
2020-09-25 07:26:29 +00:00
|
|
|
self.data_opt::<D>()
|
|
|
|
.unwrap_or_else(|| panic!("Data `{}` does not exist.", std::any::type_name::<D>()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the global data defined in the `Context` or `Schema` or `None` if the specified type data does not exist.
|
2021-02-23 15:05:08 +00:00
|
|
|
pub fn data_opt<D: Any + Send + Sync>(&self) -> Option<&'a D> {
|
2020-09-29 12:47:37 +00:00
|
|
|
self.query_data
|
2020-09-25 07:26:29 +00:00
|
|
|
.get(&TypeId::of::<D>())
|
2020-09-29 12:47:37 +00:00
|
|
|
.or_else(|| self.schema_data.get(&TypeId::of::<D>()))
|
2020-09-25 07:26:29 +00:00
|
|
|
.and_then(|d| d.downcast_ref::<D>())
|
|
|
|
}
|
2020-03-26 03:34:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
/// Parameters for `Extension::resolve_field_start`
|
|
|
|
pub struct ResolveInfo<'a> {
|
|
|
|
/// Because resolver is concurrent, `Extension::resolve_field_start` and `Extension::resolve_field_end` are
|
|
|
|
/// not strictly ordered, so each pair is identified by an id.
|
|
|
|
pub resolve_id: ResolveId,
|
|
|
|
|
|
|
|
/// Current path node, You can go through the entire path.
|
|
|
|
pub path_node: &'a QueryPathNode<'a>,
|
|
|
|
|
|
|
|
/// Parent type
|
|
|
|
pub parent_type: &'a str,
|
|
|
|
|
|
|
|
/// Current return type, is qualified name.
|
|
|
|
pub return_type: &'a str,
|
|
|
|
}
|
|
|
|
|
2020-03-26 03:34:28 +00:00
|
|
|
/// Represents a GraphQL extension
|
2020-09-30 03:37:12 +00:00
|
|
|
#[async_trait::async_trait]
|
2020-03-26 03:34:28 +00:00
|
|
|
#[allow(unused_variables)]
|
|
|
|
pub trait Extension: Sync + Send + 'static {
|
2020-04-28 07:01:19 +00:00
|
|
|
/// If this extension needs to output data to query results, you need to specify a name.
|
|
|
|
fn name(&self) -> Option<&'static str> {
|
|
|
|
None
|
|
|
|
}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
2020-09-30 03:37:12 +00:00
|
|
|
/// Called at the prepare request
|
|
|
|
async fn prepare_request(
|
|
|
|
&mut self,
|
|
|
|
ctx: &ExtensionContext<'_>,
|
|
|
|
request: Request,
|
2020-10-01 01:39:47 +00:00
|
|
|
) -> ServerResult<Request> {
|
2020-09-30 03:37:12 +00:00
|
|
|
Ok(request)
|
|
|
|
}
|
|
|
|
|
2020-03-26 03:34:28 +00:00
|
|
|
/// Called at the begin of the parse.
|
2020-09-29 12:47:37 +00:00
|
|
|
fn parse_start(
|
|
|
|
&mut self,
|
|
|
|
ctx: &ExtensionContext<'_>,
|
|
|
|
query_source: &str,
|
|
|
|
variables: &Variables,
|
|
|
|
) {
|
|
|
|
}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
|
|
|
/// Called at the end of the parse.
|
2020-09-29 12:47:37 +00:00
|
|
|
fn parse_end(&mut self, ctx: &ExtensionContext<'_>, document: &ExecutableDocument) {}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
|
|
|
/// Called at the begin of the validation.
|
2020-09-29 12:47:37 +00:00
|
|
|
fn validation_start(&mut self, ctx: &ExtensionContext<'_>) {}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
|
|
|
/// Called at the end of the validation.
|
2020-12-18 15:58:03 +00:00
|
|
|
fn validation_end(&mut self, ctx: &ExtensionContext<'_>, result: &ValidationResult) {}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
|
|
|
/// Called at the begin of the execution.
|
2020-09-29 12:47:37 +00:00
|
|
|
fn execution_start(&mut self, ctx: &ExtensionContext<'_>) {}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
|
|
|
/// Called at the end of the execution.
|
2020-09-29 12:47:37 +00:00
|
|
|
fn execution_end(&mut self, ctx: &ExtensionContext<'_>) {}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
|
|
|
/// Called at the begin of the resolve field.
|
2020-09-29 12:47:37 +00:00
|
|
|
fn resolve_start(&mut self, ctx: &ExtensionContext<'_>, info: &ResolveInfo<'_>) {}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
|
|
|
/// Called at the end of the resolve field.
|
2020-09-29 12:47:37 +00:00
|
|
|
fn resolve_end(&mut self, ctx: &ExtensionContext<'_>, info: &ResolveInfo<'_>) {}
|
2020-05-22 03:58:49 +00:00
|
|
|
|
|
|
|
/// Called when an error occurs.
|
2020-09-30 17:24:24 +00:00
|
|
|
fn error(&mut self, ctx: &ExtensionContext<'_>, err: &ServerError) {}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
|
|
|
/// Get the results
|
2020-10-10 02:32:43 +00:00
|
|
|
fn result(&mut self, ctx: &ExtensionContext<'_>) -> Option<Value> {
|
2020-04-28 07:01:19 +00:00
|
|
|
None
|
|
|
|
}
|
2020-03-26 03:34:28 +00:00
|
|
|
}
|
2020-05-22 03:58:49 +00:00
|
|
|
|
2020-06-13 14:14:47 +00:00
|
|
|
pub(crate) trait ErrorLogger {
|
2020-10-12 06:49:32 +00:00
|
|
|
fn log_error(self, ctx: &ExtensionContext<'_>, extensions: &Extensions) -> Self;
|
2020-06-13 14:14:47 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
impl<T> ErrorLogger for ServerResult<T> {
|
2020-10-12 06:49:32 +00:00
|
|
|
fn log_error(self, ctx: &ExtensionContext<'_>, extensions: &Extensions) -> Self {
|
2020-06-13 14:14:47 +00:00
|
|
|
if let Err(err) = &self {
|
2020-10-12 06:49:32 +00:00
|
|
|
extensions.error(ctx, err);
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
2020-06-13 14:14:47 +00:00
|
|
|
self
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 06:49:32 +00:00
|
|
|
|
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
|
|
|
impl<T> ErrorLogger for Result<T, Vec<ServerError>> {
|
2020-10-12 06:49:32 +00:00
|
|
|
fn log_error(self, ctx: &ExtensionContext<'_>, extensions: &Extensions) -> Self {
|
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 let Err(errors) = &self {
|
|
|
|
for error in errors {
|
2020-09-30 18:40:17 +00:00
|
|
|
extensions.error(ctx, error);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 03:58:49 +00:00
|
|
|
|
2020-10-12 06:49:32 +00:00
|
|
|
/// Extension factory
|
|
|
|
///
|
|
|
|
/// Used to create an extension instance.
|
|
|
|
pub trait ExtensionFactory: Send + Sync + 'static {
|
|
|
|
/// Create an extended instance.
|
|
|
|
fn create(&self) -> Box<dyn Extension>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct Extensions(Option<spin::Mutex<Vec<BoxExtension>>>);
|
|
|
|
|
|
|
|
impl From<Vec<BoxExtension>> for Extensions {
|
|
|
|
fn from(extensions: Vec<BoxExtension>) -> Self {
|
|
|
|
Self(if extensions.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(spin::Mutex::new(extensions))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
impl Extensions {
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.0.is_none()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn prepare_request(
|
2020-10-16 05:52:52 +00:00
|
|
|
&mut self,
|
2020-09-30 03:37:12 +00:00
|
|
|
ctx: &ExtensionContext<'_>,
|
|
|
|
request: Request,
|
2020-10-01 01:39:47 +00:00
|
|
|
) -> ServerResult<Request> {
|
2020-09-30 03:37:12 +00:00
|
|
|
let mut request = request;
|
2020-10-16 05:52:52 +00:00
|
|
|
if let Some(e) = &mut self.0 {
|
|
|
|
for e in e.get_mut().iter_mut() {
|
2020-10-12 06:49:32 +00:00
|
|
|
request = e.prepare_request(ctx, request).await?;
|
|
|
|
}
|
2020-09-30 03:37:12 +00:00
|
|
|
}
|
|
|
|
Ok(request)
|
|
|
|
}
|
|
|
|
|
2020-10-12 06:49:32 +00:00
|
|
|
pub fn parse_start(
|
2020-10-16 05:52:52 +00:00
|
|
|
&mut self,
|
2020-09-29 12:47:37 +00:00
|
|
|
ctx: &ExtensionContext<'_>,
|
|
|
|
query_source: &str,
|
|
|
|
variables: &Variables,
|
|
|
|
) {
|
2020-10-16 05:52:52 +00:00
|
|
|
if let Some(e) = &mut self.0 {
|
|
|
|
e.get_mut()
|
2020-10-12 06:49:32 +00:00
|
|
|
.iter_mut()
|
|
|
|
.for_each(|e| e.parse_start(ctx, query_source, variables));
|
|
|
|
}
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:52:52 +00:00
|
|
|
pub fn parse_end(&mut self, ctx: &ExtensionContext<'_>, document: &ExecutableDocument) {
|
|
|
|
if let Some(e) = &mut self.0 {
|
|
|
|
e.get_mut()
|
|
|
|
.iter_mut()
|
|
|
|
.for_each(|e| e.parse_end(ctx, document));
|
2020-10-12 06:49:32 +00:00
|
|
|
}
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:52:52 +00:00
|
|
|
pub fn validation_start(&mut self, ctx: &ExtensionContext<'_>) {
|
|
|
|
if let Some(e) = &mut self.0 {
|
|
|
|
e.get_mut().iter_mut().for_each(|e| e.validation_start(ctx));
|
2020-10-12 06:49:32 +00:00
|
|
|
}
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:58:03 +00:00
|
|
|
pub fn validation_end(&mut self, ctx: &ExtensionContext<'_>, result: &ValidationResult) {
|
2020-10-16 05:52:52 +00:00
|
|
|
if let Some(e) = &mut self.0 {
|
2020-12-18 15:58:03 +00:00
|
|
|
e.get_mut()
|
|
|
|
.iter_mut()
|
|
|
|
.for_each(|e| e.validation_end(ctx, result));
|
2020-10-12 06:49:32 +00:00
|
|
|
}
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 06:49:32 +00:00
|
|
|
pub fn execution_start(&self, ctx: &ExtensionContext<'_>) {
|
|
|
|
if let Some(e) = &self.0 {
|
|
|
|
e.lock().iter_mut().for_each(|e| e.execution_start(ctx));
|
|
|
|
}
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 06:49:32 +00:00
|
|
|
pub fn execution_end(&self, ctx: &ExtensionContext<'_>) {
|
|
|
|
if let Some(e) = &self.0 {
|
|
|
|
e.lock().iter_mut().for_each(|e| e.execution_end(ctx));
|
|
|
|
}
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 06:49:32 +00:00
|
|
|
pub fn resolve_start(&self, ctx: &ExtensionContext<'_>, info: &ResolveInfo<'_>) {
|
|
|
|
if let Some(e) = &self.0 {
|
|
|
|
e.lock().iter_mut().for_each(|e| e.resolve_start(ctx, info));
|
|
|
|
}
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 06:49:32 +00:00
|
|
|
pub fn resolve_end(&self, ctx: &ExtensionContext<'_>, resolve_id: &ResolveInfo<'_>) {
|
|
|
|
if let Some(e) = &self.0 {
|
|
|
|
e.lock()
|
|
|
|
.iter_mut()
|
|
|
|
.for_each(|e| e.resolve_end(ctx, resolve_id));
|
|
|
|
}
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 06:49:32 +00:00
|
|
|
pub fn error(&self, ctx: &ExtensionContext<'_>, err: &ServerError) {
|
|
|
|
if let Some(e) = &self.0 {
|
|
|
|
e.lock().iter_mut().for_each(|e| e.error(ctx, err));
|
|
|
|
}
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 06:49:32 +00:00
|
|
|
pub fn result(&self, ctx: &ExtensionContext<'_>) -> Option<Value> {
|
|
|
|
if let Some(e) = &self.0 {
|
|
|
|
let value = e
|
|
|
|
.lock()
|
2020-06-13 14:14:47 +00:00
|
|
|
.iter_mut()
|
2020-05-22 03:58:49 +00:00
|
|
|
.filter_map(|e| {
|
|
|
|
if let Some(name) = e.name() {
|
2020-10-10 02:32:43 +00:00
|
|
|
e.result(ctx).map(|res| (Name::new(name), res))
|
2020-05-22 03:58:49 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2020-10-10 02:32:43 +00:00
|
|
|
.collect::<BTreeMap<_, _>>();
|
2020-05-22 03:58:49 +00:00
|
|
|
if value.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2020-10-10 02:32:43 +00:00
|
|
|
Some(Value::Object(value))
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|