async-graphql/src/query.rs

287 lines
9.0 KiB
Rust
Raw Normal View History

2020-04-28 07:01:19 +00:00
use crate::context::{Data, ResolveId};
2020-04-14 01:53:17 +00:00
use crate::error::ParseRequestError;
2020-04-03 14:19:15 +00:00
use crate::mutation_resolver::do_mutation_resolve;
use crate::parser::ast::{
Definition, Document, OperationDefinition, SelectionSet, VariableDefinition,
};
use crate::parser::parse_query;
2020-04-01 08:53:49 +00:00
use crate::registry::CacheControl;
2020-04-14 01:53:17 +00:00
use crate::validation::{check_rules, CheckResult};
use crate::{
2020-05-10 02:59:51 +00:00
do_resolve, ContextBase, Error, ObjectType, Pos, Positioned, QueryError, Result, Schema,
Variables,
2020-03-17 09:26:59 +00:00
};
2020-04-14 01:53:17 +00:00
use itertools::Itertools;
2020-03-31 03:19:18 +00:00
use std::any::Any;
2020-03-17 09:26:59 +00:00
use std::collections::HashMap;
2020-04-21 02:14:14 +00:00
use std::path::{Path, PathBuf};
2020-03-26 03:34:28 +00:00
use std::sync::atomic::AtomicUsize;
use tempdir::TempDir;
2020-03-17 09:26:59 +00:00
2020-04-21 02:14:14 +00:00
/// IntoQueryBuilder options
#[derive(Default, Clone)]
pub struct IntoQueryBuilderOpts {
/// A temporary path to store the contents of all files.
///
/// If None, the system temporary path is used.
pub temp_dir: Option<PathBuf>,
/// Maximum file size.
pub max_file_size: Option<usize>,
/// Maximum number of files.
pub max_num_files: Option<usize>,
}
2020-04-14 01:53:17 +00:00
#[allow(missing_docs)]
#[async_trait::async_trait]
2020-04-21 02:14:14 +00:00
pub trait IntoQueryBuilder: Sized {
async fn into_query_builder(self) -> std::result::Result<QueryBuilder, ParseRequestError> {
self.into_query_builder_opts(&Default::default()).await
}
async fn into_query_builder_opts(
self,
opts: &IntoQueryBuilderOpts,
) -> std::result::Result<QueryBuilder, ParseRequestError>;
2020-04-14 01:53:17 +00:00
}
/// Query response
2020-05-01 23:57:34 +00:00
#[derive(Debug)]
pub struct QueryResponse {
2020-04-01 08:53:49 +00:00
/// Data of query result
pub data: serde_json::Value,
/// Extensions result
pub extensions: Option<serde_json::Map<String, serde_json::Value>>,
2020-04-14 01:53:17 +00:00
/// Cache control value
pub cache_control: CacheControl,
2020-03-17 09:26:59 +00:00
}
/// Query builder
2020-04-14 01:53:17 +00:00
pub struct QueryBuilder {
pub(crate) query_source: String,
2020-04-01 08:53:49 +00:00
pub(crate) operation_name: Option<String>,
pub(crate) variables: Variables,
2020-03-31 03:19:18 +00:00
pub(crate) ctx_data: Option<Data>,
pub(crate) files_holder: Option<TempDir>,
2020-03-17 09:26:59 +00:00
}
2020-04-14 01:53:17 +00:00
impl QueryBuilder {
/// Create query builder with query source.
pub fn new<T: Into<String>>(query_source: T) -> QueryBuilder {
QueryBuilder {
query_source: query_source.into(),
operation_name: None,
variables: Default::default(),
ctx_data: None,
files_holder: None,
2020-03-17 09:26:59 +00:00
}
}
2020-04-01 08:53:49 +00:00
/// Specify the operation name.
pub fn operator_name<T: Into<String>>(self, name: T) -> Self {
QueryBuilder {
operation_name: Some(name.into()),
..self
}
2020-03-17 09:26:59 +00:00
}
2020-04-01 08:53:49 +00:00
/// Specify the variables.
pub fn variables(self, variables: Variables) -> Self {
QueryBuilder { variables, ..self }
}
2020-03-26 03:34:28 +00:00
2020-04-01 08:53:49 +00:00
/// Add a context data that can be accessed in the `Context`, you access it with `Context::data`.
2020-04-22 02:35:07 +00:00
///
/// **This data is only valid for this query**
2020-04-01 08:53:49 +00:00
pub fn data<D: Any + Send + Sync>(mut self, data: D) -> Self {
if let Some(ctx_data) = &mut self.ctx_data {
ctx_data.insert(data);
} else {
let mut ctx_data = Data::default();
ctx_data.insert(data);
self.ctx_data = Some(ctx_data);
}
self
}
2020-03-17 09:26:59 +00:00
/// Set file holder
pub fn set_files_holder(&mut self, files_holder: TempDir) {
self.files_holder = Some(files_holder);
}
/// Set uploaded file path
2020-03-17 09:26:59 +00:00
pub fn set_upload(
&mut self,
var_path: &str,
filename: &str,
content_type: Option<&str>,
path: &Path,
2020-03-17 09:26:59 +00:00
) {
self.variables
.set_upload(var_path, filename, content_type, path);
2020-03-17 09:26:59 +00:00
}
/// Execute the query.
2020-04-14 01:53:17 +00:00
pub async fn execute<Query, Mutation, Subscription>(
self,
schema: &Schema<Query, Mutation, Subscription>,
) -> Result<QueryResponse>
2020-03-17 09:26:59 +00:00
where
2020-03-19 09:20:12 +00:00
Query: ObjectType + Send + Sync,
Mutation: ObjectType + Send + Sync,
2020-03-17 09:26:59 +00:00
{
2020-04-14 01:53:17 +00:00
// create extension instances
let extensions = schema
.0
.extensions
.iter()
.map(|factory| factory())
.collect_vec();
// parse query source
extensions
.iter()
.for_each(|e| e.parse_start(&self.query_source));
let document = parse_query(&self.query_source).map_err(Into::<Error>::into)?;
extensions.iter().for_each(|e| e.parse_end());
// check rules
extensions.iter().for_each(|e| e.validation_start());
let CheckResult {
cache_control,
complexity,
depth,
} = check_rules(&schema.0.registry, &document, schema.0.validation_mode)?;
extensions.iter().for_each(|e| e.validation_end());
// check limit
if let Some(limit_complexity) = schema.0.complexity {
if complexity > limit_complexity {
return Err(QueryError::TooComplex.into_error(Pos::default()));
}
}
if let Some(limit_depth) = schema.0.depth {
if depth > limit_depth {
return Err(QueryError::TooDeep.into_error(Pos::default()));
}
}
// execute
2020-04-28 07:01:19 +00:00
let inc_resolve_id = AtomicUsize::default();
2020-04-01 08:53:49 +00:00
let mut fragments = HashMap::new();
let (selection_set, variable_definitions, is_query) =
2020-04-14 01:53:17 +00:00
current_operation(&document, self.operation_name.as_deref()).ok_or_else(|| {
Error::Query {
pos: Pos::default(),
path: None,
err: QueryError::MissingOperation,
}
})?;
2020-04-01 08:53:49 +00:00
2020-04-14 01:53:17 +00:00
for definition in &document.definitions {
if let Definition::Fragment(fragment) = &definition.node {
fragments.insert(fragment.name.clone_inner(), fragment.clone_inner());
2020-04-01 08:53:49 +00:00
}
}
2020-03-17 09:26:59 +00:00
let ctx = ContextBase {
2020-03-26 10:30:29 +00:00
path_node: None,
2020-04-28 07:01:19 +00:00
resolve_id: ResolveId::root(),
inc_resolve_id: &inc_resolve_id,
2020-04-14 01:53:17 +00:00
extensions: &extensions,
2020-04-01 08:53:49 +00:00
item: selection_set,
2020-03-17 09:26:59 +00:00
variables: &self.variables,
2020-04-01 08:53:49 +00:00
variable_definitions,
2020-04-14 01:53:17 +00:00
registry: &schema.0.registry,
data: &schema.0.data,
2020-03-31 03:19:18 +00:00
ctx_data: self.ctx_data.as_ref(),
2020-04-01 08:53:49 +00:00
fragments: &fragments,
2020-03-17 09:26:59 +00:00
};
2020-04-14 01:53:17 +00:00
extensions.iter().for_each(|e| e.execution_start());
2020-04-01 08:53:49 +00:00
let data = if is_query {
2020-04-14 01:53:17 +00:00
do_resolve(&ctx, &schema.0.query).await?
2020-04-01 08:53:49 +00:00
} else {
2020-04-14 01:53:17 +00:00
do_mutation_resolve(&ctx, &schema.0.mutation).await?
2020-04-01 08:53:49 +00:00
};
2020-04-14 01:53:17 +00:00
extensions.iter().for_each(|e| e.execution_end());
2020-03-26 03:34:28 +00:00
let res = QueryResponse {
2020-03-26 03:34:28 +00:00
data,
2020-04-14 01:53:17 +00:00
extensions: if !extensions.is_empty() {
2020-03-26 03:34:28 +00:00
Some(
2020-04-14 01:53:17 +00:00
extensions
2020-03-26 03:34:28 +00:00
.iter()
2020-04-28 07:01:19 +00:00
.filter_map(|e| {
if let Some(name) = e.name() {
e.result().map(|res| (name.to_string(), res))
} else {
None
}
})
2020-04-01 08:53:49 +00:00
.collect::<serde_json::Map<_, _>>(),
2020-03-26 03:34:28 +00:00
)
} else {
None
},
2020-04-14 01:53:17 +00:00
cache_control,
2020-03-26 03:34:28 +00:00
};
Ok(res)
2020-03-17 09:26:59 +00:00
}
2020-04-14 01:53:17 +00:00
}
2020-03-22 08:45:59 +00:00
#[allow(clippy::type_complexity)]
2020-04-14 01:53:17 +00:00
fn current_operation<'a>(
document: &'a Document,
operation_name: Option<&str>,
) -> Option<(
2020-05-10 02:59:51 +00:00
&'a Positioned<SelectionSet>,
&'a [Positioned<VariableDefinition>],
bool,
)> {
2020-04-14 01:53:17 +00:00
for definition in &document.definitions {
match &definition.node {
Definition::Operation(operation_definition) => match &operation_definition.node {
2020-04-14 01:53:17 +00:00
OperationDefinition::SelectionSet(s) => {
return Some((s, &[], true));
}
OperationDefinition::Query(query)
if query.name.is_none()
|| operation_name.is_none()
|| query.name.as_ref().map(|name| name.as_str())
== operation_name.as_deref() =>
2020-04-14 01:53:17 +00:00
{
return Some((&query.selection_set, &query.variable_definitions, true));
}
OperationDefinition::Mutation(mutation)
if mutation.name.is_none()
|| operation_name.is_none()
|| mutation.name.as_ref().map(|name| name.as_str())
== operation_name.as_deref() =>
2020-04-14 01:53:17 +00:00
{
return Some((
&mutation.selection_set,
&mutation.variable_definitions,
false,
));
}
OperationDefinition::Subscription(subscription)
if subscription.name.is_none()
|| operation_name.is_none()
|| subscription.name.as_ref().map(|name| name.as_str())
== operation_name.as_deref() =>
2020-04-14 01:53:17 +00:00
{
return None;
}
_ => {}
},
Definition::Fragment(_) => {}
}
2020-03-22 08:45:59 +00:00
}
2020-04-14 01:53:17 +00:00
None
2020-03-22 08:45:59 +00:00
}