Replies: 6 comments 6 replies
-
Sascha Grunert has published a project building a web application with Yew which uses the Cap'n Proto protocol for data transfer which is really neat. Yew also provides a Fetch service and WebSocket service which you could send GraphQL queries using (or use to connect to a RESTful API). |
Beta Was this translation helpful? Give feedback.
-
Yeah, I think it's on you to pick your transport protocol (http or ws), request protocol (jsonrpc, graphql, etc), and encoding (json, binary, capn proto). Yew has wrappers around For example, for work we've been exploring the use of https://github.com/google/tarpc which is pretty close to being able to build to wasm and makes it easy to provide your own transport (in our case websockets). So, you may have to fork an existing client to get it working with wasm, I would be surprised if you had to write a totally new graphql client, but I'm not familiar with those rust projects. |
Beta Was this translation helpful? Give feedback.
-
I'm working on a GraphQL client (still very early on in the process, but it looks like it might show some promise), which is something like a Relay or Apollo GraphQL clone. You might be interested in it. |
Beta Was this translation helpful? Give feedback.
-
Tarpc looks excellent, can't remember if it has wasm support yet. |
Beta Was this translation helpful? Give feedback.
-
Since I had the same question and I couldn't really find any good examples: Here's a minimal example of a full stack code that does communication with the jsonrpc library, without any non-rust code (not even any schema specifications): common/src/lib.rs: //! defines the isomorphic code (common to both client and server)
use jsonrpc_derive::rpc;
use jsonrpc_core::{BoxFuture, Result};
#[rpc]
pub trait Rpc {
/// Adds two numbers and returns a result
#[rpc(name = "add")]
fn add(&self, a: u64, b: u64) -> Result<u64>;
/// Performs asynchronous operation
#[rpc(name = "callAsync")]
fn call(&self, a: u64) -> BoxFuture<Result<String>>;
}
pub use gen_client::Client as ApiClient; frontend/src/main.rs: use jsonrpc_core_client::transports::wasmhttp;
use yew::prelude::*;
pub async fn connect() -> common::ApiClient {
let (client, receiver_task) = wasmhttp::connect::<common::ApiClient>("http://localhost:3030/")
.await
.expect("shouldn't fail");
wasm_bindgen_futures::spawn_local(receiver_task);
client
}
#[function_component(App)]
fn test_app() -> Html {
let sum = use_state(|| 0);
{
let sum = sum.clone();
use_effect_with_deps(
move |_| {
let videos = sum.clone();
wasm_bindgen_futures::spawn_local(async move {
let client = connect().await; // todo: connect only once
videos.set(client.add(1, 2).await.expect("todo: handle"));
});
|| ()
},
(),
);
}
html! {
<div>{"hello. result of 1 + 2 = "}{*sum}</div>
}
}
fn main() {
wasm_logger::init(wasm_logger::Config::default());
log::info!("Hello, world!");
yew::start_app::<App>();
} backend/src/main.rs use std::future;
use jsonrpc_core::{Result, BoxFuture};
use jsonrpc_http_server::ServerBuilder;
use common::Rpc;
struct RpcImpl;
// Server implementation
impl Rpc for RpcImpl {
fn add(&self, a: u64, b: u64) -> Result<u64> {
Ok(a + b)
}
fn call(&self, _: u64) -> BoxFuture<Result<String>> {
Box::pin(future::ready(Ok("OK".to_owned())))
}
}
fn main() {
let mut io = jsonrpc_core::IoHandler::new();
io.extend_with(RpcImpl.to_delegate());
let server = ServerBuilder::new(io)
.threads(3)
.start_http(&"127.0.0.1:3030".parse().unwrap())
.unwrap();
server.wait();
} then: trunk serve frontend/index.html # to run the client
cargo run --bin backend # to run the server The only missing part is the http-wasm transport for jsonrpc, implemented here: paritytech/jsonrpc#668 |
Beta Was this translation helpful? Give feedback.
-
it would be nice to have something like https://trpc.io/ |
Beta Was this translation helpful? Give feedback.
-
Question
What are the best alternatives to communicate from front-end rust/yew to back-end rust? Somewhere I read that some folks use GraphQL. But I'm wondering whether there is a more direct, more rusty approach...
Am I just unaware of corresponding technology, or do we have to invent something?
Beta Was this translation helpful? Give feedback.
All reactions