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;
|
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
use std::{
|
|
|
|
any::{Any, TypeId},
|
|
|
|
future::Future,
|
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
|
|
|
|
use futures_util::stream::BoxStream;
|
|
|
|
|
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")]
|
2021-04-04 04:05:54 +00:00
|
|
|
pub use self::opentelemetry::OpenTelemetry;
|
2020-09-14 18:38:41 +00:00
|
|
|
#[cfg(feature = "tracing")]
|
2021-04-04 04:05:54 +00:00
|
|
|
pub use self::tracing::Tracing;
|
|
|
|
use crate::{
|
2022-04-19 04:25:11 +00:00
|
|
|
parser::types::ExecutableDocument, Data, DataContext, Error, QueryPathNode, Request, Response,
|
|
|
|
Result, SchemaEnv, ServerError, ServerResult, ValidationResult, Value, Variables,
|
2021-04-04 04:05:54 +00:00
|
|
|
};
|
2020-03-26 03:34:28 +00:00
|
|
|
|
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)]
|
2021-04-07 08:23:28 +00:00
|
|
|
pub schema_env: &'a SchemaEnv,
|
2020-09-25 07:26:29 +00:00
|
|
|
|
2020-09-26 07:52:59 +00:00
|
|
|
#[doc(hidden)]
|
2021-03-25 08:33:11 +00:00
|
|
|
pub session_data: &'a Data,
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub query_data: Option<&'a Data>,
|
2020-09-25 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 09:49:47 +00:00
|
|
|
impl<'a> DataContext<'a> for ExtensionContext<'a> {
|
|
|
|
fn data<D: Any + Send + Sync>(&self) -> Result<&'a D> {
|
|
|
|
ExtensionContext::data::<D>(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn data_unchecked<D: Any + Send + Sync>(&self) -> &'a D {
|
|
|
|
ExtensionContext::data_unchecked::<D>(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn data_opt<D: Any + Send + Sync>(&self) -> Option<&'a D> {
|
|
|
|
ExtensionContext::data_opt::<D>(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
impl<'a> ExtensionContext<'a> {
|
2021-04-07 08:23:28 +00:00
|
|
|
/// Convert the specified [ExecutableDocument] into a query string.
|
|
|
|
///
|
|
|
|
/// Usually used for log extension, it can hide secret arguments.
|
|
|
|
pub fn stringify_execute_doc(&self, doc: &ExecutableDocument, variables: &Variables) -> String {
|
|
|
|
self.schema_env
|
|
|
|
.registry
|
|
|
|
.stringify_exec_doc(variables, doc)
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2020-09-25 07:26:29 +00:00
|
|
|
/// Gets the global data defined in the `Context` or `Schema`.
|
|
|
|
///
|
2022-04-19 04:25:11 +00:00
|
|
|
/// If both `Schema` and `Query` have the same data type, the data in the
|
|
|
|
/// `Query` is obtained.
|
2020-09-25 07:26:29 +00:00
|
|
|
///
|
|
|
|
/// # 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>()))
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
/// 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
|
2021-03-25 08:33:11 +00:00
|
|
|
.and_then(|query_data| query_data.get(&TypeId::of::<D>()))
|
|
|
|
.or_else(|| self.session_data.get(&TypeId::of::<D>()))
|
2021-04-07 08:23:28 +00:00
|
|
|
.or_else(|| self.schema_env.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> {
|
|
|
|
/// 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,
|
2021-06-29 01:30:26 +00:00
|
|
|
|
|
|
|
/// Current field name
|
|
|
|
pub name: &'a str,
|
|
|
|
|
|
|
|
/// Current field alias
|
|
|
|
pub alias: Option<&'a str>,
|
2020-09-29 12:47:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
type RequestFut<'a> = &'a mut (dyn Future<Output = Response> + Send + Unpin);
|
|
|
|
|
|
|
|
type ParseFut<'a> = &'a mut (dyn Future<Output = ServerResult<ExecutableDocument>> + Send + Unpin);
|
|
|
|
|
|
|
|
type ValidationFut<'a> =
|
|
|
|
&'a mut (dyn Future<Output = Result<ValidationResult, Vec<ServerError>>> + Send + Unpin);
|
|
|
|
|
|
|
|
type ExecuteFut<'a> = &'a mut (dyn Future<Output = Response> + Send + Unpin);
|
|
|
|
|
2021-11-19 10:49:37 +00:00
|
|
|
/// A future type used to resolve the field
|
|
|
|
pub type ResolveFut<'a> = &'a mut (dyn Future<Output = ServerResult<Option<Value>>> + Send + Unpin);
|
2021-04-04 04:05:54 +00:00
|
|
|
|
2021-04-05 04:21:02 +00:00
|
|
|
/// The remainder of a extension chain for request.
|
|
|
|
pub struct NextRequest<'a> {
|
2021-04-04 04:05:54 +00:00
|
|
|
chain: &'a [Arc<dyn Extension>],
|
2021-04-05 04:21:02 +00:00
|
|
|
request_fut: RequestFut<'a>,
|
2021-04-04 04:05:54 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 04:21:02 +00:00
|
|
|
impl<'a> NextRequest<'a> {
|
2021-04-04 04:05:54 +00:00
|
|
|
/// Call the [Extension::request] function of next extension.
|
2021-04-05 04:21:02 +00:00
|
|
|
pub async fn run(self, ctx: &ExtensionContext<'_>) -> Response {
|
2021-04-04 04:05:54 +00:00
|
|
|
if let Some((first, next)) = self.chain.split_first() {
|
2021-04-05 04:21:02 +00:00
|
|
|
first
|
|
|
|
.request(
|
|
|
|
ctx,
|
|
|
|
NextRequest {
|
|
|
|
chain: next,
|
|
|
|
request_fut: self.request_fut,
|
|
|
|
},
|
|
|
|
)
|
2021-04-04 04:05:54 +00:00
|
|
|
.await
|
2021-04-05 04:21:02 +00:00
|
|
|
} else {
|
|
|
|
self.request_fut.await
|
2021-04-04 04:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-05 04:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The remainder of a extension chain for subscribe.
|
|
|
|
pub struct NextSubscribe<'a> {
|
|
|
|
chain: &'a [Arc<dyn Extension>],
|
|
|
|
}
|
2021-04-04 04:05:54 +00:00
|
|
|
|
2021-04-05 04:21:02 +00:00
|
|
|
impl<'a> NextSubscribe<'a> {
|
2021-04-04 04:05:54 +00:00
|
|
|
/// Call the [Extension::subscribe] function of next extension.
|
2021-04-05 04:21:02 +00:00
|
|
|
pub fn run<'s>(
|
2021-04-04 04:05:54 +00:00
|
|
|
self,
|
|
|
|
ctx: &ExtensionContext<'_>,
|
|
|
|
stream: BoxStream<'s, Response>,
|
|
|
|
) -> BoxStream<'s, Response> {
|
|
|
|
if let Some((first, next)) = self.chain.split_first() {
|
2021-04-05 04:21:02 +00:00
|
|
|
first.subscribe(ctx, stream, NextSubscribe { chain: next })
|
2021-04-04 04:05:54 +00:00
|
|
|
} else {
|
|
|
|
stream
|
|
|
|
}
|
|
|
|
}
|
2021-04-05 04:21:02 +00:00
|
|
|
}
|
2021-04-04 04:05:54 +00:00
|
|
|
|
2021-04-05 04:21:02 +00:00
|
|
|
/// The remainder of a extension chain for subscribe.
|
|
|
|
pub struct NextPrepareRequest<'a> {
|
|
|
|
chain: &'a [Arc<dyn Extension>],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> NextPrepareRequest<'a> {
|
2021-04-04 04:05:54 +00:00
|
|
|
/// Call the [Extension::prepare_request] function of next extension.
|
2021-04-05 04:21:02 +00:00
|
|
|
pub async fn run(self, ctx: &ExtensionContext<'_>, request: Request) -> ServerResult<Request> {
|
2021-04-04 04:05:54 +00:00
|
|
|
if let Some((first, next)) = self.chain.split_first() {
|
|
|
|
first
|
2021-04-05 04:21:02 +00:00
|
|
|
.prepare_request(ctx, request, NextPrepareRequest { chain: next })
|
2021-04-04 04:05:54 +00:00
|
|
|
.await
|
|
|
|
} else {
|
|
|
|
Ok(request)
|
|
|
|
}
|
2020-09-30 03:37:12 +00:00
|
|
|
}
|
2021-04-05 04:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The remainder of a extension chain for parse query.
|
|
|
|
pub struct NextParseQuery<'a> {
|
|
|
|
chain: &'a [Arc<dyn Extension>],
|
|
|
|
parse_query_fut: ParseFut<'a>,
|
|
|
|
}
|
2020-09-30 03:37:12 +00:00
|
|
|
|
2021-04-05 04:21:02 +00:00
|
|
|
impl<'a> NextParseQuery<'a> {
|
2021-04-04 04:05:54 +00:00
|
|
|
/// Call the [Extension::parse_query] function of next extension.
|
2021-04-05 04:21:02 +00:00
|
|
|
pub async fn run(
|
|
|
|
self,
|
2020-09-29 12:47:37 +00:00
|
|
|
ctx: &ExtensionContext<'_>,
|
2021-04-04 04:05:54 +00:00
|
|
|
query: &str,
|
2020-09-29 12:47:37 +00:00
|
|
|
variables: &Variables,
|
2021-04-04 04:05:54 +00:00
|
|
|
) -> ServerResult<ExecutableDocument> {
|
|
|
|
if let Some((first, next)) = self.chain.split_first() {
|
|
|
|
first
|
2021-04-05 04:21:02 +00:00
|
|
|
.parse_query(
|
|
|
|
ctx,
|
|
|
|
query,
|
|
|
|
variables,
|
|
|
|
NextParseQuery {
|
|
|
|
chain: next,
|
|
|
|
parse_query_fut: self.parse_query_fut,
|
|
|
|
},
|
|
|
|
)
|
2021-04-04 04:05:54 +00:00
|
|
|
.await
|
|
|
|
} else {
|
2021-04-05 04:21:02 +00:00
|
|
|
self.parse_query_fut.await
|
2021-04-04 04:05:54 +00:00
|
|
|
}
|
2020-09-29 12:47:37 +00:00
|
|
|
}
|
2021-04-05 04:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The remainder of a extension chain for validation.
|
|
|
|
pub struct NextValidation<'a> {
|
|
|
|
chain: &'a [Arc<dyn Extension>],
|
|
|
|
validation_fut: ValidationFut<'a>,
|
|
|
|
}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
2021-04-05 04:21:02 +00:00
|
|
|
impl<'a> NextValidation<'a> {
|
2021-04-04 04:05:54 +00:00
|
|
|
/// Call the [Extension::validation] function of next extension.
|
2021-04-05 04:21:02 +00:00
|
|
|
pub async fn run(
|
|
|
|
self,
|
2021-04-04 04:05:54 +00:00
|
|
|
ctx: &ExtensionContext<'_>,
|
|
|
|
) -> Result<ValidationResult, Vec<ServerError>> {
|
|
|
|
if let Some((first, next)) = self.chain.split_first() {
|
2021-04-05 04:21:02 +00:00
|
|
|
first
|
|
|
|
.validation(
|
|
|
|
ctx,
|
|
|
|
NextValidation {
|
|
|
|
chain: next,
|
|
|
|
validation_fut: self.validation_fut,
|
|
|
|
},
|
|
|
|
)
|
2021-04-04 04:05:54 +00:00
|
|
|
.await
|
2021-04-05 04:21:02 +00:00
|
|
|
} else {
|
|
|
|
self.validation_fut.await
|
2021-04-04 04:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-05 04:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The remainder of a extension chain for execute.
|
|
|
|
pub struct NextExecute<'a> {
|
|
|
|
chain: &'a [Arc<dyn Extension>],
|
|
|
|
execute_fut: ExecuteFut<'a>,
|
|
|
|
}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
2021-04-05 04:21:02 +00:00
|
|
|
impl<'a> NextExecute<'a> {
|
2021-04-04 04:05:54 +00:00
|
|
|
/// Call the [Extension::execute] function of next extension.
|
2021-06-10 02:17:33 +00:00
|
|
|
pub async fn run(self, ctx: &ExtensionContext<'_>, operation_name: Option<&str>) -> Response {
|
2021-04-04 04:05:54 +00:00
|
|
|
if let Some((first, next)) = self.chain.split_first() {
|
2021-04-05 04:21:02 +00:00
|
|
|
first
|
|
|
|
.execute(
|
|
|
|
ctx,
|
2021-06-10 02:17:33 +00:00
|
|
|
operation_name,
|
2021-04-05 04:21:02 +00:00
|
|
|
NextExecute {
|
|
|
|
chain: next,
|
|
|
|
execute_fut: self.execute_fut,
|
|
|
|
},
|
|
|
|
)
|
2021-04-04 04:05:54 +00:00
|
|
|
.await
|
2021-04-05 04:21:02 +00:00
|
|
|
} else {
|
|
|
|
self.execute_fut.await
|
2021-04-04 04:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-05 04:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The remainder of a extension chain for resolve.
|
|
|
|
pub struct NextResolve<'a> {
|
|
|
|
chain: &'a [Arc<dyn Extension>],
|
|
|
|
resolve_fut: ResolveFut<'a>,
|
|
|
|
}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
2021-04-05 04:21:02 +00:00
|
|
|
impl<'a> NextResolve<'a> {
|
2021-04-04 04:05:54 +00:00
|
|
|
/// Call the [Extension::resolve] function of next extension.
|
2021-04-05 04:21:02 +00:00
|
|
|
pub async fn run(
|
|
|
|
self,
|
2021-04-04 04:05:54 +00:00
|
|
|
ctx: &ExtensionContext<'_>,
|
|
|
|
info: ResolveInfo<'_>,
|
|
|
|
) -> ServerResult<Option<Value>> {
|
|
|
|
if let Some((first, next)) = self.chain.split_first() {
|
2021-04-05 04:21:02 +00:00
|
|
|
first
|
|
|
|
.resolve(
|
|
|
|
ctx,
|
|
|
|
info,
|
|
|
|
NextResolve {
|
|
|
|
chain: next,
|
|
|
|
resolve_fut: self.resolve_fut,
|
|
|
|
},
|
|
|
|
)
|
2021-04-04 04:05:54 +00:00
|
|
|
.await
|
2021-04-05 04:21:02 +00:00
|
|
|
} else {
|
|
|
|
self.resolve_fut.await
|
2021-04-04 04:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
/// Represents a GraphQL extension
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
pub trait Extension: Sync + Send + 'static {
|
|
|
|
/// Called at start query/mutation request.
|
2021-04-05 04:21:02 +00:00
|
|
|
async fn request(&self, ctx: &ExtensionContext<'_>, next: NextRequest<'_>) -> Response {
|
|
|
|
next.run(ctx).await
|
2021-04-04 04:05:54 +00:00
|
|
|
}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
/// Called at subscribe request.
|
|
|
|
fn subscribe<'s>(
|
|
|
|
&self,
|
|
|
|
ctx: &ExtensionContext<'_>,
|
|
|
|
stream: BoxStream<'s, Response>,
|
2021-04-05 04:21:02 +00:00
|
|
|
next: NextSubscribe<'_>,
|
2021-04-04 04:05:54 +00:00
|
|
|
) -> BoxStream<'s, Response> {
|
2021-04-05 04:21:02 +00:00
|
|
|
next.run(ctx, stream)
|
2021-04-04 04:05:54 +00:00
|
|
|
}
|
2020-05-22 03:58:49 +00:00
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
/// Called at prepare request.
|
|
|
|
async fn prepare_request(
|
|
|
|
&self,
|
|
|
|
ctx: &ExtensionContext<'_>,
|
|
|
|
request: Request,
|
2021-04-05 04:21:02 +00:00
|
|
|
next: NextPrepareRequest<'_>,
|
2021-04-04 04:05:54 +00:00
|
|
|
) -> ServerResult<Request> {
|
2021-04-05 04:21:02 +00:00
|
|
|
next.run(ctx, request).await
|
2021-04-04 04:05:54 +00:00
|
|
|
}
|
2020-03-26 03:34:28 +00:00
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
/// Called at parse query.
|
|
|
|
async fn parse_query(
|
|
|
|
&self,
|
|
|
|
ctx: &ExtensionContext<'_>,
|
|
|
|
query: &str,
|
|
|
|
variables: &Variables,
|
2021-04-05 04:21:02 +00:00
|
|
|
next: NextParseQuery<'_>,
|
2021-04-04 04:05:54 +00:00
|
|
|
) -> ServerResult<ExecutableDocument> {
|
2021-04-05 04:21:02 +00:00
|
|
|
next.run(ctx, query, variables).await
|
2020-04-28 07:01:19 +00:00
|
|
|
}
|
2020-05-22 03:58:49 +00:00
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
/// Called at validation query.
|
|
|
|
async fn validation(
|
|
|
|
&self,
|
|
|
|
ctx: &ExtensionContext<'_>,
|
2021-04-05 04:21:02 +00:00
|
|
|
next: NextValidation<'_>,
|
2021-04-04 04:05:54 +00:00
|
|
|
) -> Result<ValidationResult, Vec<ServerError>> {
|
2021-04-05 04:21:02 +00:00
|
|
|
next.run(ctx).await
|
2021-04-04 04:05:54 +00:00
|
|
|
}
|
2020-06-13 14:14:47 +00:00
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
/// Called at execute query.
|
2021-06-10 02:17:33 +00:00
|
|
|
async fn execute(
|
|
|
|
&self,
|
|
|
|
ctx: &ExtensionContext<'_>,
|
|
|
|
operation_name: Option<&str>,
|
|
|
|
next: NextExecute<'_>,
|
|
|
|
) -> Response {
|
|
|
|
next.run(ctx, operation_name).await
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
2020-10-12 06:49:32 +00:00
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
/// Called at resolve field.
|
|
|
|
async fn resolve(
|
|
|
|
&self,
|
|
|
|
ctx: &ExtensionContext<'_>,
|
|
|
|
info: ResolveInfo<'_>,
|
2021-04-05 04:21:02 +00:00
|
|
|
next: NextResolve<'_>,
|
2021-04-04 04:05:54 +00:00
|
|
|
) -> ServerResult<Option<Value>> {
|
2021-04-05 04:21:02 +00:00
|
|
|
next.run(ctx, info).await
|
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
|
|
|
}
|
|
|
|
}
|
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.
|
2021-04-04 04:05:54 +00:00
|
|
|
fn create(&self) -> Arc<dyn Extension>;
|
2020-10-12 06:49:32 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
#[derive(Clone)]
|
2020-10-12 06:49:32 +00:00
|
|
|
#[doc(hidden)]
|
2021-03-25 08:33:11 +00:00
|
|
|
pub struct Extensions {
|
2021-04-04 04:05:54 +00:00
|
|
|
extensions: Vec<Arc<dyn Extension>>,
|
2021-03-25 08:33:11 +00:00
|
|
|
schema_env: SchemaEnv,
|
|
|
|
session_data: Arc<Data>,
|
|
|
|
query_data: Option<Arc<Data>>,
|
|
|
|
}
|
2020-10-12 06:49:32 +00:00
|
|
|
|
2021-03-25 08:33:11 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
impl Extensions {
|
2021-04-04 04:05:54 +00:00
|
|
|
pub(crate) fn new(
|
|
|
|
extensions: impl IntoIterator<Item = Arc<dyn Extension>>,
|
2021-03-25 08:33:11 +00:00
|
|
|
schema_env: SchemaEnv,
|
|
|
|
session_data: Arc<Data>,
|
|
|
|
) -> Self {
|
|
|
|
Extensions {
|
2021-04-04 04:05:54 +00:00
|
|
|
extensions: extensions.into_iter().collect(),
|
2021-03-25 08:33:11 +00:00
|
|
|
schema_env,
|
|
|
|
session_data,
|
|
|
|
query_data: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 04:21:02 +00:00
|
|
|
#[inline]
|
2021-03-25 08:33:11 +00:00
|
|
|
pub fn attach_query_data(&mut self, data: Arc<Data>) {
|
|
|
|
self.query_data = Some(data);
|
|
|
|
}
|
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
#[inline]
|
|
|
|
pub(crate) fn is_empty(&self) -> bool {
|
|
|
|
self.extensions.is_empty()
|
2020-10-12 06:49:32 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 08:33:11 +00:00
|
|
|
#[inline]
|
2021-04-04 04:05:54 +00:00
|
|
|
fn create_context(&self) -> ExtensionContext {
|
2021-03-25 08:33:11 +00:00
|
|
|
ExtensionContext {
|
2021-04-07 08:23:28 +00:00
|
|
|
schema_env: &self.schema_env,
|
2021-03-25 08:33:11 +00:00
|
|
|
session_data: &self.session_data,
|
|
|
|
query_data: self.query_data.as_deref(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
pub async fn request(&self, request_fut: RequestFut<'_>) -> Response {
|
2021-04-05 04:21:02 +00:00
|
|
|
let next = NextRequest {
|
|
|
|
chain: &self.extensions,
|
|
|
|
request_fut,
|
|
|
|
};
|
|
|
|
next.run(&self.create_context()).await
|
2021-03-25 08:33:11 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
pub fn subscribe<'s>(&self, stream: BoxStream<'s, Response>) -> BoxStream<'s, Response> {
|
2021-04-05 04:21:02 +00:00
|
|
|
let next = NextSubscribe {
|
|
|
|
chain: &self.extensions,
|
|
|
|
};
|
|
|
|
next.run(&self.create_context(), stream)
|
2021-03-25 08:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn prepare_request(&self, request: Request) -> ServerResult<Request> {
|
2021-04-05 04:21:02 +00:00
|
|
|
let next = NextPrepareRequest {
|
|
|
|
chain: &self.extensions,
|
|
|
|
};
|
|
|
|
next.run(&self.create_context(), request).await
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
pub async fn parse_query(
|
|
|
|
&self,
|
|
|
|
query: &str,
|
|
|
|
variables: &Variables,
|
|
|
|
parse_query_fut: ParseFut<'_>,
|
|
|
|
) -> ServerResult<ExecutableDocument> {
|
2021-04-05 04:21:02 +00:00
|
|
|
let next = NextParseQuery {
|
|
|
|
chain: &self.extensions,
|
|
|
|
parse_query_fut,
|
|
|
|
};
|
|
|
|
next.run(&self.create_context(), query, variables).await
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
pub async fn validation(
|
|
|
|
&self,
|
|
|
|
validation_fut: ValidationFut<'_>,
|
|
|
|
) -> Result<ValidationResult, Vec<ServerError>> {
|
2021-04-05 04:21:02 +00:00
|
|
|
let next = NextValidation {
|
|
|
|
chain: &self.extensions,
|
|
|
|
validation_fut,
|
|
|
|
};
|
|
|
|
next.run(&self.create_context()).await
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 02:17:33 +00:00
|
|
|
pub async fn execute(
|
|
|
|
&self,
|
|
|
|
operation_name: Option<&str>,
|
|
|
|
execute_fut: ExecuteFut<'_>,
|
|
|
|
) -> Response {
|
2021-04-05 04:21:02 +00:00
|
|
|
let next = NextExecute {
|
|
|
|
chain: &self.extensions,
|
|
|
|
execute_fut,
|
|
|
|
};
|
2021-06-10 02:17:33 +00:00
|
|
|
next.run(&self.create_context(), operation_name).await
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 04:05:54 +00:00
|
|
|
pub async fn resolve(
|
|
|
|
&self,
|
|
|
|
info: ResolveInfo<'_>,
|
|
|
|
resolve_fut: ResolveFut<'_>,
|
|
|
|
) -> ServerResult<Option<Value>> {
|
2021-04-05 04:21:02 +00:00
|
|
|
let next = NextResolve {
|
|
|
|
chain: &self.extensions,
|
|
|
|
resolve_fut,
|
|
|
|
};
|
|
|
|
next.run(&self.create_context(), info).await
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
}
|