Skip to content

Commit 2b35ab5

Browse files
committed
Replace custom web HTTP client with reqwest
This is more maintenance burden than we can take, and it is better separation of concerns. There was no good third-party wasm web HTTP client when we developed ours, but now there is. Note: the web example compiles, but graphql-hub, the public API we used, is now down.
1 parent 74af385 commit 2b35ab5

File tree

12 files changed

+41
-238
lines changed

12 files changed

+41
-238
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Cargo.lock
55
.idea
66
scripts/*
77
!scripts/*.sh
8+
/.vscode

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ members = [
33
"graphql_client",
44
"graphql_client_cli",
55
"graphql_client_codegen",
6-
"graphql_client_web",
76
"graphql-introspection-query",
87
"graphql_query_derive",
98

examples/web/Cargo.toml

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ version = "0.1.0"
44
authors = ["Tom Houlé <tom@tomhoule.com>"]
55
edition = "2018"
66

7-
# https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#profile-overrides
8-
#[profile.release]
9-
#lto = "thin"
7+
[lib]
8+
crate-type = ["cdylib", "rlib"]
109

11-
[dev-dependencies]
12-
graphql_client = { path = "../../graphql_client" }
13-
graphql_client_web = { path = "../../graphql_client_web" }
10+
[dependencies]
11+
graphql_client = { path = "../../graphql_client", features = ["reqwest"] }
1412
wasm-bindgen = "^0.2"
1513
serde = { version = "1.0.67", features = ["derive"] }
1614
lazy_static = "1.0.1"
1715
js-sys = "0.3.6"
1816
futures-util = "0.3.8"
1917
wasm-bindgen-futures = "0.4.18"
18+
reqwest = "0.11.3"
2019

21-
[dev-dependencies.web-sys]
20+
[dependencies.web-sys]
2221
version = "0.3.6"
2322
features = [
2423
"console",
@@ -30,7 +29,3 @@ features = [
3029
"HtmlDocument",
3130
"HtmlElement",
3231
]
33-
34-
[[example]]
35-
name = "web"
36-
crate-type = ["cdylib"]

examples/web/examples/web.rs renamed to examples/web/src/lib.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use graphql_client::GraphQLQuery;
1+
use graphql_client::{reqwest::post_graphql, GraphQLQuery};
22
use lazy_static::*;
33
use std::cell::RefCell;
44
use std::sync::Mutex;
@@ -9,7 +9,7 @@ use wasm_bindgen_futures::future_to_promise;
99
#[derive(GraphQLQuery)]
1010
#[graphql(
1111
schema_path = "schema.json",
12-
query_path = "examples/puppy_smiles.graphql",
12+
query_path = "src/puppy_smiles.graphql",
1313
response_derives = "Debug"
1414
)]
1515
struct PuppySmiles;
@@ -23,20 +23,22 @@ lazy_static! {
2323
}
2424

2525
async fn load_more() -> Result<JsValue, JsValue> {
26-
let client = graphql_client::web::Client::new("https://www.graphqlhub.com/graphql");
26+
let url = "https://www.graphqlhub.com/graphql";
2727
let variables = puppy_smiles::Variables {
2828
after: LAST_ENTRY
2929
.lock()
3030
.ok()
3131
.and_then(|opt| opt.borrow().to_owned()),
3232
};
33-
let response = client.call(PuppySmiles, variables).await.map_err(|err| {
34-
log(&format!(
35-
"Could not fetch puppies. graphql_client_web error: {:?}",
36-
err
37-
));
38-
JsValue::NULL
39-
})?;
33+
34+
let client = reqwest::Client::new();
35+
36+
let response = post_graphql::<PuppySmiles, _>(&client, url, variables)
37+
.await
38+
.map_err(|err| {
39+
log(&format!("Could not fetch puppies. error: {:?}", err));
40+
JsValue::NULL
41+
})?;
4042
render_response(response);
4143
Ok(JsValue::NULL)
4244
}
@@ -72,14 +74,14 @@ fn add_load_more_button() {
7274
on_click.forget();
7375
}
7476

75-
fn render_response(response: graphql_client_web::Response<puppy_smiles::ResponseData>) {
77+
fn render_response(response: graphql_client::Response<puppy_smiles::ResponseData>) {
7678
use std::fmt::Write;
7779

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

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

82-
let json: graphql_client_web::Response<puppy_smiles::ResponseData> = response;
84+
let json: graphql_client::Response<puppy_smiles::ResponseData> = response;
8385
let response = document()
8486
.create_element("div")
8587
.expect_throw("could not create div");

graphql_client/Cargo.toml

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,10 @@ categories = ["network-programming", "web-programming", "wasm"]
1010
edition = "2018"
1111

1212
[dependencies]
13-
doc-comment = "^0.3"
14-
anyhow = { version = "1.0", optional = true }
15-
thiserror = { version = "1.0", optional = true }
13+
reqwest = { version = "^0.11", features = ["json"], optional = true }
1614
graphql_query_derive = { path = "../graphql_query_derive", version = "0.9.0", optional = true }
17-
serde_json = "1.0"
1815
serde = { version = "^1.0.78", features = ["derive"] }
19-
20-
[dependencies.js-sys]
21-
version = "^0.3"
22-
optional = true
23-
24-
[dependencies.log]
25-
version = "^0.4"
26-
optional = true
27-
28-
[dependencies.web-sys]
29-
version = "^0.3"
30-
optional = true
31-
features = ["Headers", "Request", "RequestInit", "Response", "Window"]
32-
33-
[dependencies.wasm-bindgen]
34-
version = "^0.2"
35-
optional = true
36-
37-
[dependencies.wasm-bindgen-futures]
38-
version = "^0.4"
39-
optional = true
40-
41-
[dev-dependencies]
42-
# Note: If we bumpup wasm-bindge-test version, we should change CI setting.
43-
wasm-bindgen-test = "^0.3"
44-
reqwest = { version = "^0.10", features = ["json", "blocking"] }
16+
serde_json = "1.0"
4517

4618
[features]
4719
default = ["graphql_query_derive"]
48-
web = [
49-
"anyhow",
50-
"thiserror",
51-
"js-sys",
52-
"log",
53-
"wasm-bindgen",
54-
"wasm-bindgen-futures",
55-
"web-sys",
56-
]

graphql_client/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ extern crate graphql_query_derive;
1414
#[doc(hidden)]
1515
pub use graphql_query_derive::*;
1616

17-
use serde::*;
18-
19-
#[cfg(feature = "web")]
20-
pub mod web;
17+
#[cfg(feature = "reqwest")]
18+
pub mod reqwest;
2119

20+
use serde::*;
2221
use std::collections::HashMap;
2322
use std::fmt::{self, Display};
2423

25-
doc_comment::doctest!("../../README.md");
26-
2724
/// A convenience trait that can be used to build a GraphQL request body.
2825
///
2926
/// This will be implemented for you by codegen in the normal case. It is implemented on the struct you place the derive on.

graphql_client/src/reqwest.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! A concrete client implementation over HTTP with reqwest.
2+
3+
use crate::GraphQLQuery;
4+
5+
/// Use the provided reqwest::Client to post a GraphQL request.
6+
pub async fn post_graphql<Q: GraphQLQuery, U: reqwest::IntoUrl>(
7+
client: &reqwest::Client,
8+
url: U,
9+
variables: Q::Variables,
10+
) -> Result<crate::Response<Q::ResponseData>, reqwest::Error> {
11+
let body = Q::build_query(variables);
12+
let reqwest_response = client.post(url).json(&body).send().await?;
13+
14+
Ok(reqwest_response.json().await?)
15+
}

graphql_client/src/web.rs

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

graphql_client_web/Cargo.toml

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

graphql_client_web/README.md

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

graphql_client_web/src/lib.rs

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

0 commit comments

Comments
 (0)