1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use crate::error::RequestError;
use crate::http::multipart::{Multipart, PartData};
use crate::http::{GQLRequest, IntoQueryBuilder};
use crate::{Error, ObjectType, QueryBuilder, Result, Schema, SubscriptionType};
use futures::{AsyncRead, AsyncReadExt};
use mime::Mime;
use std::collections::HashMap;
#[allow(missing_docs)]
pub trait GQLHttpRequest {
type Body: AsyncRead + Send + Unpin;
fn content_type(&self) -> Option<&str>;
fn into_body(self) -> Self::Body;
}
#[async_trait::async_trait]
impl<R: GQLHttpRequest + Send + Sync> IntoQueryBuilder for R {
async fn into_query_builder<Query, Mutation, Subscription>(
mut self,
schema: &Schema<Query, Mutation, Subscription>,
) -> Result<QueryBuilder<Query, Mutation, Subscription>>
where
Query: ObjectType + Send + Sync + 'static,
Mutation: ObjectType + Send + Sync + 'static,
Subscription: SubscriptionType + Send + Sync + 'static,
{
if let Some(boundary) = self
.content_type()
.and_then(|value| value.parse::<Mime>().ok())
.and_then(|ct| {
if ct.essence_str() == mime::MULTIPART_FORM_DATA {
ct.get_param("boundary")
.map(|boundary| boundary.to_string())
} else {
None
}
})
{
let mut multipart = Multipart::parse(self.into_body(), boundary.as_str())
.await
.map_err(RequestError::InvalidMultipart)?;
let gql_request: GQLRequest = {
let part = multipart
.remove("operations")
.ok_or_else(|| Error::Request(RequestError::MissingOperatorsPart))?;
let reader = part
.create_reader()
.map_err(|err| Error::Request(RequestError::PartData(err)))?;
serde_json::from_reader(reader).map_err(RequestError::InvalidRequest)?
};
let mut map: HashMap<String, Vec<String>> = {
let part = multipart
.remove("map")
.ok_or_else(|| Error::Request(RequestError::MissingMapPart))?;
let reader = part
.create_reader()
.map_err(|err| Error::Request(RequestError::PartData(err)))?;
serde_json::from_reader(reader).map_err(RequestError::InvalidFilesMap)?
};
let mut builder = gql_request.into_query_builder(schema).await?;
if !builder.is_upload() {
return Err(RequestError::NotUpload.into());
}
for part in &multipart.parts {
if let Some(name) = &part.name {
if let Some(var_paths) = map.remove(name) {
for var_path in var_paths {
if let (Some(filename), PartData::File(path)) =
(&part.filename, &part.data)
{
builder.set_upload(
&var_path,
&filename,
part.content_type.as_deref(),
path,
);
}
}
}
}
}
if !map.is_empty() {
return Err(RequestError::MissingFiles.into());
}
if let Some(temp_dir) = multipart.temp_dir {
builder.set_files_holder(temp_dir);
}
Ok(builder)
} else {
let mut data = Vec::new();
self.into_body()
.read_to_end(&mut data)
.await
.map_err(RequestError::Io)?;
let gql_request: GQLRequest =
serde_json::from_slice(&data).map_err(RequestError::InvalidRequest)?;
gql_request.into_query_builder(schema).await
}
}
}