async-graphql/src/query.rs

209 lines
6.3 KiB
Rust
Raw Normal View History

2020-07-31 02:10:03 +00:00
use crate::context::{Data, ResolveId};
2020-04-14 01:53:17 +00:00
use crate::error::ParseRequestError;
use crate::extensions::{BoxExtension, ErrorLogger, Extension};
2020-04-03 14:19:15 +00:00
use crate::mutation_resolver::do_mutation_resolve;
use crate::parser::types::{OperationType, UploadValue};
2020-04-01 08:53:49 +00:00
use crate::registry::CacheControl;
use crate::{
do_resolve, ContextBase, Error, ObjectType, Pos, QueryEnv, QueryError, Result, Schema,
2020-09-08 08:30:29 +00:00
SubscriptionType, Value, Variables,
2020-03-17 09:26:59 +00:00
};
2020-03-31 03:19:18 +00:00
use std::any::Any;
use std::fs::File;
2020-03-26 03:34:28 +00:00
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
2020-03-17 09:26:59 +00:00
2020-04-21 02:14:14 +00:00
/// IntoQueryBuilder options
#[derive(Default, Clone)]
pub struct IntoQueryBuilderOpts {
/// 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
2020-05-22 03:58:49 +00:00
pub extensions: Option<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>,
extensions: Vec<Box<dyn Fn() -> BoxExtension + Send + Sync>>,
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,
extensions: Default::default(),
2020-03-17 09:26:59 +00:00
}
}
2020-04-01 08:53:49 +00:00
/// Specify the operation name.
2020-06-22 07:59:53 +00:00
pub fn operation_name<T: Into<String>>(self, name: T) -> Self {
2020-04-01 08:53:49 +00:00
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
/// Add an extension
pub fn extension<F: Fn() -> E + Send + Sync + 'static, E: Extension>(
mut self,
extension_factory: F,
) -> Self {
self.extensions
.push(Box::new(move || Box::new(extension_factory())));
self
}
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 uploaded file path
2020-03-17 09:26:59 +00:00
pub fn set_upload(
&mut self,
var_path: &str,
filename: String,
content_type: Option<String>,
content: File,
2020-03-17 09:26:59 +00:00
) {
2020-09-06 05:38:31 +00:00
let variable = match self.variables.variable_path(var_path) {
Some(variable) => variable,
None => return,
};
*variable = Value::Upload(UploadValue {
filename,
content_type,
content,
});
2020-03-17 09:26:59 +00:00
}
2020-07-31 02:10:03 +00:00
/// Execute the query, always return a complete result.
pub async fn execute<Query, Mutation, Subscription>(
self,
schema: &Schema<Query, Mutation, Subscription>,
2020-07-31 02:10:03 +00:00
) -> Result<QueryResponse>
where
Query: ObjectType + Send + Sync + 'static,
Mutation: ObjectType + Send + Sync + 'static,
Subscription: SubscriptionType + Send + Sync + 'static,
2020-03-17 09:26:59 +00:00
{
2020-09-06 05:38:31 +00:00
let (document, cache_control, extensions) =
schema.prepare_query(&self.query_source, &self.variables, &self.extensions)?;
2020-04-14 01:53:17 +00:00
// execute
2020-04-28 07:01:19 +00:00
let inc_resolve_id = AtomicUsize::default();
let document = match document.into_data(self.operation_name.as_deref()) {
2020-09-06 05:38:31 +00:00
Some(document) => document,
2020-09-06 06:16:36 +00:00
None => {
return if let Some(operation_name) = self.operation_name {
Err(Error::Query {
pos: Pos::default(),
path: None,
err: QueryError::UnknownOperationNamed {
name: operation_name,
},
})
} else {
Err(Error::Query {
pos: Pos::default(),
path: None,
err: QueryError::MissingOperation,
})
}
.log_error(&extensions)
}
2020-09-06 05:38:31 +00:00
};
2020-04-01 08:53:49 +00:00
let env = QueryEnv::new(
2020-05-22 03:58:49 +00:00
extensions,
self.variables,
document,
Arc::new(self.ctx_data.unwrap_or_default()),
);
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-09-06 05:38:31 +00:00
item: &env.document.operation.node.selection_set,
schema_env: &schema.env,
query_env: &env,
2020-03-17 09:26:59 +00:00
};
env.extensions.lock().execution_start();
2020-09-06 05:38:31 +00:00
let data = match &env.document.operation.node.ty {
OperationType::Query => do_resolve(&ctx, &schema.query).await?,
OperationType::Mutation => do_mutation_resolve(&ctx, &schema.mutation).await?,
OperationType::Subscription => {
return Err(Error::Query {
pos: Pos::default(),
path: None,
err: QueryError::NotSupported,
})
}
2020-04-01 08:53:49 +00:00
};
env.extensions.lock().execution_end();
2020-07-31 02:10:03 +00:00
let resp = QueryResponse {
2020-03-26 03:34:28 +00:00
data,
extensions: env.extensions.lock().result(),
2020-04-14 01:53:17 +00:00
cache_control,
2020-03-26 03:34:28 +00:00
};
2020-07-31 02:10:03 +00:00
Ok(resp)
2020-03-17 09:26:59 +00:00
}
/// Get query source
#[inline]
pub fn query_source(&self) -> &str {
&self.query_source
}
2020-04-14 01:53:17 +00:00
}