|
| 1 | +use async_graphql::{dynamic::*, Value, ID}; |
| 2 | +use futures_util::StreamExt; |
| 3 | + |
| 4 | +use crate::{simple_broker::SimpleBroker, Book, BookChanged, MutationType, Storage}; |
| 5 | + |
| 6 | +impl<'a> From<MutationType> for FieldValue<'a> { |
| 7 | + fn from(value: MutationType) -> Self { |
| 8 | + match value { |
| 9 | + MutationType::Created => FieldValue::value("CREATED"), |
| 10 | + MutationType::Deleted => FieldValue::value("DELETED"), |
| 11 | + } |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +pub fn schema() -> Result<Schema, SchemaError> { |
| 16 | + let mutation_type = Enum::new("MutationType") |
| 17 | + .item(EnumItem::new("CREATED").description("New book created.")) |
| 18 | + .item(EnumItem::new("DELETED").description("Current book deleted.")); |
| 19 | + |
| 20 | + let book = Object::new("Book") |
| 21 | + .description("A book that will be stored.") |
| 22 | + .field(Field::new("id", TypeRef::named_nn(TypeRef::ID), |ctx| { |
| 23 | + FieldFuture::new(async move { |
| 24 | + let book = ctx.parent_value.try_downcast_ref::<Book>()?; |
| 25 | + Ok(Some(Value::from(book.id.to_owned()))) |
| 26 | + }) |
| 27 | + })) |
| 28 | + .field(Field::new( |
| 29 | + "name", |
| 30 | + TypeRef::named_nn(TypeRef::STRING), |
| 31 | + |ctx| { |
| 32 | + FieldFuture::new(async move { |
| 33 | + let book = ctx.parent_value.try_downcast_ref::<Book>()?; |
| 34 | + Ok(Some(Value::from(book.name.to_owned()))) |
| 35 | + }) |
| 36 | + }, |
| 37 | + )) |
| 38 | + .field(Field::new( |
| 39 | + "author", |
| 40 | + TypeRef::named_nn(TypeRef::STRING), |
| 41 | + |ctx| { |
| 42 | + FieldFuture::new(async move { |
| 43 | + let book = ctx.parent_value.try_downcast_ref::<Book>()?; |
| 44 | + Ok(Some(Value::from(book.author.to_owned()))) |
| 45 | + }) |
| 46 | + }, |
| 47 | + )); |
| 48 | + let book_changed = Object::new("BookChanged") |
| 49 | + .field(Field::new( |
| 50 | + "mutationType", |
| 51 | + TypeRef::named_nn(mutation_type.type_name()), |
| 52 | + |ctx| { |
| 53 | + FieldFuture::new(async move { |
| 54 | + let book_changed = ctx.parent_value.try_downcast_ref::<BookChanged>()?; |
| 55 | + Ok(Some(FieldValue::from(book_changed.mutation_type))) |
| 56 | + }) |
| 57 | + }, |
| 58 | + )) |
| 59 | + .field(Field::new("id", TypeRef::named_nn(TypeRef::ID), |ctx| { |
| 60 | + FieldFuture::new(async move { |
| 61 | + let book_changed = ctx.parent_value.try_downcast_ref::<BookChanged>()?; |
| 62 | + Ok(Some(Value::from(book_changed.id.to_owned()))) |
| 63 | + }) |
| 64 | + })) |
| 65 | + .field(Field::new( |
| 66 | + "book", |
| 67 | + TypeRef::named(book.type_name()), |
| 68 | + |ctx| { |
| 69 | + FieldFuture::new(async move { |
| 70 | + let book_changed = ctx.parent_value.try_downcast_ref::<BookChanged>()?; |
| 71 | + let id = book_changed.id.to_owned(); |
| 72 | + let book_id = id.parse::<usize>()?; |
| 73 | + let store = ctx.data_unchecked::<Storage>().lock().await; |
| 74 | + let book = store.get(book_id).cloned(); |
| 75 | + Ok(book.map(FieldValue::owned_any)) |
| 76 | + }) |
| 77 | + }, |
| 78 | + )); |
| 79 | + |
| 80 | + let query_root = Object::new("Query") |
| 81 | + .field(Field::new( |
| 82 | + "getBooks", |
| 83 | + TypeRef::named_list(book.type_name()), |
| 84 | + |ctx| { |
| 85 | + FieldFuture::new(async move { |
| 86 | + let store = ctx.data_unchecked::<Storage>().lock().await; |
| 87 | + let books: Vec<Book> = store.iter().map(|(_, book)| book.clone()).collect(); |
| 88 | + Ok(Some(FieldValue::list( |
| 89 | + books.into_iter().map(FieldValue::owned_any), |
| 90 | + ))) |
| 91 | + }) |
| 92 | + }, |
| 93 | + )) |
| 94 | + .field( |
| 95 | + Field::new("getBook", TypeRef::named(book.type_name()), |ctx| { |
| 96 | + FieldFuture::new(async move { |
| 97 | + let id = ctx.args.try_get("id")?; |
| 98 | + let book_id = match id.string() { |
| 99 | + Ok(id) => id.to_string(), |
| 100 | + Err(_) => id.u64()?.to_string(), |
| 101 | + }; |
| 102 | + let book_id = book_id.parse::<usize>()?; |
| 103 | + let store = ctx.data_unchecked::<Storage>().lock().await; |
| 104 | + let book = store.get(book_id).cloned(); |
| 105 | + Ok(book.map(FieldValue::owned_any)) |
| 106 | + }) |
| 107 | + }) |
| 108 | + .argument(InputValue::new("id", TypeRef::named_nn(TypeRef::ID))), |
| 109 | + ); |
| 110 | + |
| 111 | + let mutatation_root = Object::new("Mutation") |
| 112 | + .field( |
| 113 | + Field::new("createBook", TypeRef::named(TypeRef::ID), |ctx| { |
| 114 | + FieldFuture::new(async move { |
| 115 | + let mut store = ctx.data_unchecked::<Storage>().lock().await; |
| 116 | + let name = ctx.args.try_get("name")?; |
| 117 | + let author = ctx.args.try_get("author")?; |
| 118 | + let entry = store.vacant_entry(); |
| 119 | + let id: ID = entry.key().into(); |
| 120 | + let book = Book { |
| 121 | + id: id.clone(), |
| 122 | + name: name.string()?.to_string(), |
| 123 | + author: author.string()?.to_string(), |
| 124 | + }; |
| 125 | + entry.insert(book); |
| 126 | + let book_mutated = BookChanged { |
| 127 | + mutation_type: MutationType::Created, |
| 128 | + id: id.clone(), |
| 129 | + }; |
| 130 | + SimpleBroker::publish(book_mutated); |
| 131 | + Ok(Some(Value::from(id))) |
| 132 | + }) |
| 133 | + }) |
| 134 | + .argument(InputValue::new("name", TypeRef::named_nn(TypeRef::STRING))) |
| 135 | + .argument(InputValue::new( |
| 136 | + "author", |
| 137 | + TypeRef::named_nn(TypeRef::STRING), |
| 138 | + )), |
| 139 | + ) |
| 140 | + .field( |
| 141 | + Field::new("deleteBook", TypeRef::named_nn(TypeRef::BOOLEAN), |ctx| { |
| 142 | + FieldFuture::new(async move { |
| 143 | + let mut store = ctx.data_unchecked::<Storage>().lock().await; |
| 144 | + let id = ctx.args.try_get("id")?; |
| 145 | + let book_id = match id.string() { |
| 146 | + Ok(id) => id.to_string(), |
| 147 | + Err(_) => id.u64()?.to_string(), |
| 148 | + }; |
| 149 | + let book_id = book_id.parse::<usize>()?; |
| 150 | + if store.contains(book_id) { |
| 151 | + store.remove(book_id); |
| 152 | + let book_mutated = BookChanged { |
| 153 | + mutation_type: MutationType::Deleted, |
| 154 | + id: book_id.into(), |
| 155 | + }; |
| 156 | + SimpleBroker::publish(book_mutated); |
| 157 | + Ok(Some(Value::from(true))) |
| 158 | + } else { |
| 159 | + Ok(Some(Value::from(false))) |
| 160 | + } |
| 161 | + }) |
| 162 | + }) |
| 163 | + .argument(InputValue::new("id", TypeRef::named_nn(TypeRef::ID))), |
| 164 | + ); |
| 165 | + let subscription_root = Subscription::new("Subscription").field(SubscriptionField::new( |
| 166 | + "bookMutation", |
| 167 | + TypeRef::named_nn(book_changed.type_name()), |
| 168 | + |_| { |
| 169 | + SubscriptionFieldFuture::new(async { |
| 170 | + Ok(SimpleBroker::<BookChanged>::subscribe() |
| 171 | + .map(|book| Ok(FieldValue::owned_any(book)))) |
| 172 | + }) |
| 173 | + }, |
| 174 | + )); |
| 175 | + |
| 176 | + Schema::build( |
| 177 | + query_root.type_name(), |
| 178 | + Some(mutatation_root.type_name()), |
| 179 | + Some(subscription_root.type_name()), |
| 180 | + ) |
| 181 | + .register(mutation_type) |
| 182 | + .register(book) |
| 183 | + .register(book_changed) |
| 184 | + .register(query_root) |
| 185 | + .register(subscription_root) |
| 186 | + .register(mutatation_root) |
| 187 | + .data(Storage::default()) |
| 188 | + .finish() |
| 189 | +} |
0 commit comments