Skip to content

Deprecate graphql_client_web, improve web example #240

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 4 commits into from
May 24, 2019
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ script:
- cargo test --all
- cargo build --manifest-path=./examples/github/Cargo.toml
- cargo build --manifest-path=./examples/web/Cargo.toml
- if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then (xvfb-run cargo test --manifest-path=./graphql_client_web/Cargo.toml --target wasm32-unknown-unknown) fi
- if [ "$TRAVIS_RUST_VERSION" = "stable" ]; then (xvfb-run cargo test --manifest-path=./graphql_client/Cargo.toml --features="web" --target wasm32-unknown-unknown) fi
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Unreleased

This release sees the switch to the Rust 2018 edition - it is
only compatible with Rust 1.31.0 and later.
## 0.8.0 - 2019-05-24

## Changed

- (BREAKING) This release sees the switch to the Rust 2018 edition - it is only
compatible with Rust 1.31.0 and later. In particular, this changes how the
derive macro is imported. See the README for more details.
- `graphql_client_web` is now deprecated. The browser client has been moved to
the `graphql_client` crate, under the `web` module. **It is only available
with the `web` feature enabled on the `wasm32-unknown-unknown` target.**

## 0.7.1

Expand Down
7 changes: 0 additions & 7 deletions README.md.skt.md

This file was deleted.

2 changes: 1 addition & 1 deletion examples/web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ crate-type = ["cdylib"]
[dependencies]
graphql_client = { path = "../../graphql_client" }
graphql_client_web = { path = "../../graphql_client_web" }
wasm-bindgen = "0.2.12"
wasm-bindgen = "0.2.43"
serde = { version = "1.0.67", features = ["derive"] }
serde_json = "1.0.22"
lazy_static = "1.0.1"
Expand Down
18 changes: 15 additions & 3 deletions examples/web/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
# call from JS example

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

## Build

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

```bash
wasm-pack build --target=web
```
./build.sh

The compiled WebAssembly program and the glue JS code will be
located in the `./pkg` directory. To run the app, start an
HTTP server in this directory - it contains an `index.html`
file. For example, if you have Python 3:

```bash
python3 -m http.server 8000
```
11 changes: 0 additions & 11 deletions examples/web/build.sh

This file was deleted.

5 changes: 4 additions & 1 deletion examples/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
</head>

<body>
<script src='./index.js'></script>
<script type="module">
import init from '/pkg/web.js';
init("/pkg/web_bg.wasm");
</script>
</body>

</html>
5 changes: 0 additions & 5 deletions examples/web/index.js

This file was deleted.

10 changes: 0 additions & 10 deletions examples/web/package.json

This file was deleted.

47 changes: 26 additions & 21 deletions examples/web/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use futures::Future;
use graphql_client_web::*;
use graphql_client::GraphQLQuery;
use lazy_static::*;
use std::cell::RefCell;
use std::sync::Mutex;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::future_to_promise;

#[derive(GraphQLQuery)]
Expand All @@ -23,7 +24,7 @@ lazy_static! {
}

fn load_more() -> impl Future<Item = JsValue, Error = JsValue> {
let client = graphql_client_web::Client::new("https://www.graphqlhub.com/graphql");
let client = graphql_client::web::Client::new("https://www.graphqlhub.com/graphql");
let variables = puppy_smiles::Variables {
after: LAST_ENTRY
.lock()
Expand All @@ -48,27 +49,31 @@ fn load_more() -> impl Future<Item = JsValue, Error = JsValue> {

fn document() -> web_sys::Document {
web_sys::window()
.expect("no window")
.expect_throw("no window")
.document()
.expect("no document")
.expect_throw("no document")
}

fn add_load_more_button() {
let btn = document()
.create_element("button")
.expect("could not create button");
.expect_throw("could not create button");
btn.set_inner_html("I WANT MORE PUPPIES");
let on_click = Closure::wrap(
Box::new(move || future_to_promise(load_more())) as Box<FnMut() -> js_sys::Promise>
);
btn.add_event_listener_with_callback(
"click",
js_sys::Function::try_from(&on_click.as_ref()).expect("on click is not a Function"),
&on_click
.as_ref()
.dyn_ref()
.expect_throw("on click is not a Function"),
)
.expect("could not add event listener to load more button");
.expect_throw("could not add event listener to load more button");

let doc = document().body().expect("no body");
doc.append_child(&btn).expect("could not append button");
let doc = document().body().expect_throw("no body");
doc.append_child(&btn)
.expect_throw("could not append button");

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

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

let parent = document().body().expect("no body");
let parent = document().body().expect_throw("no body");

let json: graphql_client_web::Response<puppy_smiles::ResponseData> = response;
let response = document()
.create_element("div")
.expect("could not create div");
.expect_throw("could not create div");
let mut inner_html = String::new();
let listings = json
.data
.expect("response data")
.expect_throw("response data")
.reddit
.expect("reddit")
.expect_throw("reddit")
.subreddit
.expect("puppy smiles subreddit")
.expect_throw("puppy smiles subreddit")
.new_listings;

let new_cursor: Option<String> = listings[listings.len() - 1]
.as_ref()
.map(|puppy| puppy.fullname_id.clone())
.to_owned();
LAST_ENTRY.lock().unwrap().replace(new_cursor);
LAST_ENTRY.lock().unwrap_throw().replace(new_cursor);

for puppy in &listings {
if let Some(puppy) = puppy {
Expand All @@ -114,7 +119,7 @@ fn render_response(response: graphql_client_web::Response<puppy_smiles::Response
"#,
puppy.title, puppy.url, puppy.title
)
.expect("write to string");
.expect_throw("write to string");
}
}
response.set_inner_html(&format!(
Expand All @@ -123,20 +128,20 @@ fn render_response(response: graphql_client_web::Response<puppy_smiles::Response
));
parent
.append_child(&response)
.expect("could not append response");
.expect_throw("could not append response");
}

#[wasm_bindgen]
#[wasm_bindgen(start)]
pub fn run() {
log("Hello there");
let message_area = document()
.create_element("div")
.expect("could not create div");
.expect_throw("could not create div");
message_area.set_inner_html("<p>good morning</p>");
let parent = document().body().unwrap();
let parent = document().body().unwrap_throw();
parent
.append_child(&message_area)
.expect("could not append message area");
.expect_throw("could not append message area");

load_more();
add_load_more_button();
Expand Down
10 changes: 0 additions & 10 deletions examples/web/webpack.config.js

This file was deleted.

52 changes: 48 additions & 4 deletions graphql_client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "graphql_client"
version = "0.7.1"
version = "0.8.0"
authors = ["Tom Houlé <tom@tomhoule.com>"]
description = "Typed GraphQL requests and responses"
repository = "https://github.com/graphql-rust/graphql-client"
Expand All @@ -11,10 +11,54 @@ edition = "2018"

[dependencies]
failure = "0.1"
graphql_query_derive = { path = "../graphql_query_derive", version = "0.7.1" }
graphql_query_derive = { path = "../graphql_query_derive", version = "0.8.0" }
serde = { version = "^1.0.78", features = ["derive"] }
serde_json = "1.0"
doc-comment = "0.3.1"

[dev-dependencies]
reqwest = "*"
[dependencies.futures]
version = "0.1"
optional = true

[dependencies.js-sys]
version = "0.3.5"
optional = true

[dependencies.log]
version = "0.4.6"
optional = true

[dependencies.web-sys]
version = "0.3.2"
optional = true
features = [
"Headers",
"Request",
"RequestInit",
"Response",
"Window",
]

[dependencies.wasm-bindgen]
version = "0.2.43"
optional = true

[dependencies.wasm-bindgen-futures]
version = "0.3.2"
optional = true

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies.reqwest]
version = "*"

[dev-dependencies.wasm-bindgen-test]
version = "0.2.43"

[features]
web = [
"futures",
"js-sys",
"log",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
]
8 changes: 3 additions & 5 deletions graphql_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ pub use graphql_query_derive::*;

use serde::*;

#[cfg(test)]
use serde_json::json;

#[doc(hidden)]
pub use graphql_query_derive::*;
#[cfg(feature = "web")]
pub mod web;

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

#[test]
fn graphql_error_works_with_just_message() {
Expand Down
Loading