diff --git a/graphql_client/src/lib.rs b/graphql_client/src/lib.rs index 67364730..07552444 100644 --- a/graphql_client/src/lib.rs +++ b/graphql_client/src/lib.rs @@ -86,6 +86,8 @@ pub trait GraphQLQuery { /// Produce a GraphQL query struct that can be JSON serialized and sent to a GraphQL API. fn build_query(variables: Self::Variables) -> QueryBody; + /// Produce a GraphQL batch query struct that can be JSON serialized and sent to a GraphQL API. + fn build_batch_query(variables: Vec) -> Vec>; } /// The form in which queries are sent over HTTP in most implementations. This will be built using the [`GraphQLQuery`] trait normally. diff --git a/graphql_client/src/reqwest.rs b/graphql_client/src/reqwest.rs index 3541915e..ecb1e987 100644 --- a/graphql_client/src/reqwest.rs +++ b/graphql_client/src/reqwest.rs @@ -16,6 +16,19 @@ pub async fn post_graphql( reqwest_response.json().await } +/// Use the provided reqwest::Client to post a GraphQL request. +#[cfg(any(feature = "reqwest", feature = "reqwest-rustls"))] +pub async fn post_graphql_batch( + client: &reqwest::Client, + url: U, + variables: Vec, +) -> Result, reqwest::Error> { + let body = Q::build_batch_query(variables); + let reqwest_response = client.post(url).json(&body).send().await?; + + reqwest_response.json().await +} + /// Use the provided reqwest::Client to post a GraphQL request. #[cfg(feature = "reqwest-blocking")] pub fn post_graphql_blocking( @@ -28,3 +41,16 @@ pub fn post_graphql_blocking( reqwest_response.json() } + +/// Use the provided reqwest::Client to post a GraphQL request. +#[cfg(feature = "reqwest-blocking")] +pub fn post_graphql_blocking_batch( + client: &reqwest::blocking::Client, + url: U, + variables: Vec, +) -> Result, reqwest::Error> { + let body = Q::build_batch_query(variables); + let reqwest_response = client.post(url).json(&body).send()?; + + reqwest_response.json() +} diff --git a/graphql_client/tests/batch_queries.rs b/graphql_client/tests/batch_queries.rs new file mode 100644 index 00000000..6005c55f --- /dev/null +++ b/graphql_client/tests/batch_queries.rs @@ -0,0 +1,28 @@ +use graphql_client::*; + +#[derive(GraphQLQuery)] +#[graphql( + query_path = "tests/operation_selection/queries.graphql", + schema_path = "tests/operation_selection/schema.graphql", + response_derives = "Debug, PartialEq, Eq" +)] +pub struct Echo; + +#[test] +fn batch_query() { + let echo_variables = vec![ + echo::Variables { + msg: Some("hi".to_string()), + }, + echo::Variables { + msg: Some("hello".to_string()), + }, + ]; + let echo_batch_queries: serde_json::Value = + serde_json::to_value(Echo::build_batch_query(echo_variables)) + .expect("Failed to serialize the query!"); + assert_eq!( + echo_batch_queries.to_string(), + 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"}}]"# + ); +}