Skip to content

Commit 366a370

Browse files
committed
Implement reqwest-blocking feature and convert examples
1 parent 1c9fb6d commit 366a370

File tree

7 files changed

+43
-34
lines changed

7 files changed

+43
-34
lines changed

examples/github/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ edition = "2018"
66

77
[dev-dependencies]
88
anyhow = "1.0"
9-
graphql_client = { path = "../../graphql_client" }
9+
graphql_client = { path = "../../graphql_client", features = ["reqwest-blocking"] }
1010
serde = "^1.0"
1111
reqwest = { version = "^0.11", features = ["json", "blocking"] }
1212
prettytable-rs = "^0.7"
1313
structopt = "^0.3"
14-
envy = "^0.3"
1514
log = "^0.4"
1615
env_logger = "^0.5"

examples/github/examples/github.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use ::reqwest::blocking::Client;
22
use anyhow::*;
3-
use graphql_client::*;
3+
use graphql_client::{reqwest::post_graphql_blocking as post_graphql, GraphQLQuery};
44
use log::*;
55
use prettytable::*;
6-
use serde::*;
76
use structopt::StructOpt;
87

98
#[allow(clippy::upper_case_acronyms)]
@@ -24,11 +23,6 @@ struct Command {
2423
repo: String,
2524
}
2625

27-
#[derive(Deserialize, Debug)]
28-
struct Env {
29-
github_api_token: String,
30-
}
31-
3226
fn parse_repo_name(repo_name: &str) -> Result<(&str, &str), anyhow::Error> {
3327
let mut parts = repo_name.split('/');
3428
match (parts.next(), parts.next()) {
@@ -40,29 +34,34 @@ fn parse_repo_name(repo_name: &str) -> Result<(&str, &str), anyhow::Error> {
4034
fn main() -> Result<(), anyhow::Error> {
4135
env_logger::init();
4236

43-
let config: Env = envy::from_env().context("while reading from environment")?;
37+
let github_api_token =
38+
std::env::var("GITHUB_API_TOKEN").expect("Missing GITHUB_API_TOKEN env var");
4439

4540
let args = Command::from_args();
4641

4742
let repo = args.repo;
4843
let (owner, name) = parse_repo_name(&repo).unwrap_or(("tomhoule", "graphql-client"));
4944

50-
let q = RepoView::build_query(repo_view::Variables {
45+
let variables = repo_view::Variables {
5146
owner: owner.to_string(),
5247
name: name.to_string(),
53-
});
54-
55-
let client = Client::builder().user_agent("graphql-rust/0.9.0").build()?;
56-
57-
let res = client
58-
.post("https://api.github.com/graphql")
59-
.bearer_auth(config.github_api_token)
60-
.json(&q)
61-
.send()?;
62-
63-
res.error_for_status_ref()?;
48+
};
49+
50+
let client = Client::builder()
51+
.user_agent("graphql-rust/0.9.0")
52+
.default_headers(
53+
std::iter::once((
54+
reqwest::header::AUTHORIZATION,
55+
reqwest::header::HeaderValue::from_str(&format!("Bearer {}", github_api_token))
56+
.unwrap(),
57+
))
58+
.collect(),
59+
)
60+
.build()?;
61+
62+
let response_body =
63+
post_graphql::<RepoView, _>(&client, "https://api.github.com/graphql", variables).unwrap();
6464

65-
let response_body: Response<repo_view::ResponseData> = res.json()?;
6665
info!("{:?}", response_body);
6766

6867
let response_data: repo_view::ResponseData = response_body.data.expect("missing response data");

examples/hasura/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2018"
66

77
[dev-dependencies]
88
anyhow = "1.0"
9-
graphql_client = { path = "../../graphql_client", features = ["reqwest"] }
9+
graphql_client = { path = "../../graphql_client", features = ["reqwest-blocking"] }
1010
serde = { version = "1.0", features = ["derive"] }
1111
serde_json = "1.0"
1212
reqwest = { version = "^0.11", features = ["json", "blocking"] }

examples/hasura/examples/hasura.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ::reqwest::blocking::Client;
2-
use graphql_client::*;
2+
use graphql_client::{reqwest::post_graphql_blocking as post_graphql, GraphQLQuery};
33
use log::*;
44
use prettytable::*;
55

@@ -19,24 +19,20 @@ fn main() -> Result<(), anyhow::Error> {
1919
use upsert_issue::{IssuesUpdateColumn::*, *};
2020
env_logger::init();
2121

22-
let q = UpsertIssue::build_query(Variables {
22+
let v = Variables {
2323
issues: vec![IssuesInsertInput {
2424
id: Some("001000000000000".to_string()),
2525
name: Some("Name".to_string()),
2626
status: Some("Draft".to_string()),
2727
salesforce_updated_at: Some("2019-06-11T08:14:28Z".to_string()),
2828
}],
2929
update_columns: vec![Name, Status, SalesforceUpdatedAt],
30-
});
30+
};
3131

3232
let client = Client::new();
3333

34-
let res = client
35-
.post("https://localhost:8080/v1/graphql")
36-
.json(&q)
37-
.send()?;
38-
39-
let response_body: Response<ResponseData> = res.json()?;
34+
let response_body =
35+
post_graphql::<UpsertIssue, _>(&client, "https://localhost:8080/v1/graphql", v)?;
4036
info!("{:?}", response_body);
4137

4238
if let Some(errors) = response_body.errors {

graphql_client/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ serde_json = "1.0"
1717

1818
[features]
1919
default = ["graphql_query_derive"]
20+
reqwest-blocking = ["reqwest/blocking"]

graphql_client/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern crate graphql_query_derive;
1414
#[doc(hidden)]
1515
pub use graphql_query_derive::*;
1616

17-
#[cfg(feature = "reqwest")]
17+
#[cfg(any(feature = "reqwest", feature = "reqwest-blocking"))]
1818
pub mod reqwest;
1919

2020
use serde::*;

graphql_client/src/reqwest.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::GraphQLQuery;
44

55
/// Use the provided reqwest::Client to post a GraphQL request.
6+
#[cfg(feature = "reqwest")]
67
pub async fn post_graphql<Q: GraphQLQuery, U: reqwest::IntoUrl>(
78
client: &reqwest::Client,
89
url: U,
@@ -13,3 +14,16 @@ pub async fn post_graphql<Q: GraphQLQuery, U: reqwest::IntoUrl>(
1314

1415
Ok(reqwest_response.json().await?)
1516
}
17+
18+
/// Use the provided reqwest::Client to post a GraphQL request.
19+
#[cfg(feature = "reqwest-blocking")]
20+
pub fn post_graphql_blocking<Q: GraphQLQuery, U: reqwest::IntoUrl>(
21+
client: &reqwest::blocking::Client,
22+
url: U,
23+
variables: Q::Variables,
24+
) -> Result<crate::Response<Q::ResponseData>, reqwest::Error> {
25+
let body = Q::build_query(variables);
26+
let reqwest_response = client.post(url).json(&body).send()?;
27+
28+
reqwest_response.json()
29+
}

0 commit comments

Comments
 (0)