2020-09-29 12:47:37 +00:00
|
|
|
use crate::context::{Data, QueryEnvInner, ResolveId};
|
|
|
|
use crate::extensions::{ErrorLogger, Extension, ExtensionContext, ExtensionFactory, Extensions};
|
2020-03-04 06:24:44 +00:00
|
|
|
use crate::model::__DirectiveLocation;
|
2020-05-09 09:55:04 +00:00
|
|
|
use crate::parser::parse_query;
|
2020-09-29 12:47:37 +00:00
|
|
|
use crate::parser::types::{DocumentOperations, OperationType};
|
2020-05-15 02:08:37 +00:00
|
|
|
use crate::registry::{MetaDirective, MetaInputValue, Registry};
|
2020-09-29 23:45:48 +00:00
|
|
|
use crate::resolver_utils::{resolve_container, resolve_container_serial, ContainerType};
|
2020-09-13 17:52:36 +00:00
|
|
|
use crate::subscription::collect_subscription_streams;
|
2020-03-03 11:15:18 +00:00
|
|
|
use crate::types::QueryRoot;
|
2020-05-22 03:58:49 +00:00
|
|
|
use crate::validation::{check_rules, CheckResult, ValidationMode};
|
2020-03-29 12:02:52 +00:00
|
|
|
use crate::{
|
2020-10-01 01:07:19 +00:00
|
|
|
BatchRequest, BatchResponse, CacheControl, ContextBase, ObjectType, QueryEnv, Request,
|
2020-10-10 02:32:43 +00:00
|
|
|
Response, ServerError, SubscriptionType, Type, Value, ID,
|
2020-03-29 12:02:52 +00:00
|
|
|
};
|
2020-09-13 17:52:36 +00:00
|
|
|
use futures::stream::{self, Stream, StreamExt};
|
2020-05-16 02:05:48 +00:00
|
|
|
use indexmap::map::IndexMap;
|
2020-05-22 03:58:49 +00:00
|
|
|
use itertools::Itertools;
|
2020-04-06 05:49:39 +00:00
|
|
|
use std::any::Any;
|
2020-10-10 02:32:43 +00:00
|
|
|
use std::collections::BTreeMap;
|
2020-05-19 08:24:15 +00:00
|
|
|
use std::ops::Deref;
|
2020-03-29 12:02:52 +00:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
use std::sync::Arc;
|
2020-03-01 10:54:34 +00:00
|
|
|
|
2020-03-29 12:02:52 +00:00
|
|
|
/// Schema builder
|
2020-05-19 08:24:15 +00:00
|
|
|
pub struct SchemaBuilder<Query, Mutation, Subscription> {
|
|
|
|
validation_mode: ValidationMode,
|
|
|
|
query: QueryRoot<Query>,
|
|
|
|
mutation: Mutation,
|
|
|
|
subscription: Subscription,
|
|
|
|
registry: Registry,
|
|
|
|
data: Data,
|
|
|
|
complexity: Option<usize>,
|
|
|
|
depth: Option<usize>,
|
2020-09-29 12:47:37 +00:00
|
|
|
extensions: Vec<Box<dyn ExtensionFactory>>,
|
2020-06-02 00:49:27 +00:00
|
|
|
enable_federation: bool,
|
2020-05-19 08:24:15 +00:00
|
|
|
}
|
2020-03-29 12:02:52 +00:00
|
|
|
|
2020-09-29 23:45:48 +00:00
|
|
|
impl<Query: ContainerType, Mutation: ContainerType, Subscription: SubscriptionType>
|
2020-03-29 12:02:52 +00:00
|
|
|
SchemaBuilder<Query, Mutation, Subscription>
|
2020-03-17 09:26:59 +00:00
|
|
|
{
|
2020-09-06 05:38:31 +00:00
|
|
|
/// Manually register a type in the schema.
|
|
|
|
///
|
|
|
|
/// You can use this function to register schema types that are not directly referenced.
|
2020-04-21 12:47:48 +00:00
|
|
|
pub fn register_type<T: Type>(mut self) -> Self {
|
2020-05-19 08:24:15 +00:00
|
|
|
T::create_type_info(&mut self.registry);
|
2020-04-21 12:47:48 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-09-06 05:38:31 +00:00
|
|
|
/// Disable introspection queries.
|
2020-03-29 12:02:52 +00:00
|
|
|
pub fn disable_introspection(mut self) -> Self {
|
2020-05-19 08:24:15 +00:00
|
|
|
self.query.disable_introspection = true;
|
2020-03-29 12:02:52 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-09-06 05:38:31 +00:00
|
|
|
/// Set the maximum complexity a query can have. By default there is no limit.
|
2020-03-29 12:02:52 +00:00
|
|
|
pub fn limit_complexity(mut self, complexity: usize) -> Self {
|
2020-05-19 08:24:15 +00:00
|
|
|
self.complexity = Some(complexity);
|
2020-03-29 12:02:52 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-09-06 05:38:31 +00:00
|
|
|
/// Set the maximum depth a query can have. By default there is no limit.
|
2020-03-29 12:02:52 +00:00
|
|
|
pub fn limit_depth(mut self, depth: usize) -> Self {
|
2020-05-19 08:24:15 +00:00
|
|
|
self.depth = Some(depth);
|
2020-03-29 12:02:52 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-09-06 05:38:31 +00:00
|
|
|
/// Add an extension to the schema.
|
2020-09-29 12:47:37 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use async_graphql::*;
|
|
|
|
///
|
|
|
|
/// #[derive(SimpleObject)]
|
|
|
|
/// struct Query;
|
|
|
|
///
|
|
|
|
/// let schema = Schema::build(Query, EmptyMutation,EmptySubscription)
|
|
|
|
/// .extension(extensions::Logger)
|
|
|
|
/// .finish();
|
|
|
|
/// ```
|
|
|
|
pub fn extension(mut self, extension: impl ExtensionFactory) -> Self {
|
|
|
|
self.extensions.push(Box::new(extension));
|
2020-03-29 12:02:52 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-09-06 05:38:31 +00:00
|
|
|
/// Add a global data that can be accessed in the `Schema`. You access it with `Context::data`.
|
2020-03-29 12:02:52 +00:00
|
|
|
pub fn data<D: Any + Send + Sync>(mut self, data: D) -> Self {
|
2020-05-19 08:24:15 +00:00
|
|
|
self.data.insert(data);
|
2020-03-29 12:02:52 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-04-06 11:57:21 +00:00
|
|
|
/// Set the validation mode, default is `ValidationMode::Strict`.
|
|
|
|
pub fn validation_mode(mut self, validation_mode: ValidationMode) -> Self {
|
2020-05-19 08:24:15 +00:00
|
|
|
self.validation_mode = validation_mode;
|
2020-04-06 11:57:21 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-06-02 00:49:27 +00:00
|
|
|
/// Enable federation, which is automatically enabled if the Query has least one entity definition.
|
|
|
|
pub fn enable_federation(mut self) -> Self {
|
|
|
|
self.enable_federation = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:02:52 +00:00
|
|
|
/// Build schema.
|
2020-06-02 00:49:27 +00:00
|
|
|
pub fn finish(mut self) -> Schema<Query, Mutation, Subscription> {
|
|
|
|
// federation
|
|
|
|
if self.enable_federation || self.registry.has_entities() {
|
|
|
|
self.registry.create_federation_types();
|
|
|
|
}
|
|
|
|
|
2020-05-19 08:24:15 +00:00
|
|
|
Schema(Arc::new(SchemaInner {
|
|
|
|
validation_mode: self.validation_mode,
|
|
|
|
query: self.query,
|
|
|
|
mutation: self.mutation,
|
|
|
|
subscription: self.subscription,
|
|
|
|
complexity: self.complexity,
|
|
|
|
depth: self.depth,
|
|
|
|
extensions: self.extensions,
|
|
|
|
env: SchemaEnv(Arc::new(SchemaEnvInner {
|
|
|
|
registry: self.registry,
|
|
|
|
data: self.data,
|
|
|
|
})),
|
|
|
|
}))
|
2020-03-29 12:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 08:24:15 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct SchemaEnvInner {
|
|
|
|
pub registry: Registry,
|
|
|
|
pub data: Data,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SchemaEnv(Arc<SchemaEnvInner>);
|
|
|
|
|
|
|
|
impl Deref for SchemaEnv {
|
|
|
|
type Target = SchemaEnvInner;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct SchemaInner<Query, Mutation, Subscription> {
|
|
|
|
pub(crate) validation_mode: ValidationMode,
|
|
|
|
pub(crate) query: QueryRoot<Query>,
|
|
|
|
pub(crate) mutation: Mutation,
|
|
|
|
pub(crate) subscription: Subscription,
|
|
|
|
pub(crate) complexity: Option<usize>,
|
|
|
|
pub(crate) depth: Option<usize>,
|
2020-09-29 12:47:37 +00:00
|
|
|
pub(crate) extensions: Vec<Box<dyn ExtensionFactory>>,
|
2020-05-19 08:24:15 +00:00
|
|
|
pub(crate) env: SchemaEnv,
|
|
|
|
}
|
|
|
|
|
2020-09-06 05:38:31 +00:00
|
|
|
/// GraphQL schema.
|
|
|
|
///
|
|
|
|
/// Cloning a schema is cheap, so it can be easily shared.
|
2020-05-19 08:24:15 +00:00
|
|
|
pub struct Schema<Query, Mutation, Subscription>(Arc<SchemaInner<Query, Mutation, Subscription>>);
|
2020-03-29 12:02:52 +00:00
|
|
|
|
|
|
|
impl<Query, Mutation, Subscription> Clone for Schema<Query, Mutation, Subscription> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Schema(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 06:19:35 +00:00
|
|
|
impl<Query, Mutation, Subscription> Default for Schema<Query, Mutation, Subscription>
|
|
|
|
where
|
|
|
|
Query: Default + ObjectType + Send + Sync + 'static,
|
|
|
|
Mutation: Default + ObjectType + Send + Sync + 'static,
|
|
|
|
Subscription: Default + SubscriptionType + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
fn default() -> Self {
|
|
|
|
Schema::new(
|
|
|
|
Query::default(),
|
|
|
|
Mutation::default(),
|
|
|
|
Subscription::default(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 09:29:52 +00:00
|
|
|
impl<Query, Mutation, Subscription> Deref for Schema<Query, Mutation, Subscription> {
|
2020-05-19 08:24:15 +00:00
|
|
|
type Target = SchemaInner<Query, Mutation, Subscription>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:02:52 +00:00
|
|
|
impl<Query, Mutation, Subscription> Schema<Query, Mutation, Subscription>
|
|
|
|
where
|
|
|
|
Query: ObjectType + Send + Sync + 'static,
|
|
|
|
Mutation: ObjectType + Send + Sync + 'static,
|
|
|
|
Subscription: SubscriptionType + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
/// Create a schema builder
|
2020-03-09 10:05:52 +00:00
|
|
|
///
|
|
|
|
/// The root object for the query and Mutation needs to be specified.
|
2020-03-19 09:20:12 +00:00
|
|
|
/// If there is no mutation, you can use `EmptyMutation`.
|
|
|
|
/// If there is no subscription, you can use `EmptySubscription`.
|
2020-03-29 12:02:52 +00:00
|
|
|
pub fn build(
|
|
|
|
query: Query,
|
|
|
|
mutation: Mutation,
|
|
|
|
subscription: Subscription,
|
|
|
|
) -> SchemaBuilder<Query, Mutation, Subscription> {
|
2020-09-23 00:04:00 +00:00
|
|
|
SchemaBuilder {
|
|
|
|
validation_mode: ValidationMode::Strict,
|
|
|
|
query: QueryRoot {
|
|
|
|
inner: query,
|
|
|
|
disable_introspection: false,
|
|
|
|
},
|
|
|
|
mutation,
|
|
|
|
subscription,
|
|
|
|
registry: Self::create_registry(),
|
|
|
|
data: Default::default(),
|
|
|
|
complexity: None,
|
|
|
|
depth: None,
|
|
|
|
extensions: Default::default(),
|
|
|
|
enable_federation: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_registry() -> Registry {
|
2020-03-08 12:35:36 +00:00
|
|
|
let mut registry = Registry {
|
|
|
|
types: Default::default(),
|
|
|
|
directives: Default::default(),
|
|
|
|
implements: Default::default(),
|
|
|
|
query_type: Query::type_name().to_string(),
|
|
|
|
mutation_type: if Mutation::is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Mutation::type_name().to_string())
|
|
|
|
},
|
2020-03-17 09:26:59 +00:00
|
|
|
subscription_type: if Subscription::is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Subscription::type_name().to_string())
|
|
|
|
},
|
2020-03-08 12:35:36 +00:00
|
|
|
};
|
2020-03-04 06:24:44 +00:00
|
|
|
|
2020-05-15 02:08:37 +00:00
|
|
|
registry.add_directive(MetaDirective {
|
2020-03-04 06:24:44 +00:00
|
|
|
name: "include",
|
|
|
|
description: Some("Directs the executor to include this field or fragment only when the `if` argument is true."),
|
2020-03-05 13:34:31 +00:00
|
|
|
locations: vec![
|
|
|
|
__DirectiveLocation::FIELD,
|
|
|
|
__DirectiveLocation::FRAGMENT_SPREAD,
|
|
|
|
__DirectiveLocation::INLINE_FRAGMENT
|
|
|
|
],
|
2020-03-08 12:35:36 +00:00
|
|
|
args: {
|
2020-05-16 02:05:48 +00:00
|
|
|
let mut args = IndexMap::new();
|
2020-05-15 02:08:37 +00:00
|
|
|
args.insert("if", MetaInputValue {
|
2020-03-08 12:35:36 +00:00
|
|
|
name: "if",
|
|
|
|
description: Some("Included when true."),
|
|
|
|
ty: "Boolean!".to_string(),
|
2020-03-21 01:32:13 +00:00
|
|
|
default_value: None,
|
2020-03-22 01:34:32 +00:00
|
|
|
validator: None,
|
2020-03-08 12:35:36 +00:00
|
|
|
});
|
|
|
|
args
|
|
|
|
}
|
2020-03-04 06:24:44 +00:00
|
|
|
});
|
|
|
|
|
2020-05-15 02:08:37 +00:00
|
|
|
registry.add_directive(MetaDirective {
|
2020-03-04 06:24:44 +00:00
|
|
|
name: "skip",
|
|
|
|
description: Some("Directs the executor to skip this field or fragment when the `if` argument is true."),
|
2020-03-05 13:34:31 +00:00
|
|
|
locations: vec![
|
|
|
|
__DirectiveLocation::FIELD,
|
|
|
|
__DirectiveLocation::FRAGMENT_SPREAD,
|
|
|
|
__DirectiveLocation::INLINE_FRAGMENT
|
|
|
|
],
|
2020-03-08 12:35:36 +00:00
|
|
|
args: {
|
2020-05-16 02:05:48 +00:00
|
|
|
let mut args = IndexMap::new();
|
2020-05-15 02:08:37 +00:00
|
|
|
args.insert("if", MetaInputValue {
|
2020-03-08 12:35:36 +00:00
|
|
|
name: "if",
|
|
|
|
description: Some("Skipped when true."),
|
|
|
|
ty: "Boolean!".to_string(),
|
2020-03-21 01:32:13 +00:00
|
|
|
default_value: None,
|
2020-03-22 01:34:32 +00:00
|
|
|
validator: None,
|
2020-03-08 12:35:36 +00:00
|
|
|
});
|
|
|
|
args
|
|
|
|
}
|
2020-03-04 06:24:44 +00:00
|
|
|
});
|
|
|
|
|
2020-08-06 06:52:54 +00:00
|
|
|
registry.add_directive(MetaDirective {
|
|
|
|
name: "ifdef",
|
|
|
|
description: Some("Directs the executor to query only when the field exists."),
|
|
|
|
locations: vec![__DirectiveLocation::FIELD],
|
|
|
|
args: Default::default(),
|
|
|
|
});
|
|
|
|
|
2020-03-04 06:24:44 +00:00
|
|
|
// register scalars
|
|
|
|
bool::create_type_info(&mut registry);
|
|
|
|
i32::create_type_info(&mut registry);
|
|
|
|
f32::create_type_info(&mut registry);
|
|
|
|
String::create_type_info(&mut registry);
|
2020-05-19 08:24:15 +00:00
|
|
|
ID::create_type_info(&mut registry);
|
2020-03-04 06:24:44 +00:00
|
|
|
|
2020-03-08 12:35:36 +00:00
|
|
|
QueryRoot::<Query>::create_type_info(&mut registry);
|
2020-03-05 09:06:14 +00:00
|
|
|
if !Mutation::is_empty() {
|
|
|
|
Mutation::create_type_info(&mut registry);
|
|
|
|
}
|
2020-03-17 09:26:59 +00:00
|
|
|
if !Subscription::is_empty() {
|
|
|
|
Subscription::create_type_info(&mut registry);
|
|
|
|
}
|
2020-03-04 06:24:44 +00:00
|
|
|
|
2020-09-23 00:04:00 +00:00
|
|
|
registry
|
2020-03-25 07:07:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:02:52 +00:00
|
|
|
/// Create a schema
|
|
|
|
pub fn new(
|
|
|
|
query: Query,
|
|
|
|
mutation: Mutation,
|
|
|
|
subscription: Subscription,
|
|
|
|
) -> Schema<Query, Mutation, Subscription> {
|
|
|
|
Self::build(query, mutation, subscription).finish()
|
2020-03-05 00:39:56 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 00:04:00 +00:00
|
|
|
/// Returns SDL(Schema Definition Language) of this schema.
|
|
|
|
pub fn sdl() -> String {
|
|
|
|
Self::create_registry().export_sdl(false)
|
|
|
|
}
|
|
|
|
|
2020-10-01 01:39:47 +00:00
|
|
|
async fn prepare_request(
|
2020-05-22 03:58:49 +00:00
|
|
|
&self,
|
2020-09-30 17:24:24 +00:00
|
|
|
request: Request,
|
|
|
|
) -> Result<(QueryEnvInner, CacheControl), Vec<ServerError>> {
|
2020-05-22 03:58:49 +00:00
|
|
|
// create extension instances
|
2020-06-13 14:14:47 +00:00
|
|
|
let extensions = spin::Mutex::new(Extensions(
|
2020-06-05 07:42:29 +00:00
|
|
|
self.0
|
|
|
|
.extensions
|
|
|
|
.iter()
|
2020-09-29 12:47:37 +00:00
|
|
|
.map(|factory| factory.create())
|
2020-06-05 07:42:29 +00:00
|
|
|
.collect_vec(),
|
2020-06-13 14:14:47 +00:00
|
|
|
));
|
2020-09-30 03:37:12 +00:00
|
|
|
|
|
|
|
let request = extensions
|
|
|
|
.lock()
|
|
|
|
.prepare_request(
|
|
|
|
&ExtensionContext {
|
|
|
|
schema_data: &self.env.data,
|
|
|
|
query_data: &Default::default(),
|
|
|
|
},
|
|
|
|
request,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
let ctx_extension = ExtensionContext {
|
|
|
|
schema_data: &self.env.data,
|
|
|
|
query_data: &request.data,
|
|
|
|
};
|
2020-05-22 03:58:49 +00:00
|
|
|
|
2020-09-11 07:54:56 +00:00
|
|
|
extensions
|
|
|
|
.lock()
|
2020-09-29 12:47:37 +00:00
|
|
|
.parse_start(&ctx_extension, &request.query, &request.variables);
|
2020-09-11 07:54:56 +00:00
|
|
|
let document = parse_query(&request.query)
|
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
|
|
|
.map_err(Into::<ServerError>::into)
|
2020-09-29 12:47:37 +00:00
|
|
|
.log_error(&ctx_extension, &extensions)?;
|
|
|
|
extensions.lock().parse_end(&ctx_extension, &document);
|
2020-05-22 03:58:49 +00:00
|
|
|
|
|
|
|
// check rules
|
2020-09-29 12:47:37 +00:00
|
|
|
extensions.lock().validation_start(&ctx_extension);
|
2020-05-22 03:58:49 +00:00
|
|
|
let CheckResult {
|
|
|
|
cache_control,
|
|
|
|
complexity,
|
|
|
|
depth,
|
2020-07-11 02:05:30 +00:00
|
|
|
} = check_rules(
|
|
|
|
&self.env.registry,
|
|
|
|
&document,
|
2020-09-11 07:54:56 +00:00
|
|
|
Some(&request.variables),
|
2020-07-11 02:05:30 +00:00
|
|
|
self.validation_mode,
|
|
|
|
)
|
2020-09-29 12:47:37 +00:00
|
|
|
.log_error(&ctx_extension, &extensions)?;
|
|
|
|
extensions.lock().validation_end(&ctx_extension);
|
2020-05-22 03:58:49 +00:00
|
|
|
|
|
|
|
// check limit
|
|
|
|
if let Some(limit_complexity) = self.complexity {
|
|
|
|
if complexity > limit_complexity {
|
2020-09-30 17:24:24 +00:00
|
|
|
return Err(vec![ServerError::new("Query is too complex.")])
|
2020-09-29 12:47:37 +00:00
|
|
|
.log_error(&ctx_extension, &extensions);
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(limit_depth) = self.depth {
|
|
|
|
if depth > limit_depth {
|
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
|
|
|
return Err(vec![ServerError::new("Query is nested too deep.")])
|
2020-09-29 12:47:37 +00:00
|
|
|
.log_error(&ctx_extension, &extensions);
|
2020-05-22 03:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 18:59:48 +00:00
|
|
|
let operation = if let Some(operation_name) = &request.operation_name {
|
|
|
|
match document.operations {
|
|
|
|
DocumentOperations::Single(_) => None,
|
|
|
|
DocumentOperations::Multiple(mut operations) => {
|
|
|
|
operations.remove(operation_name.as_str())
|
|
|
|
}
|
|
|
|
}
|
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_or_else(|| {
|
|
|
|
ServerError::new(format!(r#"Unknown operation named "{}""#, operation_name))
|
2020-09-22 18:59:48 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
match document.operations {
|
|
|
|
DocumentOperations::Single(operation) => Ok(operation),
|
|
|
|
DocumentOperations::Multiple(map) if map.len() == 1 => {
|
|
|
|
Ok(map.into_iter().next().unwrap().1)
|
|
|
|
}
|
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
|
|
|
DocumentOperations::Multiple(_) => {
|
|
|
|
Err(ServerError::new("Operation name required in request."))
|
|
|
|
}
|
2020-09-22 18:59:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let operation = match operation {
|
|
|
|
Ok(operation) => operation,
|
|
|
|
Err(e) => {
|
2020-09-30 17:24:24 +00:00
|
|
|
extensions.lock().error(&ctx_extension, &e);
|
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
|
|
|
return Err(vec![e]);
|
2020-09-10 04:49:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
let env = QueryEnvInner {
|
|
|
|
extensions,
|
|
|
|
variables: request.variables,
|
|
|
|
operation,
|
|
|
|
fragments: document.fragments,
|
2020-10-10 02:32:43 +00:00
|
|
|
uploads: request.uploads,
|
2020-09-29 12:47:37 +00:00
|
|
|
ctx_data: Arc::new(request.data),
|
|
|
|
};
|
|
|
|
Ok((env, cache_control))
|
2020-09-11 07:54:56 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
async fn execute_once(&self, env: QueryEnv) -> Response {
|
2020-09-11 07:54:56 +00:00
|
|
|
// execute
|
|
|
|
let inc_resolve_id = AtomicUsize::default();
|
2020-09-10 04:49:08 +00:00
|
|
|
let ctx = ContextBase {
|
|
|
|
path_node: None,
|
|
|
|
resolve_id: ResolveId::root(),
|
|
|
|
inc_resolve_id: &inc_resolve_id,
|
2020-09-22 18:59:48 +00:00
|
|
|
item: &env.operation.node.selection_set,
|
2020-09-10 08:39:43 +00:00
|
|
|
schema_env: &self.env,
|
2020-09-10 04:49:08 +00:00
|
|
|
query_env: &env,
|
|
|
|
};
|
2020-09-29 12:47:37 +00:00
|
|
|
let ctx_extension = ExtensionContext {
|
|
|
|
schema_data: &self.env.data,
|
|
|
|
query_data: &env.ctx_data,
|
|
|
|
};
|
2020-09-10 04:49:08 +00:00
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
env.extensions.lock().execution_start(&ctx_extension);
|
2020-09-13 17:52:36 +00:00
|
|
|
|
2020-09-22 18:59:48 +00:00
|
|
|
let data = match &env.operation.node.ty {
|
2020-09-29 23:45:48 +00:00
|
|
|
OperationType::Query => resolve_container(&ctx, &self.query).await,
|
|
|
|
OperationType::Mutation => resolve_container_serial(&ctx, &self.mutation).await,
|
2020-09-10 04:49:08 +00:00
|
|
|
OperationType::Subscription => {
|
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
|
|
|
return Response::from_errors(vec![ServerError::new(
|
|
|
|
"Subscriptions are not supported on this transport.",
|
|
|
|
)])
|
2020-09-10 04:49:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
env.extensions.lock().execution_end(&ctx_extension);
|
|
|
|
let extensions = env.extensions.lock().result(&ctx_extension);
|
2020-09-13 17:52:36 +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
|
|
|
match data {
|
|
|
|
Ok(data) => Response::new(data),
|
|
|
|
Err(e) => Response::from_errors(vec![e]),
|
|
|
|
}
|
|
|
|
.extensions(extensions)
|
2020-09-10 04:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 07:54:56 +00:00
|
|
|
/// Execute an GraphQL query.
|
|
|
|
pub async fn execute(&self, request: impl Into<Request>) -> Response {
|
|
|
|
let request = request.into();
|
2020-09-30 03:37:12 +00:00
|
|
|
match self.prepare_request(request).await {
|
2020-09-29 12:47:37 +00:00
|
|
|
Ok((env, cache_control)) => self
|
|
|
|
.execute_once(QueryEnv::new(env))
|
2020-09-13 17:52:36 +00:00
|
|
|
.await
|
|
|
|
.cache_control(cache_control),
|
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
|
|
|
Err(errors) => Response::from_errors(errors),
|
2020-09-13 17:52:36 +00:00
|
|
|
}
|
2020-09-11 07:54:56 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 08:39:55 +00:00
|
|
|
/// Execute an GraphQL batch query.
|
|
|
|
pub async fn execute_batch(&self, batch_request: BatchRequest) -> BatchResponse {
|
|
|
|
match batch_request {
|
|
|
|
BatchRequest::Single(request) => BatchResponse::Single(self.execute(request).await),
|
|
|
|
BatchRequest::Batch(requests) => BatchResponse::Batch(
|
|
|
|
futures::stream::iter(requests.into_iter())
|
|
|
|
.then(|request| self.execute(request))
|
|
|
|
.collect()
|
|
|
|
.await,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 08:41:56 +00:00
|
|
|
pub(crate) fn execute_stream_with_ctx_data(
|
|
|
|
&self,
|
2020-09-17 18:22:54 +00:00
|
|
|
request: impl Into<Request> + Send,
|
2020-09-11 08:41:56 +00:00
|
|
|
ctx_data: Arc<Data>,
|
2020-09-17 18:22:54 +00:00
|
|
|
) -> impl Stream<Item = Response> + Send {
|
2020-09-10 23:58:02 +00:00
|
|
|
let schema = self.clone();
|
2020-09-13 17:52:36 +00:00
|
|
|
|
2020-09-11 07:54:56 +00:00
|
|
|
async_stream::stream! {
|
2020-09-10 23:58:02 +00:00
|
|
|
let request = request.into();
|
2020-09-30 03:37:12 +00:00
|
|
|
let (mut env, cache_control) = match schema.prepare_request(request).await {
|
2020-09-11 07:54:56 +00:00
|
|
|
Ok(res) => res,
|
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
|
|
|
Err(errors) => {
|
|
|
|
yield Response::from_errors(errors);
|
2020-09-10 23:58:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2020-09-29 12:47:37 +00:00
|
|
|
env.ctx_data = ctx_data;
|
|
|
|
let env = QueryEnv::new(env);
|
2020-09-10 23:58:02 +00:00
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
if env.operation.node.ty != OperationType::Subscription {
|
2020-09-13 17:52:36 +00:00
|
|
|
yield schema
|
2020-09-29 12:47:37 +00:00
|
|
|
.execute_once(env)
|
2020-09-13 17:52:36 +00:00
|
|
|
.await
|
|
|
|
.cache_control(cache_control);
|
2020-09-10 23:58:02 +00:00
|
|
|
return;
|
2020-06-13 14:14:47 +00:00
|
|
|
}
|
2020-03-29 12:02:52 +00:00
|
|
|
|
2020-09-10 23:58:02 +00:00
|
|
|
let resolve_id = AtomicUsize::default();
|
|
|
|
let ctx = env.create_context(
|
|
|
|
&schema.env,
|
|
|
|
None,
|
2020-09-22 18:59:48 +00:00
|
|
|
&env.operation.node.selection_set,
|
2020-09-26 07:52:59 +00:00
|
|
|
ResolveId::root(),
|
2020-09-10 23:58:02 +00:00
|
|
|
&resolve_id,
|
|
|
|
);
|
2020-09-29 12:47:37 +00:00
|
|
|
let ctx_extension = ExtensionContext {
|
|
|
|
schema_data: &schema.env.data,
|
|
|
|
query_data: &env.ctx_data,
|
|
|
|
};
|
2020-09-10 23:58:02 +00:00
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
env.extensions.lock().execution_start(&ctx_extension);
|
2020-09-10 23:58:02 +00:00
|
|
|
|
2020-09-13 17:52:36 +00:00
|
|
|
let mut streams = Vec::new();
|
|
|
|
if let Err(e) = collect_subscription_streams(&ctx, &schema.subscription, &mut streams) {
|
2020-09-29 12:47:37 +00:00
|
|
|
env.extensions.lock().execution_end(&ctx_extension);
|
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
|
|
|
yield Response::from_errors(vec![e]);
|
2020-09-10 23:58:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-05-18 16:03:15 +00:00
|
|
|
|
2020-09-29 12:47:37 +00:00
|
|
|
env.extensions.lock().execution_end(&ctx_extension);
|
2020-09-26 07:52:59 +00:00
|
|
|
|
2020-09-13 17:52:36 +00:00
|
|
|
let mut stream = stream::select_all(streams);
|
|
|
|
while let Some(data) = stream.next().await {
|
|
|
|
let is_err = data.is_err();
|
2020-09-29 12:47:37 +00:00
|
|
|
let extensions = env.extensions.lock().result(&ctx_extension);
|
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
|
|
|
yield match data {
|
2020-10-10 02:32:43 +00:00
|
|
|
Ok((name, value)) => {
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
map.insert(name, value);
|
|
|
|
Response::new(Value::Object(map))
|
|
|
|
},
|
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
|
|
|
Err(e) => Response::from_errors(vec![e]),
|
|
|
|
}.extensions(extensions);
|
2020-09-10 23:58:02 +00:00
|
|
|
if is_err {
|
|
|
|
break;
|
|
|
|
}
|
2020-09-10 08:39:43 +00:00
|
|
|
}
|
2020-09-11 07:54:56 +00:00
|
|
|
}
|
2020-03-29 12:02:52 +00:00
|
|
|
}
|
2020-09-11 08:41:56 +00:00
|
|
|
|
|
|
|
/// Execute an GraphQL subscription.
|
2020-09-17 18:22:54 +00:00
|
|
|
pub fn execute_stream(
|
|
|
|
&self,
|
|
|
|
request: impl Into<Request>,
|
|
|
|
) -> impl Stream<Item = Response> + Send {
|
2020-09-11 08:41:56 +00:00
|
|
|
let mut request = request.into();
|
2020-09-12 16:07:46 +00:00
|
|
|
let ctx_data = std::mem::take(&mut request.data);
|
2020-09-11 08:41:56 +00:00
|
|
|
self.execute_stream_with_ctx_data(request, Arc::new(ctx_data))
|
|
|
|
}
|
2020-03-01 10:54:34 +00:00
|
|
|
}
|