A toolkit for building Tauri plugins that enables type-safe IPC through GraphQL.
This project is a fork from JonasKruckenberg/tauri-plugin-graphql.
But I thought that it would be a great a idea to push the plugin futher and create a toolkit for building GraphQL Tauri Plugins.
With the introduction of plugin command permissions in Tauri v2, you need your Mizuki plugin into a separate library crate. If you already have, then congrats!
- modify the plugin
Cargo.toml
:
[package]
name = "my-todo-plugin"
version = "0.1.0"
edition = "2021"
## your plugin name
## Required or your plugin might not build
links = "todo-plugin"
[build-dependencies]
mizuki-build = "1"
## Other of your plugin build dependencies
[dependencies]
mizuki = "1"
async-graphql = "7"
tauri = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
## Other of your plugin dependecies
- Make a
build.rs
file on your plugin root directory (Same asCargo.toml
):
fn main() {
mizuki_build::build()
}
It will automaticly generate the required permission schemas.
- Write your plugin code (mainly your lib.rs):
You need to register the plugin giving it a async_graphql::Schema
. This schema will be used to fulfill requests.
use async_graphql::{Schema, Object, EmptySubscription, EmptyMutation, Result as GraphQLResult, SimpleObject};
#[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)
}
}
pub fn init_plugin<R: tauri::Runtime>() -> mizuki::MizukiPlugin<R, Query, EmptyMutation, EmptySubscription> {
mizuki::Builder::new("todo-plugin", Schema::new(
Query,
EmptyMutation,
EmptySubscription,
)).build()
}
-
Add your plugin to main app
Cargo.toml
-
Register the plugin:
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
// The plugin name is required
.plugin(my_todo_plugin::init_plugin())
.run(tauri::generate_context!())
.expect("failed to run app");
}
- Enable the
core:event:default
,<your-plugin>:allow-graphql
,<your-plugin>:allow-subscriptions
in your tauri appcapabilities
:
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "Capability for the main window",
"windows": ["main"],
"permissions": [
"core:default",
"core:event:default",
"todo-plugin:allow-graphql",
"todo-plugin:allow-subscriptions"
]
}
core:event:default
is required for subscriptions.
The only client-side adapter currently are:
If you need adapters for other GraphQL clients, open a PR!
Package | Version (click for changelogs) |
---|---|
mizuki-urql-adapter |
|
mizuki-apollo-link |
[] |
- Install the
mizuki-urql-adapter
npm package with the package manager of your choice (mine is pnpm):
pnpm install mizuki-urql-adapter
- Intialize the client:
import { Client } from "@urql/core" // or whatever urql framework packages
import { getExchanges } from "mizuki-urql-adapter"
const client = new Client({
// Not required but needed if you want to some releive Typescript errors
url: "",
exchanges: [...getExchanges("todo-plugin")]
})
- Use the client anyway you need it.
- Install the
mizuki-apollo-link
npm package with the package manager of your choice (mine is pnpm):
pnpm add mizuki-apollo-link
- Intialize the client:
import { ApolloClient, InMemoryCache } from "@apollo/client/core"; // or @apollo/client if you want to
import { MizukiLink } from "mizuki-apollo-link";
const client = new ApolloClient({
link: new MizukiLink("todo-plugin"),
cache: new InMemoryCache()
});
PRs are welcome!