Skip to content

Commit 36cca4d

Browse files
feat: support batch queries
1 parent 46791c8 commit 36cca4d

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

graphql_client/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ pub trait GraphQLQuery {
8686

8787
/// Produce a GraphQL query struct that can be JSON serialized and sent to a GraphQL API.
8888
fn build_query(variables: Self::Variables) -> QueryBody<Self::Variables>;
89+
/// Produce a GraphQL batch query struct that can be JSON serialized and sent to a GraphQL API.
90+
fn build_batch_query(variables: Vec<Self::Variables>) -> Vec<QueryBody<Self::Variables>>;
8991
}
9092

9193
/// The form in which queries are sent over HTTP in most implementations. This will be built using the [`GraphQLQuery`] trait normally.

graphql_client/src/reqwest.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ pub async fn post_graphql<Q: GraphQLQuery, U: reqwest::IntoUrl>(
1616
reqwest_response.json().await
1717
}
1818

19+
/// Use the provided reqwest::Client to post a GraphQL request.
20+
#[cfg(any(feature = "reqwest", feature = "reqwest-rustls"))]
21+
pub async fn post_graphql_batch<Q: GraphQLQuery, U: reqwest::IntoUrl>(
22+
client: &reqwest::Client,
23+
url: U,
24+
variables: Vec<Q::Variables>,
25+
) -> Result<crate::Response<Q::ResponseData>, reqwest::Error> {
26+
let body = Q::build_batch_query(variables);
27+
let reqwest_response = client.post(url).json(&body).send().await?;
28+
29+
reqwest_response.json().await
30+
}
31+
1932
/// Use the provided reqwest::Client to post a GraphQL request.
2033
#[cfg(feature = "reqwest-blocking")]
2134
pub fn post_graphql_blocking<Q: GraphQLQuery, U: reqwest::IntoUrl>(
@@ -28,3 +41,16 @@ pub fn post_graphql_blocking<Q: GraphQLQuery, U: reqwest::IntoUrl>(
2841

2942
reqwest_response.json()
3043
}
44+
45+
/// Use the provided reqwest::Client to post a GraphQL request.
46+
#[cfg(feature = "reqwest-blocking")]
47+
pub fn post_graphql_blocking_batch<Q: GraphQLQuery, U: reqwest::IntoUrl>(
48+
client: &reqwest::blocking::Client,
49+
url: U,
50+
variables: Vec<Q::Variables>,
51+
) -> Result<crate::Response<Q::ResponseData>, reqwest::Error> {
52+
let body = Q::build_batch_query(variables);
53+
let reqwest_response = client.post(url).json(&body).send()?;
54+
55+
reqwest_response.json()
56+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use graphql_client::*;
2+
3+
#[derive(GraphQLQuery)]
4+
#[graphql(
5+
query_path = "tests/operation_selection/queries.graphql",
6+
schema_path = "tests/operation_selection/schema.graphql",
7+
response_derives = "Debug, PartialEq, Eq"
8+
)]
9+
pub struct Echo;
10+
11+
#[test]
12+
fn batch_query() {
13+
let echo_variables = vec![
14+
echo::Variables {
15+
msg: Some("hi".to_string()),
16+
},
17+
echo::Variables {
18+
msg: Some("hello".to_string()),
19+
},
20+
];
21+
let echo_batch_queries: serde_json::Value =
22+
serde_json::to_value(Echo::build_batch_query(echo_variables))
23+
.expect("Failed to serialize the query!");
24+
assert_eq!(
25+
echo_batch_queries.to_string(),
26+
r#"[{"operationName":"Echo","query":"query Heights($buildingId: ID!, $mountainName: String) {\n mountainHeight(name: $mountainName)\n buildingHeight(id: $buildingId)\n}\n\nquery Echo($msg: String) {\n echo(msg: $msg)\n}\n","variables":{"msg":"hi"}},{"operationName":"Echo","query":"query Heights($buildingId: ID!, $mountainName: String) {\n mountainHeight(name: $mountainName)\n buildingHeight(id: $buildingId)\n}\n\nquery Echo($msg: String) {\n echo(msg: $msg)\n}\n","variables":{"msg":"hello"}}]"#
27+
);
28+
}

0 commit comments

Comments
 (0)