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
use crate::parser::types::UploadValue;
use crate::{Data, Value, Variables};
use std::any::Any;
use std::fs::File;
pub struct Request {
pub(crate) query: String,
pub(crate) operation_name: Option<String>,
pub(crate) variables: Variables,
pub(crate) ctx_data: Data,
}
impl Request {
pub fn new(query: impl Into<String>) -> Self {
Self {
query: query.into(),
operation_name: None,
variables: Variables::default(),
ctx_data: Data::default(),
}
}
pub fn operation_name<T: Into<String>>(self, name: T) -> Self {
Self {
operation_name: Some(name.into()),
..self
}
}
pub fn variables(self, variables: Variables) -> Self {
Self { variables, ..self }
}
pub fn data<D: Any + Send + Sync>(mut self, data: D) -> Self {
self.ctx_data.insert(data);
self
}
pub fn set_upload(
&mut self,
var_path: &str,
filename: String,
content_type: Option<String>,
content: File,
) {
let variable = match self.variables.variable_path(var_path) {
Some(variable) => variable,
None => return,
};
*variable = Value::Upload(UploadValue {
filename,
content_type,
content,
});
}
}
impl<T: Into<String>> From<T> for Request {
fn from(query: T) -> Self {
Self::new(query)
}
}