async-graphql/src/validation/visitor.rs

822 lines
24 KiB
Rust
Raw Normal View History

2020-10-15 06:38:10 +00:00
use std::collections::HashMap;
use std::fmt::{self, Display, Formatter};
2020-12-18 06:59:37 +00:00
use async_graphql_value::Value;
2020-09-06 05:38:31 +00:00
use crate::parser::types::{
Directive, ExecutableDocument, Field, FragmentDefinition, FragmentSpread, InlineFragment,
OperationDefinition, OperationType, Selection, SelectionSet, TypeCondition, VariableDefinition,
2020-03-08 12:35:36 +00:00
};
use crate::registry::{self, MetaType, MetaTypeName};
2020-12-18 06:59:37 +00:00
use crate::{InputType, Name, Pos, Positioned, ServerError, ServerResult, Variables};
2020-03-22 08:45:59 +00:00
2020-12-18 06:59:37 +00:00
#[doc(hidden)]
pub struct VisitorContext<'a> {
Rework errors This completely overhauls the error system used in async-graphql. - `Error` has been renamed to `ServerError` and `FieldError` has been renamed to just `Error`. This is because `FieldError` is by far the most common error that users will have to use so it makes sense to use the most obvious error name. Also, the current name didn't make sense as it was used for things other than field errors, such as the data callback for websockets. - `ServerError` has been made completely opaque. Before it was an enum of all the possible errors, but now it just contains an error message, the locations, the path and extensions. It is a shame that we lose information, it makes more sense as _conceptually_ GraphQL does not provide that information. It also frees us to change the internals of async-graphql a lot more. - The path of errors is no longer an opaque JSON value but a regular type, `Vec<PathSegment>`. The type duplication of `PathSegment` and `QueryPathSegment` is unfortunate, I plan to work on this in the future. - Now that `ServerError` is opaque, `RuleError` has been removed from the public API, making it simpler. - Additionally `QueryError` has been completely removed. Instead the error messages are constructed ad-hoc; I took care to never repeat an error message. - Instead of constructing field-not-found errors inside the implementations of field resolvers they now return `Option`s, where a `None` value is representative of the field not being found. - As an unfortunate consequence of the last change, self-referential types based on the output of a subscription resolver can no longer be created. This does not mean anything for users, but causes lifetime issues in the implementation of merged objects. I fixed it with a bit of a hack, but this'll have to be looked into further. - `InputValueError` now has a generic parameter - it's kind of weird but it's necessary for ergonomics. It also improves error messages. - The `ErrorExtensions` trait has been removed. I didn't think the `extend` method was necessary since `From` impls exist. But the ergonomics are still there with a new trait `ExtendError`, which is implemented for both errors and results. - `Response` now supports serializing multiple errors. This allows for nice things like having multiple validation errors not be awkwardly shoved into a single error. - When an error occurs in execution, data is sent as `null`. This is slightly more compliant with the spec but the algorithm described in <https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has yet to be implemented.
2020-09-29 19:06:44 +00:00
pub(crate) registry: &'a registry::Registry,
pub(crate) variables: Option<&'a Variables>,
pub(crate) errors: Vec<RuleError>,
type_stack: Vec<Option<&'a registry::MetaType>>,
input_type: Vec<Option<MetaTypeName<'a>>>,
fragments: &'a HashMap<Name, Positioned<FragmentDefinition>>,
2020-03-22 08:45:59 +00:00
}
impl<'a> VisitorContext<'a> {
Rework errors This completely overhauls the error system used in async-graphql. - `Error` has been renamed to `ServerError` and `FieldError` has been renamed to just `Error`. This is because `FieldError` is by far the most common error that users will have to use so it makes sense to use the most obvious error name. Also, the current name didn't make sense as it was used for things other than field errors, such as the data callback for websockets. - `ServerError` has been made completely opaque. Before it was an enum of all the possible errors, but now it just contains an error message, the locations, the path and extensions. It is a shame that we lose information, it makes more sense as _conceptually_ GraphQL does not provide that information. It also frees us to change the internals of async-graphql a lot more. - The path of errors is no longer an opaque JSON value but a regular type, `Vec<PathSegment>`. The type duplication of `PathSegment` and `QueryPathSegment` is unfortunate, I plan to work on this in the future. - Now that `ServerError` is opaque, `RuleError` has been removed from the public API, making it simpler. - Additionally `QueryError` has been completely removed. Instead the error messages are constructed ad-hoc; I took care to never repeat an error message. - Instead of constructing field-not-found errors inside the implementations of field resolvers they now return `Option`s, where a `None` value is representative of the field not being found. - As an unfortunate consequence of the last change, self-referential types based on the output of a subscription resolver can no longer be created. This does not mean anything for users, but causes lifetime issues in the implementation of merged objects. I fixed it with a bit of a hack, but this'll have to be looked into further. - `InputValueError` now has a generic parameter - it's kind of weird but it's necessary for ergonomics. It also improves error messages. - The `ErrorExtensions` trait has been removed. I didn't think the `extend` method was necessary since `From` impls exist. But the ergonomics are still there with a new trait `ExtendError`, which is implemented for both errors and results. - `Response` now supports serializing multiple errors. This allows for nice things like having multiple validation errors not be awkwardly shoved into a single error. - When an error occurs in execution, data is sent as `null`. This is slightly more compliant with the spec but the algorithm described in <https://spec.graphql.org/June2018/#sec-Errors-and-Non-Nullability> has yet to be implemented.
2020-09-29 19:06:44 +00:00
pub(crate) fn new(
registry: &'a registry::Registry,
doc: &'a ExecutableDocument,
variables: Option<&'a Variables>,
) -> Self {
2020-03-22 08:45:59 +00:00
Self {
registry,
variables,
2020-03-22 08:45:59 +00:00
errors: Default::default(),
type_stack: Default::default(),
2020-04-05 08:00:26 +00:00
input_type: Default::default(),
fragments: &doc.fragments,
2020-03-22 08:45:59 +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
pub(crate) fn report_error<T: Into<String>>(&mut self, locations: Vec<Pos>, msg: T) {
2020-03-22 08:45:59 +00:00
self.errors.push(RuleError {
locations,
message: msg.into(),
})
}
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
pub(crate) fn append_errors(&mut self, errors: Vec<RuleError>) {
2020-03-22 08:45:59 +00:00
self.errors.extend(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
pub(crate) fn with_type<F: FnMut(&mut VisitorContext<'a>)>(
2020-03-22 08:45:59 +00:00
&mut self,
ty: Option<&'a registry::MetaType>,
2020-03-22 08:45:59 +00:00
mut f: F,
) {
self.type_stack.push(ty);
f(self);
self.type_stack.pop();
}
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
pub(crate) fn with_input_type<F: FnMut(&mut VisitorContext<'a>)>(
2020-04-05 08:00:26 +00:00
&mut self,
ty: Option<MetaTypeName<'a>>,
2020-04-05 08:00:26 +00:00
mut f: F,
) {
self.input_type.push(ty);
f(self);
self.input_type.pop();
}
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
pub(crate) fn parent_type(&self) -> Option<&'a registry::MetaType> {
2020-03-22 08:45:59 +00:00
if self.type_stack.len() >= 2 {
2020-04-05 08:00:26 +00:00
self.type_stack
.get(self.type_stack.len() - 2)
.copied()
.flatten()
2020-03-22 08:45:59 +00:00
} else {
None
}
}
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
pub(crate) fn current_type(&self) -> Option<&'a registry::MetaType> {
2020-04-05 08:00:26 +00:00
self.type_stack.last().copied().flatten()
2020-03-22 08:45:59 +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
pub(crate) fn is_known_fragment(&self, name: &str) -> bool {
2020-03-22 08:45:59 +00:00
self.fragments.contains_key(name)
}
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
pub(crate) fn fragment(&self, name: &str) -> Option<&'a Positioned<FragmentDefinition>> {
self.fragments.get(name)
2020-03-22 08:45:59 +00:00
}
2020-12-18 06:59:37 +00:00
#[doc(hidden)]
pub fn param_value<T: InputType>(
&self,
variable_definitions: &[Positioned<VariableDefinition>],
field: &Field,
name: &str,
default: Option<fn() -> T>,
) -> ServerResult<T> {
let value = field.get_argument(name).cloned();
if value.is_none() {
if let Some(default) = default {
return Ok(default());
}
}
let (pos, value) = match value {
Some(value) => {
let pos = value.pos;
(
pos,
Some(value.node.into_const_with(|name| {
variable_definitions
.iter()
.find(|def| def.node.name.node == name)
.and_then(|def| {
if let Some(variables) = self.variables {
variables
.get(&def.node.name.node)
.or_else(|| def.node.default_value())
} else {
None
}
})
.cloned()
.ok_or_else(|| {
ServerError::new(format!("Variable {} is not defined.", name))
.at(pos)
})
})?),
)
}
None => (Pos::default(), None),
};
T::parse(value).map_err(|e| e.into_server_error().at(pos))
}
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub(crate) enum VisitMode {
Normal,
Inline,
2020-03-22 08:45:59 +00:00
}
2020-03-08 12:35: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
pub(crate) trait Visitor<'a> {
2020-12-18 06:59:37 +00:00
fn mode(&self) -> VisitMode {
VisitMode::Normal
}
fn enter_document(&mut self, _ctx: &mut VisitorContext<'a>, _doc: &'a ExecutableDocument) {}
fn exit_document(&mut self, _ctx: &mut VisitorContext<'a>, _doc: &'a ExecutableDocument) {}
2020-03-08 12:35:36 +00:00
fn enter_operation_definition(
&mut self,
2020-03-22 08:45:59 +00:00
_ctx: &mut VisitorContext<'a>,
_name: Option<&'a Name>,
2020-05-10 02:59:51 +00:00
_operation_definition: &'a Positioned<OperationDefinition>,
2020-03-08 12:35:36 +00:00
) {
}
fn exit_operation_definition(
&mut self,
2020-03-22 08:45:59 +00:00
_ctx: &mut VisitorContext<'a>,
_name: Option<&'a Name>,
2020-05-10 02:59:51 +00:00
_operation_definition: &'a Positioned<OperationDefinition>,
2020-03-08 12:35:36 +00:00
) {
}
fn enter_fragment_definition(
&mut self,
2020-03-22 08:45:59 +00:00
_ctx: &mut VisitorContext<'a>,
_name: &'a Name,
2020-05-10 02:59:51 +00:00
_fragment_definition: &'a Positioned<FragmentDefinition>,
2020-03-08 12:35:36 +00:00
) {
}
fn exit_fragment_definition(
&mut self,
2020-03-22 08:45:59 +00:00
_ctx: &mut VisitorContext<'a>,
_name: &'a Name,
2020-05-10 02:59:51 +00:00
_fragment_definition: &'a Positioned<FragmentDefinition>,
2020-03-08 12:35:36 +00:00
) {
}
fn enter_variable_definition(
&mut self,
2020-03-22 08:45:59 +00:00
_ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
_variable_definition: &'a Positioned<VariableDefinition>,
2020-03-08 12:35:36 +00:00
) {
}
fn exit_variable_definition(
&mut self,
2020-03-22 08:45:59 +00:00
_ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
_variable_definition: &'a Positioned<VariableDefinition>,
2020-03-08 12:35:36 +00:00
) {
}
fn enter_directive(
&mut self,
_ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
_directive: &'a Positioned<Directive>,
) {
}
fn exit_directive(
&mut self,
_ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
_directive: &'a Positioned<Directive>,
) {
}
2020-03-08 12:35:36 +00:00
2020-03-09 12:39:46 +00:00
fn enter_argument(
&mut self,
2020-03-22 08:45:59 +00:00
_ctx: &mut VisitorContext<'a>,
_name: &'a Positioned<Name>,
2020-05-10 02:59:51 +00:00
_value: &'a Positioned<Value>,
2020-03-09 12:39:46 +00:00
) {
}
fn exit_argument(
&mut self,
2020-03-22 08:45:59 +00:00
_ctx: &mut VisitorContext<'a>,
_name: &'a Positioned<Name>,
2020-05-10 02:59:51 +00:00
_value: &'a Positioned<Value>,
2020-03-09 12:39:46 +00:00
) {
}
2020-03-08 12:35:36 +00:00
2020-03-12 09:11:02 +00:00
fn enter_selection_set(
&mut self,
2020-03-22 08:45:59 +00:00
_ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
_selection_set: &'a Positioned<SelectionSet>,
2020-03-12 09:11:02 +00:00
) {
}
fn exit_selection_set(
&mut self,
2020-03-22 08:45:59 +00:00
_ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
_selection_set: &'a Positioned<SelectionSet>,
2020-03-12 09:11:02 +00:00
) {
}
fn enter_selection(
&mut self,
_ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
_selection: &'a Positioned<Selection>,
) {
}
fn exit_selection(
&mut self,
_ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
_selection: &'a Positioned<Selection>,
) {
}
2020-03-08 12:35:36 +00:00
2020-05-10 02:59:51 +00:00
fn enter_field(&mut self, _ctx: &mut VisitorContext<'a>, _field: &'a Positioned<Field>) {}
fn exit_field(&mut self, _ctx: &mut VisitorContext<'a>, _field: &'a Positioned<Field>) {}
2020-03-08 12:35:36 +00:00
fn enter_fragment_spread(
&mut self,
2020-03-22 08:45:59 +00:00
_ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
_fragment_spread: &'a Positioned<FragmentSpread>,
2020-03-08 12:35:36 +00:00
) {
}
fn exit_fragment_spread(
&mut self,
2020-03-22 08:45:59 +00:00
_ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
_fragment_spread: &'a Positioned<FragmentSpread>,
2020-03-08 12:35:36 +00:00
) {
}
fn enter_inline_fragment(
&mut self,
2020-03-22 08:45:59 +00:00
_ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
_inline_fragment: &'a Positioned<InlineFragment>,
2020-03-08 12:35:36 +00:00
) {
}
fn exit_inline_fragment(
&mut self,
2020-03-22 08:45:59 +00:00
_ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
_inline_fragment: &'a Positioned<InlineFragment>,
2020-03-08 12:35:36 +00:00
) {
}
2020-04-05 08:00:26 +00:00
fn enter_input_value(
&mut self,
_ctx: &mut VisitorContext<'a>,
_pos: Pos,
_expected_type: &Option<MetaTypeName<'a>>,
2020-04-05 08:00:26 +00:00
_value: &'a Value,
) {
}
fn exit_input_value(
&mut self,
_ctx: &mut VisitorContext<'a>,
_pos: Pos,
_expected_type: &Option<MetaTypeName<'a>>,
2020-04-05 08:00:26 +00:00
_value: &Value,
) {
}
2020-03-08 12:35: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
pub(crate) struct VisitorNil;
2020-03-08 12:35:36 +00:00
impl VisitorNil {
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
pub(crate) fn with<V>(self, visitor: V) -> VisitorCons<V, Self> {
2020-03-08 12:35:36 +00:00
VisitorCons(visitor, 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
pub(crate) struct VisitorCons<A, B>(A, B);
2020-03-08 12:35:36 +00:00
impl<A, B> VisitorCons<A, B> {
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
pub(crate) fn with<V>(self, visitor: V) -> VisitorCons<V, Self> {
2020-03-08 12:35:36 +00:00
VisitorCons(visitor, self)
}
}
impl<'a> Visitor<'a> for VisitorNil {}
impl<'a, A, B> Visitor<'a> for VisitorCons<A, B>
where
A: Visitor<'a> + 'a,
B: Visitor<'a> + 'a,
{
fn enter_document(&mut self, ctx: &mut VisitorContext<'a>, doc: &'a ExecutableDocument) {
2020-03-08 12:35:36 +00:00
self.0.enter_document(ctx, doc);
self.1.enter_document(ctx, doc);
}
fn exit_document(&mut self, ctx: &mut VisitorContext<'a>, doc: &'a ExecutableDocument) {
2020-03-08 12:35:36 +00:00
self.0.exit_document(ctx, doc);
self.1.exit_document(ctx, doc);
}
fn enter_operation_definition(
&mut self,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
name: Option<&'a Name>,
2020-05-10 02:59:51 +00:00
operation_definition: &'a Positioned<OperationDefinition>,
2020-03-08 12:35:36 +00:00
) {
self.0
.enter_operation_definition(ctx, name, operation_definition);
self.1
.enter_operation_definition(ctx, name, operation_definition);
2020-03-08 12:35:36 +00:00
}
fn exit_operation_definition(
&mut self,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
name: Option<&'a Name>,
2020-05-10 02:59:51 +00:00
operation_definition: &'a Positioned<OperationDefinition>,
2020-03-08 12:35:36 +00:00
) {
self.0
.exit_operation_definition(ctx, name, operation_definition);
self.1
.exit_operation_definition(ctx, name, operation_definition);
2020-03-08 12:35:36 +00:00
}
fn enter_fragment_definition(
&mut self,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
name: &'a Name,
2020-05-10 02:59:51 +00:00
fragment_definition: &'a Positioned<FragmentDefinition>,
2020-03-08 12:35:36 +00:00
) {
self.0
.enter_fragment_definition(ctx, name, fragment_definition);
self.1
.enter_fragment_definition(ctx, name, fragment_definition);
2020-03-08 12:35:36 +00:00
}
fn exit_fragment_definition(
&mut self,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
name: &'a Name,
2020-05-10 02:59:51 +00:00
fragment_definition: &'a Positioned<FragmentDefinition>,
2020-03-08 12:35:36 +00:00
) {
self.0
.exit_fragment_definition(ctx, name, fragment_definition);
self.1
.exit_fragment_definition(ctx, name, fragment_definition);
2020-03-08 12:35:36 +00:00
}
fn enter_variable_definition(
&mut self,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
variable_definition: &'a Positioned<VariableDefinition>,
2020-03-08 12:35:36 +00:00
) {
self.0.enter_variable_definition(ctx, variable_definition);
self.1.enter_variable_definition(ctx, variable_definition);
}
fn exit_variable_definition(
&mut self,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
variable_definition: &'a Positioned<VariableDefinition>,
2020-03-08 12:35:36 +00:00
) {
self.0.exit_variable_definition(ctx, variable_definition);
self.1.exit_variable_definition(ctx, variable_definition);
}
2020-05-10 02:59:51 +00:00
fn enter_directive(
&mut self,
ctx: &mut VisitorContext<'a>,
directive: &'a Positioned<Directive>,
) {
2020-03-08 12:35:36 +00:00
self.0.enter_directive(ctx, directive);
self.1.enter_directive(ctx, directive);
}
2020-05-10 02:59:51 +00:00
fn exit_directive(
&mut self,
ctx: &mut VisitorContext<'a>,
directive: &'a Positioned<Directive>,
) {
2020-03-08 12:35:36 +00:00
self.0.exit_directive(ctx, directive);
self.1.exit_directive(ctx, directive);
}
2020-03-09 12:39:46 +00:00
fn enter_argument(
&mut self,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
name: &'a Positioned<Name>,
2020-05-10 02:59:51 +00:00
value: &'a Positioned<Value>,
2020-03-09 12:39:46 +00:00
) {
self.0.enter_argument(ctx, name, value);
self.1.enter_argument(ctx, name, value);
2020-03-08 12:35:36 +00:00
}
2020-03-09 12:39:46 +00:00
fn exit_argument(
&mut self,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
name: &'a Positioned<Name>,
2020-05-10 02:59:51 +00:00
value: &'a Positioned<Value>,
2020-03-09 12:39:46 +00:00
) {
self.0.exit_argument(ctx, name, value);
self.1.exit_argument(ctx, name, value);
2020-03-08 12:35:36 +00:00
}
2020-03-12 09:11:02 +00:00
fn enter_selection_set(
&mut self,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
selection_set: &'a Positioned<SelectionSet>,
2020-03-12 09:11:02 +00:00
) {
self.0.enter_selection_set(ctx, selection_set);
self.1.enter_selection_set(ctx, selection_set);
}
fn exit_selection_set(
&mut self,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
selection_set: &'a Positioned<SelectionSet>,
2020-03-12 09:11:02 +00:00
) {
self.0.exit_selection_set(ctx, selection_set);
self.1.exit_selection_set(ctx, selection_set);
}
2020-05-10 02:59:51 +00:00
fn enter_selection(
&mut self,
ctx: &mut VisitorContext<'a>,
selection: &'a Positioned<Selection>,
) {
2020-03-08 12:35:36 +00:00
self.0.enter_selection(ctx, selection);
self.1.enter_selection(ctx, selection);
}
2020-05-10 02:59:51 +00:00
fn exit_selection(
&mut self,
ctx: &mut VisitorContext<'a>,
selection: &'a Positioned<Selection>,
) {
2020-03-08 12:35:36 +00:00
self.0.exit_selection(ctx, selection);
self.1.exit_selection(ctx, selection);
}
2020-05-10 02:59:51 +00:00
fn enter_field(&mut self, ctx: &mut VisitorContext<'a>, field: &'a Positioned<Field>) {
2020-03-08 12:35:36 +00:00
self.0.enter_field(ctx, field);
self.1.enter_field(ctx, field);
}
2020-05-10 02:59:51 +00:00
fn exit_field(&mut self, ctx: &mut VisitorContext<'a>, field: &'a Positioned<Field>) {
2020-03-08 12:35:36 +00:00
self.0.exit_field(ctx, field);
self.1.exit_field(ctx, field);
}
fn enter_fragment_spread(
&mut self,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
fragment_spread: &'a Positioned<FragmentSpread>,
2020-03-08 12:35:36 +00:00
) {
self.0.enter_fragment_spread(ctx, fragment_spread);
self.1.enter_fragment_spread(ctx, fragment_spread);
}
fn exit_fragment_spread(
&mut self,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
fragment_spread: &'a Positioned<FragmentSpread>,
2020-03-08 12:35:36 +00:00
) {
self.0.exit_fragment_spread(ctx, fragment_spread);
self.1.exit_fragment_spread(ctx, fragment_spread);
}
fn enter_inline_fragment(
&mut self,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
inline_fragment: &'a Positioned<InlineFragment>,
2020-03-08 12:35:36 +00:00
) {
self.0.enter_inline_fragment(ctx, inline_fragment);
self.1.enter_inline_fragment(ctx, inline_fragment);
}
fn exit_inline_fragment(
&mut self,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
inline_fragment: &'a Positioned<InlineFragment>,
2020-03-08 12:35:36 +00:00
) {
self.0.exit_inline_fragment(ctx, inline_fragment);
self.1.exit_inline_fragment(ctx, inline_fragment);
}
}
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
pub(crate) fn visit<'a, V: Visitor<'a>>(
2020-09-08 08:30:29 +00:00
v: &mut V,
ctx: &mut VisitorContext<'a>,
doc: &'a ExecutableDocument,
) {
2020-03-08 12:35:36 +00:00
v.enter_document(ctx, doc);
for (name, fragment) in &doc.fragments {
ctx.with_type(
ctx.registry
.types
.get(fragment.node.type_condition.node.on.node.as_str()),
|ctx| visit_fragment_definition(v, ctx, name, fragment),
)
2020-03-08 12:35:36 +00:00
}
for (name, operation) in doc.operations.iter() {
visit_operation_definition(v, ctx, name, operation);
}
v.exit_document(ctx, doc);
2020-03-08 12:35:36 +00:00
}
fn visit_operation_definition<'a, V: Visitor<'a>>(
v: &mut V,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
name: Option<&'a Name>,
2020-05-10 02:59:51 +00:00
operation: &'a Positioned<OperationDefinition>,
2020-03-08 12:35:36 +00:00
) {
v.enter_operation_definition(ctx, name, operation);
2020-09-06 05:38:31 +00:00
let root_name = match &operation.node.ty {
OperationType::Query => Some(&*ctx.registry.query_type),
OperationType::Mutation => ctx.registry.mutation_type.as_deref(),
OperationType::Subscription => ctx.registry.subscription_type.as_deref(),
};
if let Some(root_name) = root_name {
ctx.with_type(Some(&ctx.registry.types[&*root_name]), |ctx| {
visit_variable_definitions(v, ctx, &operation.node.variable_definitions);
visit_directives(v, ctx, &operation.node.directives);
visit_selection_set(v, ctx, &operation.node.selection_set);
});
} else {
ctx.report_error(
vec![operation.pos],
// The only one with an irregular plural, "query", is always present
format!("Schema is not configured for {}s.", operation.node.ty),
);
2020-03-08 12:35:36 +00:00
}
v.exit_operation_definition(ctx, name, operation);
2020-03-08 12:35:36 +00:00
}
fn visit_selection_set<'a, V: Visitor<'a>>(
v: &mut V,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
selection_set: &'a Positioned<SelectionSet>,
2020-03-08 12:35:36 +00:00
) {
2020-09-06 05:38:31 +00:00
if !selection_set.node.items.is_empty() {
2020-03-25 07:07:16 +00:00
v.enter_selection_set(ctx, selection_set);
2020-09-06 05:38:31 +00:00
for selection in &selection_set.node.items {
2020-03-25 07:07:16 +00:00
visit_selection(v, ctx, selection);
}
v.exit_selection_set(ctx, selection_set);
2020-03-08 12:35:36 +00:00
}
}
fn visit_selection<'a, V: Visitor<'a>>(
v: &mut V,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
selection: &'a Positioned<Selection>,
2020-03-08 12:35:36 +00:00
) {
v.enter_selection(ctx, selection);
match &selection.node {
2020-03-09 04:08:50 +00:00
Selection::Field(field) => {
2020-09-06 05:38:31 +00:00
if field.node.name.node != "__typename" {
2020-04-05 08:00:26 +00:00
ctx.with_type(
ctx.current_type()
2020-09-06 05:38:31 +00:00
.and_then(|ty| ty.field_by_name(&field.node.name.node))
2020-04-05 08:00:26 +00:00
.and_then(|schema_field| {
2020-04-06 10:30:38 +00:00
ctx.registry.concrete_type_by_name(&schema_field.ty)
2020-04-05 08:00:26 +00:00
}),
|ctx| {
visit_field(v, ctx, field);
},
);
2020-03-09 04:08:50 +00:00
}
}
2020-03-08 12:35:36 +00:00
Selection::FragmentSpread(fragment_spread) => {
visit_fragment_spread(v, ctx, fragment_spread)
}
Selection::InlineFragment(inline_fragment) => {
2020-09-06 06:16:36 +00:00
if let Some(TypeCondition { on: name }) = &inline_fragment
.node
.type_condition
.as_ref()
.map(|c| &c.node)
{
ctx.with_type(ctx.registry.types.get(name.node.as_str()), |ctx| {
2020-04-05 08:00:26 +00:00
visit_inline_fragment(v, ctx, inline_fragment)
});
2020-03-09 04:08:50 +00:00
}
2020-03-08 12:35:36 +00:00
}
}
v.exit_selection(ctx, selection);
}
fn visit_field<'a, V: Visitor<'a>>(
v: &mut V,
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
field: &'a Positioned<Field>,
) {
2020-03-08 12:35:36 +00:00
v.enter_field(ctx, field);
2020-04-05 08:00:26 +00:00
2020-09-06 05:38:31 +00:00
for (name, value) in &field.node.arguments {
v.enter_argument(ctx, name, value);
2020-04-05 08:00:26 +00:00
let expected_ty = ctx
.parent_type()
2020-09-06 05:38:31 +00:00
.and_then(|ty| ty.field_by_name(&field.node.name.node))
.and_then(|schema_field| schema_field.args.get(&*name.node))
.map(|input_ty| MetaTypeName::create(&input_ty.ty));
2020-04-05 08:00:26 +00:00
ctx.with_input_type(expected_ty, |ctx| {
2020-09-06 05:38:31 +00:00
visit_input_value(v, ctx, field.pos, expected_ty, &value.node)
2020-04-05 08:00:26 +00:00
});
v.exit_argument(ctx, name, value);
2020-04-05 08:00:26 +00:00
}
2020-09-06 05:38:31 +00:00
visit_directives(v, ctx, &field.node.directives);
visit_selection_set(v, ctx, &field.node.selection_set);
2020-03-08 12:35:36 +00:00
v.exit_field(ctx, field);
}
2020-04-05 08:00:26 +00:00
fn visit_input_value<'a, V: Visitor<'a>>(
2020-03-08 12:35:36 +00:00
v: &mut V,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-03-09 12:39:46 +00:00
pos: Pos,
expected_ty: Option<MetaTypeName<'a>>,
2020-04-05 08:00:26 +00:00
value: &'a Value,
2020-03-08 12:35:36 +00:00
) {
2020-04-05 08:00:26 +00:00
v.enter_input_value(ctx, pos, &expected_ty, value);
match value {
Value::List(values) => {
if let Some(expected_ty) = expected_ty {
let elem_ty = expected_ty.unwrap_non_null();
if let MetaTypeName::List(expected_ty) = elem_ty {
2020-04-05 08:00:26 +00:00
values.iter().for_each(|value| {
visit_input_value(
v,
ctx,
pos,
Some(MetaTypeName::create(expected_ty)),
value,
)
2020-04-05 08:00:26 +00:00
});
}
}
}
Value::Object(values) => {
if let Some(expected_ty) = expected_ty {
let expected_ty = expected_ty.unwrap_non_null();
if let MetaTypeName::Named(expected_ty) = expected_ty {
2021-02-13 01:55:53 +00:00
if let Some(MetaType::InputObject { input_fields, .. }) = ctx
2020-04-05 08:00:26 +00:00
.registry
.types
.get(MetaTypeName::concrete_typename(expected_ty))
2020-04-05 08:00:26 +00:00
{
2021-02-13 01:55:53 +00:00
for (item_key, item_value) in values {
if let Some(input_value) = input_fields.get(item_key.as_str()) {
visit_input_value(
v,
ctx,
pos,
Some(MetaTypeName::create(&input_value.ty)),
item_value,
);
2020-04-05 08:00:26 +00:00
}
}
}
}
}
}
_ => {}
2020-03-08 12:35:36 +00:00
}
2020-04-05 08:00:26 +00:00
v.exit_input_value(ctx, pos, &expected_ty, value);
2020-03-08 12:35:36 +00:00
}
fn visit_variable_definitions<'a, V: Visitor<'a>>(
v: &mut V,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
variable_definitions: &'a [Positioned<VariableDefinition>],
2020-03-08 12:35:36 +00:00
) {
for d in variable_definitions {
v.enter_variable_definition(ctx, d);
v.exit_variable_definition(ctx, d);
}
}
fn visit_directives<'a, V: Visitor<'a>>(
v: &mut V,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
directives: &'a [Positioned<Directive>],
2020-03-08 12:35:36 +00:00
) {
for d in directives {
v.enter_directive(ctx, d);
2020-04-05 08:00:26 +00:00
let schema_directive = ctx.registry.directives.get(d.node.name.node.as_str());
2020-04-05 08:00:26 +00:00
2020-09-06 05:38:31 +00:00
for (name, value) in &d.node.arguments {
v.enter_argument(ctx, name, value);
2020-04-05 08:00:26 +00:00
let expected_ty = schema_directive
2020-09-06 05:38:31 +00:00
.and_then(|schema_directive| schema_directive.args.get(&*name.node))
.map(|input_ty| MetaTypeName::create(&input_ty.ty));
2020-04-05 08:00:26 +00:00
ctx.with_input_type(expected_ty, |ctx| {
2020-09-06 05:38:31 +00:00
visit_input_value(v, ctx, d.pos, expected_ty, &value.node)
2020-04-05 08:00:26 +00:00
});
v.exit_argument(ctx, name, value);
2020-04-05 08:00:26 +00:00
}
2020-03-08 12:35:36 +00:00
v.exit_directive(ctx, d);
}
}
fn visit_fragment_definition<'a, V: Visitor<'a>>(
v: &mut V,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
name: &'a Name,
2020-05-10 02:59:51 +00:00
fragment: &'a Positioned<FragmentDefinition>,
2020-03-08 12:35:36 +00:00
) {
2020-12-18 06:59:37 +00:00
if v.mode() == VisitMode::Normal {
v.enter_fragment_definition(ctx, name, fragment);
visit_directives(v, ctx, &fragment.node.directives);
visit_selection_set(v, ctx, &fragment.node.selection_set);
v.exit_fragment_definition(ctx, name, fragment);
}
2020-03-08 12:35:36 +00:00
}
fn visit_fragment_spread<'a, V: Visitor<'a>>(
v: &mut V,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
fragment_spread: &'a Positioned<FragmentSpread>,
2020-03-08 12:35:36 +00:00
) {
v.enter_fragment_spread(ctx, fragment_spread);
2020-09-06 05:38:31 +00:00
visit_directives(v, ctx, &fragment_spread.node.directives);
2020-12-18 06:59:37 +00:00
if v.mode() == VisitMode::Inline {
if let Some(fragment) = ctx
.fragments
.get(fragment_spread.node.fragment_name.node.as_str())
{
visit_selection_set(v, ctx, &fragment.node.selection_set);
}
}
2020-03-08 12:35:36 +00:00
v.exit_fragment_spread(ctx, fragment_spread);
}
fn visit_inline_fragment<'a, V: Visitor<'a>>(
v: &mut V,
2020-03-22 08:45:59 +00:00
ctx: &mut VisitorContext<'a>,
2020-05-10 02:59:51 +00:00
inline_fragment: &'a Positioned<InlineFragment>,
2020-03-08 12:35:36 +00:00
) {
v.enter_inline_fragment(ctx, inline_fragment);
2020-09-06 05:38:31 +00:00
visit_directives(v, ctx, &inline_fragment.node.directives);
visit_selection_set(v, ctx, &inline_fragment.node.selection_set);
2020-03-08 12:35:36 +00:00
v.exit_inline_fragment(ctx, inline_fragment);
}
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
#[derive(Debug, PartialEq)]
pub(crate) struct RuleError {
pub(crate) locations: Vec<Pos>,
pub(crate) message: String,
}
impl Display for RuleError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
for (idx, loc) in self.locations.iter().enumerate() {
if idx == 0 {
write!(f, "[")?;
} else {
write!(f, ", ")?;
}
write!(f, "{}:{}", loc.line, loc.column)?;
if idx == self.locations.len() - 1 {
write!(f, "] ")?;
}
}
write!(f, "{}", self.message)?;
Ok(())
}
}
impl From<RuleError> for ServerError {
fn from(e: RuleError) -> Self {
Self {
message: e.message,
locations: e.locations,
path: Vec::new(),
extensions: None,
}
}
}