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
// Copyright 2022 Jonas Kruckenberg
// SPDX-License-Identifier: MIT
//! This crate contains a Tauri plugin used to expose a [`juniper`] 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 juniper::{graphql_object, EmptySubscription, EmptyMutation, FieldResult, GraphQLObject, RootNode};
//! use tauri_plugin_graphql::Context as GraphQLContext;
//!
//! #[derive(GraphQLObject, Debug, Clone)]
//! struct ListItem {
//! id: i32,
//! text: String
//! }
//!
//! impl ListItem {
//! pub fn new(text: String) -> Self {
//! Self {
//! id: rand::random::<i32>(),
//! text
//! }
//! }
//! }
//!
//! struct Query;
//!
//! #[graphql_object(context = GraphQLContext)]
//! impl Query {
//! fn list() -> FieldResult<Vec<ListItem>> {
//! let item = vec![
//! ListItem::new("foo".to_string()),
//! ListItem::new("bar".to_string())
//! ];
//!
//! Ok(item)
//! }
//! }
//!
//! // Consumers of this schema can only read data,
//! // so we must specifcy `EmptyMutation` and `EmptySubscription`
//! type Schema = RootNode<
//! 'static,
//! Query,
//! EmptyMutation<GraphQLContext>,
//! EmptySubscription<GraphQLContext>
//! >;
//!
//! let schema = Schema::new(
//! Query,
//! EmptyMutation::<GraphQLContext>::new(),
//! EmptySubscription::<GraphQLContext>::new(),
//! );
//!
//! 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 juniper::{graphql_object, EmptySubscription, EmptyMutation, FieldResult, GraphQLObject, RootNode};
//! use tauri_plugin_graphql::Context as GraphQLContext;
//! use tauri::Manager;
//! use std::sync::Mutex;
//!
//! #[derive(Debug, Default)]
//! struct List(Mutex<Vec<ListItem>>);
//!
//! #[derive(GraphQLObject, Debug, Clone)]
//! struct ListItem {
//! id: i32,
//! text: String
//! }
//!
//! impl ListItem {
//! pub fn new(text: String) -> Self {
//! Self {
//! id: rand::random::<i32>(),
//! text
//! }
//! }
//! }
//!
//! struct Query;
//!
//! #[graphql_object(context = GraphQLContext)]
//! impl Query {
//! fn list(ctx: &GraphQLContext) -> FieldResult<Vec<ListItem>> {
//! let list = ctx.app().state::<List>();
//! let list = list.0.lock().unwrap();
//!
//! let items = list.iter().cloned().collect::<Vec<_>>();
//!
//! Ok(items)
//! }
//! }
//!
//! struct Mutation;
//!
//! #[graphql_object(context = GraphQLContext)]
//! impl Mutation {
//! fn add_entry(ctx: &GraphQLContext, text: String) -> FieldResult<ListItem> {
//! let list = ctx.app().state::<List>();
//! let mut list = list.0.lock().unwrap();
//!
//! let item = ListItem::new(text);
//!
//! list.push(item.clone());
//!
//! Ok(item)
//! }
//! }
//!
//! // Consumers of this schema can read and write data,
//! // so set only Subscription as being empty
//! type Schema = RootNode<
//! 'static,
//! Query,
//! Mutation,
//! EmptySubscription<GraphQLContext>
//! >;
//!
//! let schema = Schema::new(
//! Query,
//! Mutation,
//! EmptySubscription::<GraphQLContext>::new(),
//! );
//!
//! tauri::Builder::default()
//! .plugin(tauri_plugin_graphql::init(schema))
//! .setup(|app| {
//! app.manage(List::default());
//!
//! Ok(())
//! });
//! ```
//!
//! ### Subscriptions
//!
//! > **Support for GraphQL Subscriptions requires the `subscriptions` feature
//! flag**
//!
//! 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 juniper::{
//! graphql_object, graphql_subscription, EmptyMutation, FieldResult, GraphQLObject,
//! RootNode, FieldError,
//! futures::Stream
//! };
//! use tauri_plugin_graphql::Context as GraphQLContext;
//! use std::pin::Pin;
//!
//! struct Query;
//!
//! #[graphql_object(context = GraphQLContext)]
//! impl Query {
//! fn hello_world() -> FieldResult<String> {
//! Ok("Hello World!".to_string())
//! }
//! }
//!
//! struct Subscription;
//!
//! type StringStream = Pin<Box<dyn Stream<Item = Result<String, FieldError>> + Send>>;
//!
//! #[graphql_subscription(context = GraphQLContext)]
//! impl Subscription {
//! async fn hello_world() -> StringStream {
//! let stream = juniper::futures::stream::iter(vec![
//! Ok("Hello".to_string()),
//! Ok("World!".to_string())
//! ]);
//!
//! Box::pin(stream)
//! }
//! }
//!
//! // This schema allows queries and subscriptions,
//! // so the mutations must be set to empty
//! type Schema = RootNode<
//! 'static,
//! Query,
//! EmptyMutation<GraphQLContext>,
//! Subscription,
//! >;
//!
//! let schema = Schema::new(
//! Query,
//! EmptyMutation::<GraphQLContext>::new(),
//! 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/latest/futures/stream/trait.Stream.html
//! [`Commands`]: https://tauri.studio/docs/guides/command
//! [`Events`]: https://tauri.studio/docs/guides/events
//! [`GraphQL`]: https://graphql.org
use juniper::http::GraphQLBatchRequest;
#[cfg(feature = "subscriptions")]
use juniper::{
futures::{FutureExt, StreamExt, TryFutureExt},
http::GraphQLRequest,
};
#[cfg(feature = "subscriptions")]
use juniper_subscriptions::Connection;
#[cfg(feature = "subscriptions")]
use serde::Deserialize;
use std::sync::Arc;
use tauri::{
plugin::{self, TauriPlugin},
AppHandle, InvokeError, Manager, Runtime, Window,
};
pub use juniper;
/// The context that is available to GraphQL resolvers.
pub struct Context<R: Runtime = tauri::Wry> {
app: AppHandle<R>,
window: Window<R>,
}
impl<R: Runtime> Context<R> {
/// A handle to the Tauri application instance.
pub fn app(&self) -> &AppHandle<R> {
&self.app
}
/// A handle to the window that issued the GraphQL request.
pub fn window(&self) -> &Window<R> {
&self.window
}
}
impl<R: Runtime> juniper::Context for Context<R> {}
/// Initializes the GraphQL plugin
///
/// This plugin exposes a juniper 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 [`juniper::RootNode`].
///
/// ## Example
///
/// ```rust
/// use juniper::{EmptyMutation, EmptySubscription, FieldResult, GraphQLObject, RootNode, graphql_object};
/// use tauri_plugin_graphql::Context as GraphQLContext;
///
/// #[derive(GraphQLObject)]
/// struct User {
/// id: i32,
/// name: String
/// }
///
/// struct Query;
///
/// // Implement resolvers for all possible queries.
/// #[graphql_object(context = GraphQLContext)]
/// impl Query {
/// fn me() -> FieldResult<User> {
/// Ok(User {
/// id: 1,
/// name: "Luke Skywalker".to_string(),
/// })
/// }
/// }
///
/// // A shorter alias for our apps schema. Note that this schema has no mutations or subscriptions,
/// // so we specify `EmptyMutation` and `EmptySubscription` respectively.
/// type Schema = RootNode<
/// 'static,
/// Query,
/// EmptyMutation<GraphQLContext>,
/// EmptySubscription<GraphQLContext>
/// >;
///
/// let schema = Schema::new(
/// Query,
/// EmptyMutation::<GraphQLContext>::new(),
/// EmptySubscription::<GraphQLContext>::new(),
/// );
///
/// tauri::Builder::default()
/// .plugin(tauri_plugin_graphql::init(schema));
/// ```
pub fn init<R, Query, Mutation, Subscription, S>(
schema: juniper::RootNode<'static, Query, Mutation, Subscription, S>,
) -> TauriPlugin<R>
where
R: Runtime,
Query: juniper::GraphQLTypeAsync<S, Context = Context<R>> + Send + 'static,
Query::TypeInfo: Send + Sync,
Mutation: juniper::GraphQLTypeAsync<S, Context = Context<R>> + Send + 'static,
Mutation::TypeInfo: Send + Sync,
Subscription: juniper::GraphQLSubscriptionType<S, Context = Context<R>> + Send + 'static,
Subscription::TypeInfo: Send + Sync,
S: juniper::ScalarValue + Send + Sync + 'static,
{
let schema = Arc::new(schema);
plugin::Builder::new("graphql")
.invoke_handler(move |invoke| {
let window = invoke.message.window();
#[cfg(feature = "subscriptions")]
let subscription_window = window.clone();
let ctx = Context {
app: window.app_handle(),
window,
};
let schema = schema.clone();
match invoke.message.command() {
"graphql" => invoke.resolver.respond_async(async move {
let req: GraphQLBatchRequest<S> =
serde_json::from_value(invoke.message.payload().clone())
.map_err(InvokeError::from_serde_json)?;
let ret = req
.execute::<Query, Mutation, Subscription>(&schema, &ctx)
.await;
let str = serde_json::to_string(&ret).map_err(InvokeError::from_serde_json)?;
Ok((str, ret.is_ok()))
}),
#[cfg(feature = "subscriptions")]
"subscriptions" => invoke.resolver.respond_async(async move {
let req: GraphQLSubscriptionRequest<S> =
serde_json::from_value(invoke.message.payload().clone())
.map_err(InvokeError::from_serde_json)?;
let mut conn = juniper::http::resolve_into_stream(&req.inner, &schema, &ctx)
.map_ok(|(stream, errors)| Connection::from_stream(stream, errors))
.boxed()
.await?;
let event_id = &format!("graphql://{}", req.id);
while let Some(result) = conn.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(())
}),
endpoint => invoke
.resolver
.reject(format!("Invalid endpoint \"{}\". Valid endpoints are: \"graphql\", \"subscriptions\".", endpoint)),
};
})
.build()
}
#[cfg(feature = "subscriptions")]
#[derive(Debug, Deserialize)]
pub struct GraphQLSubscriptionRequest<S: juniper::ScalarValue> {
#[serde(
flatten,
bound(deserialize = "juniper::InputValue<S>: serde::Deserialize<'de>")
)]
inner: GraphQLRequest<S>,
id: u32,
}