2020-09-10 08:39:43 +00:00
|
|
|
use crate::parser::types::UploadValue;
|
|
|
|
use crate::{http, Data, ParseRequestError, Value, Variables};
|
2020-09-10 04:49:08 +00:00
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::stream;
|
|
|
|
use futures::task::Poll;
|
|
|
|
use futures::{AsyncRead, AsyncReadExt, Stream};
|
|
|
|
use multer::{Constraints, Multipart, SizeLimit};
|
2020-03-31 03:19:18 +00:00
|
|
|
use std::any::Any;
|
2020-09-10 04:49:08 +00:00
|
|
|
use std::collections::HashMap;
|
2020-05-11 13:47:24 +00:00
|
|
|
use std::fs::File;
|
2020-09-10 08:39:43 +00:00
|
|
|
use std::io::{self, Seek, SeekFrom, Write};
|
|
|
|
use std::pin::Pin;
|
2020-03-17 09:26:59 +00:00
|
|
|
|
2020-09-10 08:39:43 +00:00
|
|
|
pub struct Request {
|
2020-09-10 04:49:08 +00:00
|
|
|
pub(crate) query: String,
|
|
|
|
pub(crate) operation_name: Option<String>,
|
|
|
|
pub(crate) variables: Variables,
|
|
|
|
pub(crate) ctx_data: Data,
|
|
|
|
}
|
|
|
|
|
2020-09-10 08:39:43 +00:00
|
|
|
impl Request {
|
|
|
|
/// Create a request object with query source.
|
2020-09-10 04:49:08 +00:00
|
|
|
pub fn new(query: impl Into<String>) -> Self {
|
|
|
|
Self {
|
|
|
|
query: query.into(),
|
|
|
|
operation_name: None,
|
|
|
|
variables: Variables::default(),
|
|
|
|
ctx_data: Data::default(),
|
|
|
|
}
|
2020-04-21 02:14:14 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 04:49:08 +00:00
|
|
|
pub fn new_with_http_request(request: http::GQLRequest) -> Self {
|
|
|
|
Self {
|
|
|
|
query: request.query,
|
|
|
|
operation_name: request.operation_name,
|
|
|
|
variables: request
|
|
|
|
.variables
|
|
|
|
.map(|value| Variables::parse_from_json(value))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
ctx_data: Data::default(),
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 01:53:17 +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-09-10 04:49:08 +00:00
|
|
|
Self {
|
2020-04-01 08:53:49 +00:00
|
|
|
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 {
|
2020-09-10 04:49:08 +00:00
|
|
|
Self { variables, ..self }
|
2020-04-01 08:53:49 +00:00
|
|
|
}
|
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 {
|
2020-09-10 04:49:08 +00:00
|
|
|
self.ctx_data.insert(data);
|
2020-04-01 08:53:49 +00:00
|
|
|
self
|
|
|
|
}
|
2020-03-17 09:26:59 +00:00
|
|
|
|
2020-04-11 09:36:05 +00:00
|
|
|
/// Set uploaded file path
|
2020-03-17 09:26:59 +00:00
|
|
|
pub fn set_upload(
|
|
|
|
&mut self,
|
|
|
|
var_path: &str,
|
2020-05-11 13:47:24 +00:00
|
|
|
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-09-10 04:49:08 +00:00
|
|
|
}
|
2020-03-17 09:26:59 +00:00
|
|
|
|
2020-09-10 08:39:43 +00:00
|
|
|
impl<T: Into<String>> From<T> for Request {
|
2020-09-10 04:49:08 +00:00
|
|
|
fn from(query: T) -> Self {
|
|
|
|
Self::new(query)
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 08:53:49 +00:00
|
|
|
|
2020-09-10 08:39:43 +00:00
|
|
|
impl From<http::GQLRequest> for Request {
|
2020-09-10 04:49:08 +00:00
|
|
|
fn from(request: http::GQLRequest) -> Self {
|
|
|
|
Self::new_with_http_request(request)
|
|
|
|
}
|
|
|
|
}
|