use crate::executable::Operation;
#[cfg(doc)]
use crate::introspection;
use crate::parser::SourceMap;
use crate::parser::SourceSpan;
use crate::resolvers::input_coercion::InputCoercionError;
use crate::response::GraphQLError;
use crate::response::JsonMap;
use crate::validation::SuspectedValidationBug;
use crate::validation::Valid;
use crate::Schema;
pub fn coerce_variable_values(
schema: &Valid<Schema>,
operation: &Operation,
values: &JsonMap,
) -> Result<Valid<JsonMap>, RequestError> {
Ok(crate::resolvers::input_coercion::coerce_variable_values(
schema, operation, values,
)?)
}
#[derive(Debug, Clone)]
pub struct RequestError {
pub(crate) message: String,
pub(crate) location: Option<SourceSpan>,
pub(crate) is_suspected_validation_bug: bool,
}
impl From<InputCoercionError> for RequestError {
fn from(error: InputCoercionError) -> Self {
match error {
InputCoercionError::SuspectedValidationBug(SuspectedValidationBug {
message,
location,
}) => Self {
message,
location,
is_suspected_validation_bug: true,
},
InputCoercionError::ValueError { message, location } => Self {
message,
location,
is_suspected_validation_bug: false,
},
}
}
}
impl RequestError {
pub fn message(&self) -> impl std::fmt::Display + '_ {
&self.message
}
pub fn location(&self) -> Option<SourceSpan> {
self.location
}
pub fn to_graphql_error(&self, sources: &SourceMap) -> GraphQLError {
let mut error = GraphQLError::new(&self.message, self.location, sources);
if self.is_suspected_validation_bug {
error
.extensions
.insert("APOLLO_SUSPECTED_VALIDATION_BUG", true.into());
}
error
}
}