Crates.io | mizuki |
lib.rs | mizuki |
version | 1.0.1 |
created_at | 2023-12-04 09:48:25.871048+00 |
updated_at | 2025-02-04 22:17:50.148683+00 |
description | A toolkit for building Tauri Plugins that enables type-safe IPC through GraphQL. |
homepage | |
repository | https://github.com/tonymushah/mizuki |
max_upload_size | |
id | 1057485 |
size | 612,355 |
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!
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
build.rs
file on your plugin root directory (Same as Cargo.toml
):fn main() {
mizuki_build::build()
}
It will automaticly generate the required permission schemas.
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");
}
core:event:default
, <your-plugin>:allow-graphql
, <your-plugin>:allow-subscriptions
in your tauri app capabilities
:{
"$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 |
[ |
mizuki-urql-adapter
usagemizuki-urql-adapter
npm package with the package manager of your choice (mine is pnpm):pnpm install mizuki-urql-adapter
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")]
})
mizuki-apollo-link
usagemizuki-apollo-link
npm package with the package manager of your choice (mine is pnpm):pnpm add mizuki-apollo-link
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!