Skip to content

Commit 36610a8

Browse files
committed
Deprecate graphql_client_web, improve web example
The web client now lives in `graphql_client::web`, behind the `web` feature. The crate is still present but deprecated. It now reexports from graphql_client.
1 parent c9f3045 commit 36610a8

File tree

16 files changed

+246
-237
lines changed

16 files changed

+246
-237
lines changed

examples/web/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ crate-type = ["cdylib"]
1313
[dependencies]
1414
graphql_client = { path = "../../graphql_client" }
1515
graphql_client_web = { path = "../../graphql_client_web" }
16-
wasm-bindgen = "0.2.12"
16+
wasm-bindgen = "0.2.43"
1717
serde = { version = "1.0.67", features = ["derive"] }
1818
serde_json = "1.0.22"
1919
lazy_static = "1.0.1"

examples/web/README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
# call from JS example
22

3-
This is a demo of the library compiled to webassembly for use in a browser.
3+
This is a demo of the library compiled to WebAssembly for use in a browser.
44

55
## Build
66

7-
You will need the Rust toolchain, npm and the wasm-bindgen cli (`cargo install wasm-bindgen-cli`) for this to work. Just run:
7+
You will need the Rust toolchain and the
8+
[`wasm-pack`](https://rustwasm.github.io/wasm-pack/) CLI
9+
(`cargo install --force wasm-pack`) for this to work. To build
10+
the project, run the following command in this directory:
811

12+
```bash
13+
wasm-pack build --target=web
914
```
10-
./build.sh
15+
16+
The compiled WebAssembly program and the glue JS code will be
17+
located in the `./pkg` directory. To run the app, start an
18+
HTTP server in this directory - it contains an `index.html`
19+
file. For example, if you have Python 3:
20+
21+
```bash
22+
python3 -m http.server 8000
1123
```

examples/web/build.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

examples/web/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
</head>
99

1010
<body>
11-
<script src='./index.js'></script>
11+
<script type="module">
12+
import init from '/pkg/web.js';
13+
init("/pkg/web_bg.wasm");
14+
</script>
1215
</body>
1316

1417
</html>

examples/web/index.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

examples/web/package.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/web/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use futures::Future;
2-
use graphql_client_web::*;
2+
use graphql_client::GraphQLQuery;
33
use lazy_static::*;
44
use std::cell::RefCell;
55
use std::sync::Mutex;
66
use wasm_bindgen::prelude::*;
7+
use wasm_bindgen::JsCast;
78
use wasm_bindgen_futures::future_to_promise;
89

910
#[derive(GraphQLQuery)]
@@ -23,7 +24,7 @@ lazy_static! {
2324
}
2425

2526
fn load_more() -> impl Future<Item = JsValue, Error = JsValue> {
26-
let client = graphql_client_web::Client::new("https://www.graphqlhub.com/graphql");
27+
let client = graphql_client::web::Client::new("https://www.graphqlhub.com/graphql");
2728
let variables = puppy_smiles::Variables {
2829
after: LAST_ENTRY
2930
.lock()
@@ -63,7 +64,10 @@ fn add_load_more_button() {
6364
);
6465
btn.add_event_listener_with_callback(
6566
"click",
66-
js_sys::Function::try_from(&on_click.as_ref()).expect("on click is not a Function"),
67+
&on_click
68+
.as_ref()
69+
.dyn_ref()
70+
.expect("on click is not a Function"),
6771
)
6872
.expect("could not add event listener to load more button");
6973

@@ -126,7 +130,7 @@ fn render_response(response: graphql_client_web::Response<puppy_smiles::Response
126130
.expect("could not append response");
127131
}
128132

129-
#[wasm_bindgen]
133+
#[wasm_bindgen(start)]
130134
pub fn run() {
131135
log("Hello there");
132136
let message_area = document()

examples/web/webpack.config.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

graphql_client/Cargo.toml

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,49 @@ serde = { version = "^1.0.78", features = ["derive"] }
1616
serde_json = "1.0"
1717
doc-comment = "0.3.1"
1818

19-
[dev-dependencies]
20-
reqwest = "*"
19+
[dependencies.futures]
20+
version = "0.1"
21+
optional = true
22+
23+
[dependencies.js-sys]
24+
version = "0.3.5"
25+
optional = true
26+
27+
[dependencies.log]
28+
version = "0.4.6"
29+
optional = true
30+
31+
[dependencies.web-sys]
32+
version = "0.3.2"
33+
optional = true
34+
features = [
35+
"Headers",
36+
"Request",
37+
"RequestInit",
38+
"Response",
39+
"Window",
40+
]
41+
42+
[dependencies.wasm-bindgen]
43+
version = "0.2.43"
44+
optional = true
45+
46+
[dependencies.wasm-bindgen-futures]
47+
version = "0.3.2"
48+
optional = true
49+
50+
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies.reqwest]
51+
version = "*"
52+
53+
[dev-dependencies.wasm-bindgen-test]
54+
version = "0.2.43"
55+
56+
[features]
57+
web = [
58+
"futures",
59+
"js-sys",
60+
"log",
61+
"wasm-bindgen",
62+
"wasm-bindgen-futures",
63+
"web-sys",
64+
]

graphql_client/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ pub use graphql_query_derive::*;
1515

1616
use serde::*;
1717

18-
#[cfg(test)]
19-
use serde_json::json;
20-
21-
#[doc(hidden)]
22-
pub use graphql_query_derive::*;
18+
#[cfg(feature = "web")]
19+
pub mod web;
2320

2421
use std::collections::HashMap;
2522
use std::fmt::{self, Display};
@@ -289,6 +286,7 @@ pub struct Response<Data> {
289286
#[cfg(test)]
290287
mod tests {
291288
use super::*;
289+
use serde_json::json;
292290

293291
#[test]
294292
fn graphql_error_works_with_just_message() {

0 commit comments

Comments
 (0)