Skip to content

Commit d81b2b6

Browse files
authored
Merge pull request #240 from graphql-rust/fold-graphql-client-web
Deprecate graphql_client_web, improve web example
2 parents c9f3045 + 37a1a1b commit d81b2b6

File tree

22 files changed

+284
-273
lines changed

22 files changed

+284
-273
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ script:
2525
- cargo test --all
2626
- cargo build --manifest-path=./examples/github/Cargo.toml
2727
- cargo build --manifest-path=./examples/web/Cargo.toml
28-
- if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then (xvfb-run cargo test --manifest-path=./graphql_client_web/Cargo.toml --target wasm32-unknown-unknown) fi
28+
- if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then (xvfb-run cargo test --manifest-path=./graphql_client/Cargo.toml --features="web" --target wasm32-unknown-unknown) fi

CHANGELOG.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## Unreleased
99

10-
This release sees the switch to the Rust 2018 edition - it is
11-
only compatible with Rust 1.31.0 and later.
10+
## 0.8.0 - 2019-05-24
11+
12+
## Changed
13+
14+
- (BREAKING) This release sees the switch to the Rust 2018 edition - it is only
15+
compatible with Rust 1.31.0 and later. In particular, this changes how the
16+
derive macro is imported. See the README for more details.
17+
- `graphql_client_web` is now deprecated. The browser client has been moved to
18+
the `graphql_client` crate, under the `web` module. **It is only available
19+
with the `web` feature enabled on the `wasm32-unknown-unknown` target.**
1220

1321
## 0.7.1
1422

README.md.skt.md

-7
This file was deleted.

examples/web/Cargo.toml

+1-1
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

+15-3
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

-11
This file was deleted.

examples/web/index.html

+4-1
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

-5
This file was deleted.

examples/web/package.json

-10
This file was deleted.

examples/web/src/lib.rs

+26-21
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()
@@ -48,27 +49,31 @@ fn load_more() -> impl Future<Item = JsValue, Error = JsValue> {
4849

4950
fn document() -> web_sys::Document {
5051
web_sys::window()
51-
.expect("no window")
52+
.expect_throw("no window")
5253
.document()
53-
.expect("no document")
54+
.expect_throw("no document")
5455
}
5556

5657
fn add_load_more_button() {
5758
let btn = document()
5859
.create_element("button")
59-
.expect("could not create button");
60+
.expect_throw("could not create button");
6061
btn.set_inner_html("I WANT MORE PUPPIES");
6162
let on_click = Closure::wrap(
6263
Box::new(move || future_to_promise(load_more())) as Box<FnMut() -> js_sys::Promise>
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_throw("on click is not a Function"),
6771
)
68-
.expect("could not add event listener to load more button");
72+
.expect_throw("could not add event listener to load more button");
6973

70-
let doc = document().body().expect("no body");
71-
doc.append_child(&btn).expect("could not append button");
74+
let doc = document().body().expect_throw("no body");
75+
doc.append_child(&btn)
76+
.expect_throw("could not append button");
7277

7378
on_click.forget();
7479
}
@@ -78,27 +83,27 @@ fn render_response(response: graphql_client_web::Response<puppy_smiles::Response
7883

7984
log(&format!("response body\n\n{:?}", response));
8085

81-
let parent = document().body().expect("no body");
86+
let parent = document().body().expect_throw("no body");
8287

8388
let json: graphql_client_web::Response<puppy_smiles::ResponseData> = response;
8489
let response = document()
8590
.create_element("div")
86-
.expect("could not create div");
91+
.expect_throw("could not create div");
8792
let mut inner_html = String::new();
8893
let listings = json
8994
.data
90-
.expect("response data")
95+
.expect_throw("response data")
9196
.reddit
92-
.expect("reddit")
97+
.expect_throw("reddit")
9398
.subreddit
94-
.expect("puppy smiles subreddit")
99+
.expect_throw("puppy smiles subreddit")
95100
.new_listings;
96101

97102
let new_cursor: Option<String> = listings[listings.len() - 1]
98103
.as_ref()
99104
.map(|puppy| puppy.fullname_id.clone())
100105
.to_owned();
101-
LAST_ENTRY.lock().unwrap().replace(new_cursor);
106+
LAST_ENTRY.lock().unwrap_throw().replace(new_cursor);
102107

103108
for puppy in &listings {
104109
if let Some(puppy) = puppy {
@@ -114,7 +119,7 @@ fn render_response(response: graphql_client_web::Response<puppy_smiles::Response
114119
"#,
115120
puppy.title, puppy.url, puppy.title
116121
)
117-
.expect("write to string");
122+
.expect_throw("write to string");
118123
}
119124
}
120125
response.set_inner_html(&format!(
@@ -123,20 +128,20 @@ fn render_response(response: graphql_client_web::Response<puppy_smiles::Response
123128
));
124129
parent
125130
.append_child(&response)
126-
.expect("could not append response");
131+
.expect_throw("could not append response");
127132
}
128133

129-
#[wasm_bindgen]
134+
#[wasm_bindgen(start)]
130135
pub fn run() {
131136
log("Hello there");
132137
let message_area = document()
133138
.create_element("div")
134-
.expect("could not create div");
139+
.expect_throw("could not create div");
135140
message_area.set_inner_html("<p>good morning</p>");
136-
let parent = document().body().unwrap();
141+
let parent = document().body().unwrap_throw();
137142
parent
138143
.append_child(&message_area)
139-
.expect("could not append message area");
144+
.expect_throw("could not append message area");
140145

141146
load_more();
142147
add_load_more_button();

examples/web/webpack.config.js

-10
This file was deleted.

graphql_client/Cargo.toml

+48-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "graphql_client"
3-
version = "0.7.1"
3+
version = "0.8.0"
44
authors = ["Tom Houlé <[email protected]>"]
55
description = "Typed GraphQL requests and responses"
66
repository = "https://github.com/graphql-rust/graphql-client"
@@ -11,10 +11,54 @@ edition = "2018"
1111

1212
[dependencies]
1313
failure = "0.1"
14-
graphql_query_derive = { path = "../graphql_query_derive", version = "0.7.1" }
14+
graphql_query_derive = { path = "../graphql_query_derive", version = "0.8.0" }
1515
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

+3-5
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)