|
| 1 | +use async_graphql::{ |
| 2 | + dynamic::{Field, FieldFuture, FieldValue, InputValue, Object, Schema, SchemaError, TypeRef}, |
| 3 | + Value, |
| 4 | +}; |
| 5 | +use futures::lock::Mutex; |
| 6 | +use slab::Slab; |
| 7 | + |
| 8 | +pub type Storage = Mutex<Slab<FileInfo>>; |
| 9 | + |
| 10 | +#[derive(Clone)] |
| 11 | +pub struct FileInfo { |
| 12 | + pub id: String, |
| 13 | + url: String, |
| 14 | +} |
| 15 | + |
| 16 | +pub fn schema() -> Result<Schema, SchemaError> { |
| 17 | + let file_info = Object::new("FileInfo") |
| 18 | + .field(Field::new("id", TypeRef::named_nn(TypeRef::ID), |ctx| { |
| 19 | + FieldFuture::new(async { |
| 20 | + let file_info = ctx.parent_value.try_downcast_ref::<FileInfo>()?; |
| 21 | + Ok(Some(Value::from(&file_info.id))) |
| 22 | + }) |
| 23 | + })) |
| 24 | + .field(Field::new( |
| 25 | + "url", |
| 26 | + TypeRef::named_nn(TypeRef::STRING), |
| 27 | + |ctx| { |
| 28 | + FieldFuture::new(async { |
| 29 | + let file_info = ctx.parent_value.try_downcast_ref::<FileInfo>()?; |
| 30 | + Ok(Some(Value::from(&file_info.url))) |
| 31 | + }) |
| 32 | + }, |
| 33 | + )); |
| 34 | + |
| 35 | + let query = Object::new("Query").field(Field::new( |
| 36 | + "uploads", |
| 37 | + TypeRef::named_nn_list_nn(file_info.type_name()), |
| 38 | + |ctx| { |
| 39 | + FieldFuture::new(async move { |
| 40 | + let storage = ctx.data_unchecked::<Storage>().lock().await; |
| 41 | + Ok(Some(FieldValue::list( |
| 42 | + storage |
| 43 | + .iter() |
| 44 | + .map(|(_, file)| FieldValue::owned_any(file.clone())), |
| 45 | + ))) |
| 46 | + }) |
| 47 | + }, |
| 48 | + )); |
| 49 | + |
| 50 | + let mutation = Object::new("Mutation") |
| 51 | + .field( |
| 52 | + Field::new( |
| 53 | + "singleUpload", |
| 54 | + TypeRef::named_nn(file_info.type_name()), |
| 55 | + |ctx| { |
| 56 | + FieldFuture::new(async move { |
| 57 | + let mut storage = ctx.data_unchecked::<Storage>().lock().await; |
| 58 | + let file = ctx.args.try_get("file")?.upload()?; |
| 59 | + let entry = storage.vacant_entry(); |
| 60 | + let upload = file.value(&ctx).unwrap(); |
| 61 | + let info = FileInfo { |
| 62 | + id: entry.key().to_string(), |
| 63 | + url: upload.filename.clone(), |
| 64 | + }; |
| 65 | + entry.insert(info.clone()); |
| 66 | + Ok(Some(FieldValue::owned_any(info))) |
| 67 | + }) |
| 68 | + }, |
| 69 | + ) |
| 70 | + .argument(InputValue::new("file", TypeRef::named_nn(TypeRef::UPLOAD))), |
| 71 | + ) |
| 72 | + .field( |
| 73 | + Field::new( |
| 74 | + "multipleUpload", |
| 75 | + TypeRef::named_nn_list_nn(file_info.type_name()), |
| 76 | + |ctx| { |
| 77 | + FieldFuture::new(async move { |
| 78 | + let mut infos = Vec::new(); |
| 79 | + let mut storage = ctx.data_unchecked::<Storage>().lock().await; |
| 80 | + for item in ctx.args.try_get("files")?.list()?.iter() { |
| 81 | + let file = item.upload()?; |
| 82 | + let entry = storage.vacant_entry(); |
| 83 | + let upload = file.value(&ctx).unwrap(); |
| 84 | + let info = FileInfo { |
| 85 | + id: entry.key().to_string(), |
| 86 | + url: upload.filename.clone(), |
| 87 | + }; |
| 88 | + entry.insert(info.clone()); |
| 89 | + infos.push(FieldValue::owned_any(info)) |
| 90 | + } |
| 91 | + Ok(Some(infos)) |
| 92 | + }) |
| 93 | + }, |
| 94 | + ) |
| 95 | + .argument(InputValue::new( |
| 96 | + "files", |
| 97 | + TypeRef::named_nn_list_nn(TypeRef::UPLOAD), |
| 98 | + )), |
| 99 | + ); |
| 100 | + |
| 101 | + Schema::build(query.type_name(), Some(mutation.type_name()), None) |
| 102 | + .enable_uploading() |
| 103 | + .register(file_info) |
| 104 | + .register(query) |
| 105 | + .register(mutation) |
| 106 | + .data(Storage::default()) |
| 107 | + .finish() |
| 108 | +} |
0 commit comments