Skip to content

wasm with wasm-bindgen example #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
target/
node_modules/
**/*.rs.bk
Cargo.lock
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This library does not provide any networking, caching or other client functional

```rust
#[derive(GraphQLQuery)]
#[GraphQLQuery(
#[gql(
query = "/graphql/queries/my_query.graphql",
schema = "/graphql/schema.graphql"
)]
Expand Down
4 changes: 4 additions & 0 deletions examples/call_from_js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
call_from_js.d.ts
call_from_js.js
call_from_js_bg.wasm
package-lock.json
15 changes: 15 additions & 0 deletions examples/call_from_js/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "call_from_js"
version = "0.1.0"
authors = ["Tom Houlé <tom@tomhoule.com>"]

[lib]
crate-type = ["cdylib"]

[dependencies]
graphql_client = { path = "../..", version = "0.0.1" }
wasm-bindgen = "0.2.11"
serde = "1.0.67"
serde_derive = "1.0.67"
serde_json = "1.0.22"
lazy_static = "1.0.1"
11 changes: 11 additions & 0 deletions examples/call_from_js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# call from JS example

This is a demo of the library used for webassembly.

## Build

You will need the Rust toolchain, npm and the wasm-bindgen cli (`cargo install wasm-bindgen-cli`) for this to work. Just run:

```
./build.sh
```
9 changes: 9 additions & 0 deletions examples/call_from_js/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set -ex

cargo +nightly build --target wasm32-unknown-unknown --release

wasm-bindgen \
../../target/wasm32-unknown-unknown/debug/call_from_js.wasm --out-dir .

npm install
npm run serve
14 changes: 14 additions & 0 deletions examples/call_from_js/convenient_fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function convenient_post(url, body, onSuccess, onFailure) {
return fetch(url, {
body,
method: "POST",
headers: {
["Content-Type"]: "application/json",
["Accept"]: "application/json"
}
})
.then(res => res.text())
.then(onSuccess)
.catch(f => onSuccess(f.toString()));
// .catch(onFailure);
}
14 changes: 14 additions & 0 deletions examples/call_from_js/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>

<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<!-- boostrap 4 -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB"
crossorigin="anonymous">
</head>

<body>
<script src='./index.js'></script>
</body>

</html>
5 changes: 5 additions & 0 deletions examples/call_from_js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const js = import("./call_from_js");

js.then(js => {
js.run();
});
10 changes: 10 additions & 0 deletions examples/call_from_js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"scripts": {
"serve": "webpack-dev-server"
},
"devDependencies": {
"webpack": "^4.0.1",
"webpack-cli": "^2.0.10",
"webpack-dev-server": "^3.1.0"
}
}
Loading