Skip to content

Commit 65ee00b

Browse files
committed
Complete the wasm-bindgen example
1 parent 5985f5b commit 65ee00b

File tree

16 files changed

+6479
-8
lines changed

16 files changed

+6479
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
/target
1+
target/
2+
node_modules/
23
**/*.rs.bk
34
Cargo.lock

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ serde_json = "1.0"
2020
[workspace]
2121
members = [
2222
".",
23+
"examples/call_from_js",
2324
"examples/github",
2425
"graphql_query_derive",
2526
"graphql_client_cli",

examples/call_from_js/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
call_from_js.d.ts
2+
call_from_js.js
3+
call_from_js_bg.wasm
4+
package-lock.json

examples/call_from_js/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "call_from_js"
3+
version = "0.1.0"
4+
authors = ["Tom Houlé <tom@tomhoule.com>"]
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
[dependencies]
10+
graphql_client = { path = "../..", version = "0.0.1" }
11+
wasm-bindgen = "0.2.11"
12+
serde = "1.0.67"
13+
serde_derive = "1.0.67"
14+
serde_json = "1.0.22"
15+
lazy_static = "1.0.1"

examples/call_from_js/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# call from JS example
2+
3+
This is a demo of the library used for webassembly.
4+
5+
## Build
6+
7+
You will need the Rust toolchain, npm and the wasm-bindgen cli (`cargo install wasm-bindgen-cli`) for this to work. Just run:
8+
9+
```
10+
./build.sh
11+
```

examples/call_from_js/build.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
set -ex
2+
3+
cargo +nightly build --target wasm32-unknown-unknown --release
4+
5+
wasm-bindgen \
6+
../../target/wasm32-unknown-unknown/debug/call_from_js.wasm --out-dir .
7+
8+
npm install
9+
npm run serve
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function convenient_post(url, body, onSuccess, onFailure) {
2+
return fetch(url, {
3+
body,
4+
method: "POST",
5+
headers: {
6+
["Content-Type"]: "application/json",
7+
["Accept"]: "application/json"
8+
}
9+
})
10+
.then(res => res.text())
11+
.then(onSuccess)
12+
.catch(f => onSuccess(f.toString()));
13+
// .catch(onFailure);
14+
}

examples/call_from_js/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
3+
<head>
4+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
5+
<!-- boostrap 4 -->
6+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB"
7+
crossorigin="anonymous">
8+
</head>
9+
10+
<body>
11+
<script src='./index.js'></script>
12+
</body>
13+
14+
</html>

examples/call_from_js/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const js = import("./call_from_js");
2+
3+
js.then(js => {
4+
js.run();
5+
});

examples/call_from_js/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"scripts": {
3+
"serve": "webpack-dev-server"
4+
},
5+
"devDependencies": {
6+
"webpack": "^4.0.1",
7+
"webpack-cli": "^2.0.10",
8+
"webpack-dev-server": "^3.1.0"
9+
}
10+
}

0 commit comments

Comments
 (0)