2020-09-06 05:38:31 +00:00
|
|
|
use crate::parser::types::UploadValue;
|
2020-09-06 06:16:36 +00:00
|
|
|
use crate::{registry, InputValueError, InputValueResult, InputValueType, Type, Value};
|
2020-03-14 03:46:20 +00:00
|
|
|
use std::borrow::Cow;
|
2020-05-11 09:13:50 +00:00
|
|
|
use std::io::Read;
|
2020-03-14 03:46:20 +00:00
|
|
|
|
2020-03-30 14:02:43 +00:00
|
|
|
/// Uploaded file
|
2020-03-14 03:46:20 +00:00
|
|
|
///
|
2020-03-30 14:02:43 +00:00
|
|
|
/// **Reference:** <https://github.com/jaydenseric/graphql-multipart-request-spec>
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Graphql supports file uploads via `multipart/form-data`.
|
|
|
|
/// Enable this feature by accepting an argument of type `Upload` (single file) or
|
|
|
|
/// `Vec<Upload>` (multiple files) in your mutation like in the example blow.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// # Example
|
2020-04-28 07:41:31 +00:00
|
|
|
/// *[Full Example](<https://github.com/async-graphql/examples/blob/master/models/files/src/lib.rs>)*
|
2020-03-30 14:02:43 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2020-09-13 03:41:15 +00:00
|
|
|
/// use async_graphql::*;
|
2020-03-30 14:02:43 +00:00
|
|
|
///
|
|
|
|
/// struct MutationRoot;
|
|
|
|
///
|
2020-09-13 03:41:15 +00:00
|
|
|
/// #[GQLObject]
|
2020-03-30 14:02:43 +00:00
|
|
|
/// impl MutationRoot {
|
|
|
|
/// async fn upload(&self, file: Upload) -> bool {
|
2020-05-11 13:47:24 +00:00
|
|
|
/// println!("upload: filename={}", file.filename());
|
2020-03-30 14:02:43 +00:00
|
|
|
/// true
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # Example Curl Request
|
2020-03-30 14:36:27 +00:00
|
|
|
/// Assuming you have defined your MutationRoot like in the example above,
|
2020-03-30 14:31:38 +00:00
|
|
|
/// you can now upload a file `myFile.txt` with the below curl command:
|
2020-03-30 14:02:43 +00:00
|
|
|
///
|
|
|
|
/// ```curl
|
2020-03-30 14:47:17 +00:00
|
|
|
/// curl 'localhost:8000' \
|
2020-03-30 14:02:43 +00:00
|
|
|
/// --form 'operations={
|
|
|
|
/// "query": "mutation ($file: Upload!) { upload(file: $file) }",
|
|
|
|
/// "variables": { "file": null }}' \
|
|
|
|
/// --form 'map={ "0": ["variables.file"] }' \
|
|
|
|
/// --form '0=@myFile.txt'
|
|
|
|
/// ```
|
2020-05-11 09:13:50 +00:00
|
|
|
pub struct Upload(UploadValue);
|
|
|
|
|
|
|
|
impl Upload {
|
2020-03-20 03:56:08 +00:00
|
|
|
/// Filename
|
2020-05-11 09:13:50 +00:00
|
|
|
pub fn filename(&self) -> &str {
|
|
|
|
self.0.filename.as_str()
|
|
|
|
}
|
2020-03-20 03:56:08 +00:00
|
|
|
|
|
|
|
/// Content type, such as `application/json`, `image/jpg` ...
|
2020-05-11 09:13:50 +00:00
|
|
|
pub fn content_type(&self) -> Option<&str> {
|
|
|
|
self.0.content_type.as_deref()
|
|
|
|
}
|
|
|
|
|
2020-06-08 02:44:01 +00:00
|
|
|
/// Convert to a `Read`.
|
|
|
|
///
|
|
|
|
/// **Note**: this is a *synchronous/blocking* reader.
|
2020-05-11 13:47:24 +00:00
|
|
|
pub fn into_read(self) -> impl Read + Sync + Send + 'static {
|
|
|
|
self.0.content
|
|
|
|
}
|
2020-03-14 03:46:20 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 05:38:31 +00:00
|
|
|
impl Type for Upload {
|
2020-03-14 03:46:20 +00:00
|
|
|
fn type_name() -> Cow<'static, str> {
|
|
|
|
Cow::Borrowed("Upload")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut registry::Registry) -> String {
|
2020-05-15 02:08:37 +00:00
|
|
|
registry.create_type::<Self, _>(|_| registry::MetaType::Scalar {
|
2020-03-14 03:46:20 +00:00
|
|
|
name: Self::type_name().to_string(),
|
|
|
|
description: None,
|
2020-09-08 10:07:32 +00:00
|
|
|
is_valid: |value| matches!(value, Value::Upload(_)),
|
2020-03-14 03:46:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 05:38:31 +00:00
|
|
|
impl InputValueType for Upload {
|
2020-05-28 07:00:55 +00:00
|
|
|
fn parse(value: Option<Value>) -> InputValueResult<Self> {
|
|
|
|
let value = value.unwrap_or_default();
|
2020-05-11 13:47:24 +00:00
|
|
|
if let Value::Upload(upload) = value {
|
|
|
|
Ok(Upload(upload))
|
|
|
|
} else {
|
|
|
|
Err(InputValueError::ExpectedType(value))
|
2020-03-14 03:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-26 10:34:43 +00:00
|
|
|
|
|
|
|
fn to_value(&self) -> Value {
|
|
|
|
Value::Null
|
|
|
|
}
|
2020-03-14 03:46:20 +00:00
|
|
|
}
|