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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
// Copyright 2022 Jonas Kruckenberg
// SPDX-License-Identifier: MIT
//! This crate contains a Tauri plugin used to expose a [`async_graphql`]
//! GraphQL endpoint through Tauri's IPC system. This plugin can be used as
//! safer alternative to Tauri's existing Command API since both the Rust and
//! JavaScript side of the interface can be generated from a common schema.
//!
//! ## Rationale
//!
//! Especially in bigger projects that have specialized teams for the Frontend
//! and Rust core the existing command API falls short of being an optimal
//! solution. The Frontend is tightly coupled through `invoke()` calls to
//! backend commands, but there is no type-safety to alert Frontend developers
//! to changes in command signatures. This results in a very brittle interface
//! where changes on the Rust side will inadvertently break code in the
//! Frontend. This problem is similar exiting REST APIs, where the absence of a
//! formal contract between the server and the frontend makes future changes
//! very difficult.
//!
//! We can employ the same techniques used in traditional web development and
//! use shared schema that governs which types, methods, etc. are
//! available. GraphQL is such a schema language.
//!
//! ## Examples
//!
//! For the following examples, it is assumed you are familiar with [`Tauri
//! Commands`][`Commands`], [`Events`] and [`GraphQL`].
//!
//! ### Queries
//!
//! An example app that implements a very simple read-only todo-app using
//! GraphQL:
//!
//! ```rust
//! use async_graphql::{Schema, EmptySubscription, EmptyMutation, Object, SimpleObject, Result as GraphQLResult};
//!
//! #[derive(SimpleObject, Debug, Clone)]
//! struct ListItem {
//! id: i32,
//! text: String
//! }
//!
//! impl ListItem {
//! pub fn new(text: String) -> Self {
//! Self {
//! id: rand::random::<i32>(),
//! text
//! }
//! }
//! }
//!
//! struct Query;
//!
//! #[Object]
//! impl Query {
//! async fn list(&self) -> GraphQLResult<Vec<ListItem>> {
//! let item = vec![
//! ListItem::new("foo".to_string()),
//! ListItem::new("bar".to_string())
//! ];
//!
//! Ok(item)
//! }
//! }
//!
//! let schema = Schema::new(
//! Query,
//! EmptyMutation,
//! EmptySubscription,
//! );
//!
//! tauri::Builder::default()
//! .plugin(tauri_plugin_graphql::init(schema));
//! ```
//!
//! ### Mutations
//!
//! GraphQL mutations provide a way to update or create state in the Core.
//!
//! Similarly to queries, mutations have access to a context object and can
//! manipulate windows, menus or global state.
//!
//! ```rust
//! use async_graphql::{Schema, Object, Context, EmptySubscription, EmptyMutation, SimpleObject, Result as GraphQLObject};
//! use tauri::{AppHandle, Manager};
//! use std::sync::Mutex;
//!
//! #[derive(Debug, Default)]
//! struct List(Mutex<Vec<ListItem>>);
//!
//! #[derive(SimpleObject, Debug, Clone)]
//! struct ListItem {
//! id: i32,
//! text: String
//! }
//!
//! impl ListItem {
//! pub fn new(text: String) -> Self {
//! Self {
//! id: rand::random::<i32>(),
//! text
//! }
//! }
//! }
//!
//! struct Query;
//!
//! #[Object]
//! impl Query {
//! async fn list(&self, ctx: &Context<'_>) -> GraphQLObject<Vec<ListItem>> {
//! let app = ctx.data::<AppHandle>().unwrap();
//!
//! let list = app.state::<List>();
//! let list = list.0.lock().unwrap();
//!
//! let items = list.iter().cloned().collect::<Vec<_>>();
//!
//! Ok(items)
//! }
//! }
//!
//! struct Mutation;
//!
//! #[Object]
//! impl Mutation {
//! async fn add_entry(&self, ctx: &Context<'_>, text: String) -> GraphQLObject<ListItem> {
//! let app = ctx.data::<AppHandle>().unwrap();
//!
//! let list = app.state::<List>();
//! let mut list = list.0.lock().unwrap();
//!
//! let item = ListItem::new(text);
//!
//! list.push(item.clone());
//!
//! Ok(item)
//! }
//! }
//!
//! let schema = Schema::new(
//! Query,
//! Mutation,
//! EmptySubscription,
//! );
//!
//! tauri::Builder::default()
//! .plugin(tauri_plugin_graphql::init(schema))
//! .setup(|app| {
//! app.manage(List::default());
//!
//! Ok(())
//! });
//! ```
//!
//! ### Subscriptions
//!
//! GraphQL subscriptions are a way to push real-time data to the Frontend.
//! Similarly to queries, a client can request a set of fields, but instead of
//! immediately returning a single answer, a new result is sent to the Frontend
//! every time the Core sends one.
//!
//! Subscription resolvers should be async and must return a [`Stream`].
//!
//! ```rust
//! use async_graphql::{
//! futures_util::{self, stream::Stream},
//! Schema, Object, Subscription, EmptySubscription,
//! EmptyMutation, SimpleObject, Result as GraphQLResult
//! };
//!
//! struct Query;
//!
//! #[Object]
//! impl Query {
//! async fn hello_world(&self) -> GraphQLResult<&str> {
//! Ok("Hello World!")
//! }
//! }
//!
//! struct Subscription;
//!
//! #[Subscription]
//! impl Subscription {
//! async fn hello_world(&self) -> impl Stream<Item = &str> {
//! futures_util::stream::iter(vec!["Hello", "World!"])
//! }
//! }
//!
//! let schema = Schema::new(
//! Query,
//! EmptyMutation,
//! Subscription,
//! );
//!
//! tauri::Builder::default()
//! .plugin(tauri_plugin_graphql::init(schema));
//! ```
//!
//! ## Stability
//!
//! To work around limitations with the current command system, this plugin
//! directly implements an invoke handler instead of reyling on the
//! [`tauri::generate_handler`] macro.
//! Since the invoke handler implementation is not considered stable and might
//! change between releases **this plugin has no backwards compatibility
//! guarantees**.
//!
//! [`Stream`]: https://docs.rs/futures-util/latest/futures_util/stream/trait.Stream.html
//! [`Commands`]: https://tauri.studio/docs/guides/command
//! [`Events`]: https://tauri.studio/docs/guides/events
//! [`GraphQL`]: https://graphql.org
pub use async_graphql;
use async_graphql::{
futures_util::StreamExt, BatchRequest, ObjectType, Request, Schema, SubscriptionType,
};
use serde::Deserialize;
#[cfg(feature = "graphiql")]
use std::net::SocketAddr;
use tauri::{
plugin::{self, TauriPlugin},
Invoke, InvokeError, Manager, Runtime,
};
fn invoke_handler<R, Query, Mutation, Subscription>(
schema: Schema<Query, Mutation, Subscription>,
) -> impl Fn(Invoke<R>)
where
R: Runtime,
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
{
move |invoke| {
let window = invoke.message.window();
let schema = schema.clone();
match invoke.message.command() {
"graphql" => invoke.resolver.respond_async(async move {
let req: BatchRequest = serde_json::from_value(invoke.message.payload().clone())
.map_err(InvokeError::from_serde_json)?;
let resp = schema
.execute_batch(req.data(window.app_handle()).data(window))
.await;
let str = serde_json::to_string(&resp).map_err(InvokeError::from_serde_json)?;
Ok((str, resp.is_ok()))
}),
"subscriptions" => invoke.resolver.respond_async(async move {
let req: SubscriptionRequest = serde_json::from_value(invoke.message.payload().clone())
.map_err(InvokeError::from_serde_json)?;
let subscription_window = window.clone();
let mut stream = schema.execute_stream(req.inner.data(window.app_handle()).data(window));
let event_id = &format!("graphql://{}", req.id);
while let Some(result) = stream.next().await {
let str = serde_json::to_string(&result).map_err(InvokeError::from_serde_json)?;
subscription_window.emit(event_id, str)?;
}
subscription_window.emit(event_id, Option::<()>::None)?;
Ok(())
}),
cmd => invoke.resolver.reject(format!(
"Invalid endpoint \"{}\". Valid endpoints are: \"graphql\", \"subscriptions\".",
cmd
)),
}
}
}
/// Initializes the GraphQL plugin.
///
/// This plugin exposes a async-graphql endpoint via Tauri's IPC system,
/// allowing the frontend to invoke backend functionality through GraphQL.
/// **This does not open a web server.**
///
/// The `schema` argument must be a valid [`async_graphql::Schema`].
///
/// ## Example
///
/// ```rust
/// use async_graphql::{Schema, Object, EmptyMutation, EmptySubscription, SimpleObject, Result as GraphQLResult};
///
/// #[derive(SimpleObject)]
/// struct User {
/// id: i32,
/// name: String
/// }
///
/// struct Query;
///
/// // Implement resolvers for all possible queries.
/// #[Object]
/// impl Query {
/// async fn me(&self) -> GraphQLResult<User> {
/// Ok(User {
/// id: 1,
/// name: "Luke Skywalker".to_string(),
/// })
/// }
/// }
///
/// let schema = Schema::new(
/// Query,
/// EmptyMutation,
/// EmptySubscription,
/// );
///
/// tauri::Builder::default()
/// .plugin(tauri_plugin_graphql::init(schema));
/// ```
pub fn init<R, Query, Mutation, Subscription>(
schema: Schema<Query, Mutation, Subscription>,
) -> TauriPlugin<R>
where
R: Runtime,
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
{
plugin::Builder::new("graphql")
.invoke_handler(invoke_handler(schema))
.build()
}
/// Initializes the GraphQL plugin and GraphiQL IDE.
///
/// This plugin exposes a async-graphql endpoint via Tauri's IPC system,
/// allowing the frontend to invoke backend functionality through GraphQL.
/// While the regular `init` function does not open a web server, the GraphiQL
/// client is exposed over http.
///
/// The `schema` argument must be a valid [`async_graphql::Schema`].
///
/// ## Example
///
/// ```rust
/// use async_graphql::{Schema, Object, EmptyMutation, EmptySubscription, SimpleObject, Result as GraphQLResult};
///
/// #[derive(SimpleObject)]
/// struct User {
/// id: i32,
/// name: String
/// }
///
/// struct Query;
///
/// // Implement resolvers for all possible queries.
/// #[Object]
/// impl Query {
/// async fn me(&self) -> GraphQLResult<User> {
/// Ok(User {
/// id: 1,
/// name: "Luke Skywalker".to_string(),
/// })
/// }
/// }
///
/// let schema = Schema::new(
/// Query,
/// EmptyMutation,
/// EmptySubscription,
/// );
///
/// tauri::Builder::default()
/// .plugin(tauri_plugin_graphql::init_with_graphiql(schema, ([127,0,0,1], 8080)));
/// ```
#[cfg(feature = "graphiql")]
pub fn init_with_graphiql<R, Query, Mutation, Subscription>(
schema: Schema<Query, Mutation, Subscription>,
graphiql_addr: impl Into<SocketAddr>,
) -> TauriPlugin<R>
where
R: Runtime,
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
{
use async_graphql::http::GraphiQLSource;
use async_graphql_warp::{GraphQLBadRequest, GraphQLResponse};
use http::StatusCode;
use std::convert::Infallible;
use warp::{http::Response as HttpResponse, Filter, Rejection};
let graphiql_addr: SocketAddr = graphiql_addr.into();
plugin::Builder::new("graphql")
.invoke_handler(invoke_handler(schema.clone()))
.setup(move |_| {
let graphql_post = async_graphql_warp::graphql(schema).and_then(
|(schema, request): (
Schema<Query, Mutation, Subscription>,
async_graphql::Request,
)| async move {
Ok::<_, Infallible>(GraphQLResponse::from(schema.execute(request).await))
},
);
let graphiql = warp::path::end().and(warp::get()).map(move || {
HttpResponse::builder()
.header("content-type", "text/html")
.body(
GraphiQLSource::build()
.endpoint(&graphiql_addr.to_string())
.finish(),
)
});
let routes = graphiql
.or(graphql_post)
.recover(|err: Rejection| async move {
if let Some(GraphQLBadRequest(err)) = err.find() {
return Ok::<_, Infallible>(warp::reply::with_status(
err.to_string(),
StatusCode::BAD_REQUEST,
));
}
Ok(warp::reply::with_status(
"INTERNAL_SERVER_ERROR".to_string(),
StatusCode::INTERNAL_SERVER_ERROR,
))
});
println!("GraphiQL IDE: {}", graphiql_addr);
tauri::async_runtime::spawn(warp::serve(routes).run(graphiql_addr));
Ok(())
})
.build()
}
#[derive(Debug, Deserialize)]
struct SubscriptionRequest {
#[serde(flatten)]
inner: Request,
id: u32,
}