This crate contains a Tauri plugin builder 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, Events and GraphQL.
Queries
An example app that implements a very simple read-only todo-app using GraphQL:
use ;
;
let schema = new;
default
.plugin;
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.
use ;
use ;
use Mutex;
;
;
;
let schema = new;
default
.plugin
.setup;
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.
use ;
;
;
let schema = new;
default
.plugin;
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 builder has no backwards compatibility
guarantees.