2020-03-17 09:26:59 +00:00
|
|
|
use crate::context::Data;
|
2020-03-26 03:34:28 +00:00
|
|
|
use crate::extensions::BoxExtension;
|
2020-03-24 10:54:22 +00:00
|
|
|
use crate::registry::{CacheControl, Registry};
|
2020-03-17 09:26:59 +00:00
|
|
|
use crate::types::QueryRoot;
|
2020-03-25 07:07:16 +00:00
|
|
|
use crate::validation::{check_rules, CheckResult};
|
|
|
|
use crate::{ContextBase, OutputValueType, Result, Schema};
|
2020-03-19 09:20:12 +00:00
|
|
|
use crate::{ObjectType, QueryError, QueryParseError, Variables};
|
2020-03-17 09:26:59 +00:00
|
|
|
use bytes::Bytes;
|
|
|
|
use graphql_parser::parse_query;
|
|
|
|
use graphql_parser::query::{
|
2020-03-24 10:54:22 +00:00
|
|
|
Definition, FragmentDefinition, OperationDefinition, SelectionSet, VariableDefinition,
|
2020-03-17 09:26:59 +00:00
|
|
|
};
|
|
|
|
use std::collections::HashMap;
|
2020-03-26 03:34:28 +00:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
2020-03-17 09:26:59 +00:00
|
|
|
|
|
|
|
enum Root<'a, Query, Mutation> {
|
|
|
|
Query(&'a QueryRoot<Query>),
|
|
|
|
Mutation(&'a Mutation),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Query builder
|
2020-03-25 07:07:16 +00:00
|
|
|
pub struct QueryBuilder<'a, Query, Mutation, Subscription> {
|
|
|
|
pub(crate) schema: &'a Schema<Query, Mutation, Subscription>,
|
2020-03-26 03:34:28 +00:00
|
|
|
pub(crate) extensions: Vec<BoxExtension>,
|
2020-03-17 09:26:59 +00:00
|
|
|
pub(crate) source: &'a str,
|
|
|
|
pub(crate) operation_name: Option<&'a str>,
|
|
|
|
pub(crate) variables: Option<Variables>,
|
|
|
|
pub(crate) data: &'a Data,
|
|
|
|
}
|
|
|
|
|
2020-03-25 07:07:16 +00:00
|
|
|
impl<'a, Query, Mutation, Subscription> QueryBuilder<'a, Query, Mutation, Subscription> {
|
2020-03-17 09:26:59 +00:00
|
|
|
/// Specify the operation name.
|
|
|
|
pub fn operator_name(self, name: &'a str) -> Self {
|
|
|
|
QueryBuilder {
|
|
|
|
operation_name: Some(name),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Specify the variables.
|
|
|
|
pub fn variables(self, vars: Variables) -> Self {
|
|
|
|
QueryBuilder {
|
|
|
|
variables: Some(vars),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepare query
|
|
|
|
pub fn prepare(self) -> Result<PreparedQuery<'a, Query, Mutation>> {
|
2020-03-26 03:34:28 +00:00
|
|
|
self.extensions
|
|
|
|
.iter()
|
|
|
|
.for_each(|e| e.parse_start(self.source));
|
2020-03-17 09:26:59 +00:00
|
|
|
let document = parse_query(self.source).map_err(|err| QueryParseError(err.to_string()))?;
|
2020-03-26 03:34:28 +00:00
|
|
|
self.extensions.iter().for_each(|e| e.parse_end());
|
|
|
|
|
|
|
|
self.extensions.iter().for_each(|e| e.validation_start());
|
2020-03-25 07:07:16 +00:00
|
|
|
let CheckResult {
|
|
|
|
cache_control,
|
|
|
|
complexity,
|
|
|
|
depth,
|
|
|
|
} = check_rules(&self.schema.registry, &document)?;
|
2020-03-26 03:34:28 +00:00
|
|
|
self.extensions.iter().for_each(|e| e.validation_end());
|
2020-03-25 07:07:16 +00:00
|
|
|
|
|
|
|
if let Some(limit_complexity) = self.schema.complexity {
|
|
|
|
if complexity > limit_complexity {
|
|
|
|
return Err(QueryError::TooComplex.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(limit_depth) = self.schema.depth {
|
|
|
|
if depth > limit_depth {
|
|
|
|
return Err(QueryError::TooDeep.into());
|
|
|
|
}
|
|
|
|
}
|
2020-03-22 08:45:59 +00:00
|
|
|
|
2020-03-17 09:26:59 +00:00
|
|
|
let mut fragments = HashMap::new();
|
|
|
|
let mut selection_set = None;
|
|
|
|
let mut variable_definitions = None;
|
|
|
|
let mut root = None;
|
|
|
|
|
|
|
|
for definition in document.definitions {
|
|
|
|
match definition {
|
|
|
|
Definition::Operation(operation_definition) => match operation_definition {
|
|
|
|
OperationDefinition::SelectionSet(s) => {
|
|
|
|
selection_set = Some(s);
|
2020-03-25 07:07:16 +00:00
|
|
|
root = Some(Root::Query(&self.schema.query));
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
OperationDefinition::Query(query)
|
|
|
|
if query.name.is_none() || query.name.as_deref() == self.operation_name =>
|
|
|
|
{
|
|
|
|
selection_set = Some(query.selection_set);
|
|
|
|
variable_definitions = Some(query.variable_definitions);
|
2020-03-25 07:07:16 +00:00
|
|
|
root = Some(Root::Query(&self.schema.query));
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
OperationDefinition::Mutation(mutation)
|
|
|
|
if mutation.name.is_none()
|
|
|
|
|| mutation.name.as_deref() == self.operation_name =>
|
|
|
|
{
|
|
|
|
selection_set = Some(mutation.selection_set);
|
|
|
|
variable_definitions = Some(mutation.variable_definitions);
|
2020-03-25 07:07:16 +00:00
|
|
|
root = Some(Root::Mutation(&self.schema.mutation));
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
OperationDefinition::Subscription(subscription)
|
|
|
|
if subscription.name.is_none()
|
|
|
|
|| subscription.name.as_deref() == self.operation_name =>
|
|
|
|
{
|
|
|
|
return Err(QueryError::NotSupported.into());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
Definition::Fragment(fragment) => {
|
|
|
|
fragments.insert(fragment.name.clone(), fragment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(PreparedQuery {
|
2020-03-26 03:34:28 +00:00
|
|
|
extensions: self.extensions,
|
2020-03-25 07:07:16 +00:00
|
|
|
registry: &self.schema.registry,
|
2020-03-17 09:26:59 +00:00
|
|
|
variables: self.variables.unwrap_or_default(),
|
|
|
|
data: self.data,
|
|
|
|
fragments,
|
|
|
|
selection_set: selection_set.ok_or({
|
|
|
|
if let Some(name) = self.operation_name {
|
|
|
|
QueryError::UnknownOperationNamed {
|
|
|
|
name: name.to_string(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QueryError::MissingOperation
|
|
|
|
}
|
|
|
|
})?,
|
|
|
|
root: root.unwrap(),
|
|
|
|
variable_definitions,
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control,
|
2020-03-17 09:26:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute the query.
|
2020-03-26 03:34:28 +00:00
|
|
|
pub async fn execute(self) -> Result<QueryResult>
|
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
|
|
|
{
|
|
|
|
self.prepare()?.execute().await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 03:34:28 +00:00
|
|
|
/// Query result
|
|
|
|
pub struct QueryResult {
|
|
|
|
/// Data of query result
|
|
|
|
pub data: serde_json::Value,
|
|
|
|
|
|
|
|
/// Extensions result
|
|
|
|
pub extensions: Option<serde_json::Value>,
|
|
|
|
}
|
|
|
|
|
2020-03-20 03:56:08 +00:00
|
|
|
/// Prepared query object
|
2020-03-17 09:26:59 +00:00
|
|
|
pub struct PreparedQuery<'a, Query, Mutation> {
|
|
|
|
root: Root<'a, Query, Mutation>,
|
2020-03-26 03:34:28 +00:00
|
|
|
extensions: Vec<BoxExtension>,
|
2020-03-17 09:26:59 +00:00
|
|
|
registry: &'a Registry,
|
|
|
|
variables: Variables,
|
|
|
|
data: &'a Data,
|
|
|
|
fragments: HashMap<String, FragmentDefinition>,
|
|
|
|
selection_set: SelectionSet,
|
|
|
|
variable_definitions: Option<Vec<VariableDefinition>>,
|
2020-03-22 08:45:59 +00:00
|
|
|
cache_control: CacheControl,
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, Query, Mutation> PreparedQuery<'a, Query, Mutation> {
|
|
|
|
/// Detects whether any parameter contains the Upload type
|
|
|
|
pub fn is_upload(&self) -> bool {
|
|
|
|
if let Some(variable_definitions) = &self.variable_definitions {
|
|
|
|
for d in variable_definitions {
|
|
|
|
if let Some(ty) = self.registry.basic_type_by_parsed_type(&d.var_type) {
|
|
|
|
if ty.name() == "Upload" {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set upload files
|
|
|
|
pub fn set_upload(
|
|
|
|
&mut self,
|
|
|
|
var_path: &str,
|
|
|
|
filename: &str,
|
|
|
|
content_type: Option<&str>,
|
|
|
|
content: Bytes,
|
|
|
|
) {
|
|
|
|
self.variables
|
|
|
|
.set_upload(var_path, filename, content_type, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute the query.
|
2020-03-26 03:34:28 +00:00
|
|
|
pub async fn execute(self) -> Result<QueryResult>
|
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-03-26 03:34:28 +00:00
|
|
|
let resolve_id = AtomicUsize::default();
|
2020-03-17 09:26:59 +00:00
|
|
|
let ctx = ContextBase {
|
2020-03-26 10:30:29 +00:00
|
|
|
path_node: None,
|
2020-03-26 03:34:28 +00:00
|
|
|
resolve_id: &resolve_id,
|
|
|
|
extensions: &self.extensions,
|
2020-03-17 09:26:59 +00:00
|
|
|
item: &self.selection_set,
|
|
|
|
variables: &self.variables,
|
|
|
|
variable_definitions: self.variable_definitions.as_deref(),
|
2020-03-21 01:32:13 +00:00
|
|
|
registry: self.registry,
|
2020-03-17 09:26:59 +00:00
|
|
|
data: self.data,
|
|
|
|
fragments: &self.fragments,
|
|
|
|
};
|
|
|
|
|
2020-03-26 03:34:28 +00:00
|
|
|
self.extensions.iter().for_each(|e| e.execution_start());
|
|
|
|
let data = match self.root {
|
2020-03-25 03:39:28 +00:00
|
|
|
Root::Query(query) => OutputValueType::resolve(query, &ctx).await,
|
|
|
|
Root::Mutation(mutation) => OutputValueType::resolve(mutation, &ctx).await,
|
2020-03-26 03:34:28 +00:00
|
|
|
}?;
|
|
|
|
|
|
|
|
self.extensions.iter().for_each(|e| e.execution_end());
|
|
|
|
|
|
|
|
let res = QueryResult {
|
|
|
|
data,
|
|
|
|
extensions: if !self.extensions.is_empty() {
|
|
|
|
Some(
|
|
|
|
self.extensions
|
|
|
|
.iter()
|
|
|
|
.map(|e| (e.name().to_string(), e.result()))
|
|
|
|
.collect::<serde_json::Map<_, _>>()
|
|
|
|
.into(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
};
|
|
|
|
Ok(res)
|
2020-03-17 09:26:59 +00:00
|
|
|
}
|
2020-03-22 08:45:59 +00:00
|
|
|
|
|
|
|
/// Get cache control value
|
|
|
|
pub fn cache_control(&self) -> CacheControl {
|
|
|
|
self.cache_control
|
|
|
|
}
|
|
|
|
}
|