diff --git a/README.md b/README.md index daa278555..3948ec884 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ A typed GraphQL client library for Rust. ```rust use graphql_client::{GraphQLQuery, Response}; use std::error::Error; + use reqwest; #[derive(GraphQLQuery)] #[graphql( @@ -68,14 +69,14 @@ A typed GraphQL client library for Rust. )] pub struct UnionQuery; - fn perform_my_query(variables: union_query::Variables) -> Result<(), Box> { + async fn perform_my_query(variables: union_query::Variables) -> Result<(), Box> { // this is the important line let request_body = UnionQuery::build_query(variables); let client = reqwest::Client::new(); - let mut res = client.post("/graphql").json(&request_body).send()?; - let response_body: Response = res.json()?; + let mut res = client.post("/graphql").json(&request_body).send().await?; + let response_body: Response = res.json().await?; println!("{:#?}", response_body); Ok(()) } diff --git a/examples/github/examples/github.rs b/examples/github/examples/github.rs index 278d24e39..833ee757b 100644 --- a/examples/github/examples/github.rs +++ b/examples/github/examples/github.rs @@ -51,7 +51,9 @@ fn main() -> Result<(), anyhow::Error> { name: name.to_string(), }); - let client = reqwest::blocking::Client::new(); + let client = reqwest::blocking::Client::builder() + .user_agent("graphql-rust/0.9.0") + .build()?; let res = client .post("https://api.github.com/graphql") @@ -59,17 +61,11 @@ fn main() -> Result<(), anyhow::Error> { .json(&q) .send()?; + res.error_for_status_ref()?; + let response_body: Response = res.json()?; info!("{:?}", response_body); - if let Some(errors) = response_body.errors { - println!("there are errors:"); - - for error in &errors { - println!("{:?}", error); - } - } - let response_data: repo_view::ResponseData = response_body.data.expect("missing response data"); let stars: Option = response_data @@ -80,8 +76,8 @@ fn main() -> Result<(), anyhow::Error> { println!("{}/{} - 🌟 {}", owner, name, stars.unwrap_or(0),); let mut table = prettytable::Table::new(); - - table.add_row(row!(b => "issue", "comments")); + table.set_format(*prettytable::format::consts::FORMAT_NO_LINESEP_WITH_TITLE); + table.set_titles(row!(b => "issue", "comments")); for issue in &response_data .repository diff --git a/graphql_client/Cargo.toml b/graphql_client/Cargo.toml index 0d5b5daca..c3fae68ee 100644 --- a/graphql_client/Cargo.toml +++ b/graphql_client/Cargo.toml @@ -41,6 +41,7 @@ optional = true [dev-dependencies] # Note: If we bumpup wasm-bindge-test version, we should change CI setting. wasm-bindgen-test = "^0.3" +reqwest = { version = "^0.10", features = ["json", "blocking"] } [features] default = ["graphql_query_derive"] diff --git a/graphql_client/src/web.rs b/graphql_client/src/web.rs index 0db9351c2..7242c7331 100644 --- a/graphql_client/src/web.rs +++ b/graphql_client/src/web.rs @@ -79,7 +79,7 @@ impl Client { _query: Q, variables: Q::Variables, ) -> Result, ClientError> { - let window = web_sys::window().ok_or_else(|| ClientError::NoWindow)?; + let window = web_sys::window().ok_or(ClientError::NoWindow)?; let body = serde_json::to_string(&Q::build_query(variables)).map_err(|_| ClientError::Body)?;